首页 > 编程知识 正文

Python程序设计实验三

时间:2023-11-22 09:54:45 阅读:304144 作者:ROEZ

在本文中,我们将详细介绍Python程序设计实验三的各个方面。实验三是一个关于数据结构的实验,我们将学习如何使用Python编写并操作各种数据结构。

一、栈

栈是一种先进后出(Last In, First Out)的数据结构。在Python中,我们可以使用列表来实现栈。下面是一个栈的示例代码:

class Stack:
    def __init__(self):
        self.items = []

    def push(self, item):
        self.items.append(item)

    def pop(self):
        return self.items.pop()

    def is_empty(self):
        return len(self.items) == 0

    def size(self):
        return len(self.items)

stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(stack.pop())  # 输出3
print(stack.size())  # 输出2
print(stack.is_empty())  # 输出False

上述代码中,我们定义了一个Stack类,包含push、pop、is_empty和size这几个方法来实现栈的基本操作。通过使用列表来保存栈元素,我们可以方便地进行入栈、出栈、判断栈是否为空以及获取栈的大小。

二、队列

队列是一种先进先出(First In, First Out)的数据结构。在Python中,我们可以使用列表或者collections模块中的deque来实现队列。下面是一个队列的示例代码:

from collections import deque

queue = deque()
queue.append(1)
queue.append(2)
queue.append(3)
print(queue.popleft())  # 输出1
print(len(queue))  # 输出2
print(len(queue) == 0)  # 输出False

上述代码中,我们使用deque类来定义一个队列。通过使用append和popleft这两个方法,我们可以方便地进行入队和出队操作。通过len函数可以获取队列的大小,从而判断队列是否为空。

三、链表

链表是一种动态数据结构,它由一系列节点组成,每个节点包含数据和指向下一个节点的指针。在Python中,可以使用类来实现链表。下面是一个链表的示例代码:

class Node:
    def __init__(self, value):
        self.value = value
        self.next = None

class LinkedList:
    def __init__(self):
        self.head = None

    def add(self, value):
        new_node = Node(value)
        if self.head is None:
            self.head = new_node
        else:
            current_node = self.head
            while current_node.next:
                current_node = current_node.next
            current_node.next = new_node

    def remove(self, value):
        if self.head is None:
            return
        if self.head.value == value:
            self.head = self.head.next
        else:
            current_node = self.head
            while current_node.next:
                if current_node.next.value == value:
                    current_node.next = current_node.next.next
                    break
                current_node = current_node.next

    def print(self):
        if self.head is None:
            return
        current_node = self.head
        while current_node:
            print(current_node.value)
            current_node = current_node.next

linked_list = LinkedList()
linked_list.add(1)
linked_list.add(2)
linked_list.add(3)
linked_list.remove(2)
linked_list.print()  # 输出1, 3

上述代码中,我们定义了一个Node类和一个LinkedList类来实现链表。通过使用add方法可以添加节点,使用remove方法可以删除指定值的节点,使用print方法可以打印链表的所有节点。

四、二叉树

二叉树是一种特殊的树形数据结构,每个节点最多有两个子节点。在Python中,可以使用类来表示二叉树。下面是一个二叉树的示例代码:

class TreeNode:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

class BinaryTree:
    def __init__(self):
        self.root = None

    def add(self, value):
        new_node = TreeNode(value)
        if self.root is None:
            self.root = new_node
        else:
            queue = deque()
            queue.append(self.root)
            while queue:
                current_node = queue.popleft()
                if current_node.left is None:
                    current_node.left = new_node
                    return
                elif current_node.right is None:
                    current_node.right = new_node
                    return
                else:
                    queue.append(current_node.left)
                    queue.append(current_node.right)

    def print(self):
        def print_node(node):
            if node:
                print(node.value)
                print_node(node.left)
                print_node(node.right)
        
        print_node(self.root)

binary_tree = BinaryTree()
binary_tree.add(1)
binary_tree.add(2)
binary_tree.add(3)
binary_tree.print()  # 输出1, 2, 3

上述代码中,我们定义了一个TreeNode类和一个BinaryTree类来实现二叉树。通过使用add方法可以添加节点,使用print方法可以打印二叉树的所有节点。

五、总结

本文我们介绍了Python程序设计实验三涉及到的几个数据结构,包括栈、队列、链表和二叉树。通过学习和理解这些数据结构的实现,我们可以更好地理解和应用Python编程语言。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。