(1)设计一个顺序栈的基本操作的演示程序 (2)设计一个链栈的基本操作的演示程序 (3)设计一个循环队列的基本操作的演示程序 (4)设计一个链队列的基本操作的演示程序
时间: 2024-12-02 07:15:37 浏览: 8
(1)**顺序栈(数组栈)的基本操作演示程序**:
```python
class ArrayStack:
def __init__(self, capacity):
self.stack = [None] * capacity
self.top = -1
# 压栈(入栈)
def push(self, item):
if self.is_full():
print("栈已满,无法压栈")
else:
self.top += 1
self.stack[self.top] = item
# 弹栈(出栈)
def pop(self):
if self.is_empty():
print("栈为空,无法弹栈")
else:
return self.stack.pop(self.top)
# 查看栈顶元素
def peek(self):
if self.is_empty():
print("栈为空,无元素可见")
else:
return self.stack[self.top]
# 判断栈是否为空
def is_empty(self):
return self.top == -1
# 判断栈是否已满
def is_full(self):
return self.top == len(self.stack) - 1
# 使用示例
s = ArrayStack(5)
s.push(1)
s.push(2)
print(s.peek()) # 输出:2
s.pop()
```
(2)**链栈(链表栈)的基本操作演示程序**:
```python
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedStack:
def __init__(self):
self.top = None
# 压栈
def push(self, item):
new_node = Node(item)
new_node.next = self.top
self.top = new_node
# 弹栈
def pop(self):
if self.is_empty():
return None
popped_item = self.top.data
self.top = self.top.next
return popped_item
# 查看栈顶元素
def peek(self):
if self.is_empty():
return None
return self.top.data
# 判断栈是否为空
def is_empty(self):
return self.top is None
# 示例
l_s = LinkedStack()
l_s.push(3)
l_s.push(5)
print(l_s.peek()) # 输出:5
```
(3)**循环队列(Circular Queue)基本操作演示程序**:
```python
class CircularQueue:
def __init__(self, size):
self.queue = [None] * size
self.front = self.rear = -1
self.size = size
# 入队
def enqueue(self, item):
if self.is_full():
print("队列已满,无法入队")
else:
if self.rear + 1 == self.size:
self.rear = 0
else:
self.rear += 1
self.queue[self.rear] = item
# 出队
def dequeue(self):
if self.is_empty():
print("队列为空,无法出队")
else:
if self.front == self.rear:
front_new = (self.front + 1) % self.size
if front_new == self.rear:
return None # 队列只剩下一个元素
else:
self.front = front_new
else:
self.front += 1
return self.queue[self.front]
# 查看队首元素
def peek(self):
if self.is_empty():
print("队列为空,无元素可见")
else:
return self.queue[self.front]
# 判断队列是否为空
def is_empty(self):
return self.front == -1 or self.front >= self.rear
# 示例
cq = CircularQueue(5)
cq.enqueue(1)
cq.enqueue(2)
print(cq.peek()) # 输出:1
```
(4)**链队列(Linked Queue)基本操作演示程序**:
```python
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class LinkedListQueue:
def __init__(self):
self.front = self.rear = None
# 入队
def enqueue(self, item):
new_node = Node(item)
if not self.is_empty():
self.rear.next = new_node
else:
self.front = self.rear = new_node
self.rear = new_node
# 出队
def dequeue(self):
if self.is_empty():
print("队列为空,无法出队")
else:
removed_item = self.front.data
self.front = self.front.next
if self.front is None:
self.rear = None
return removed_item
# 查看队首元素
def peek(self):
if self.is_empty():
print("队列为空,无元素可见")
else:
return self.front.data
# 判断队列是否为空
def is_empty(self):
return self.front is None
# 示例
l_q = LinkedListQueue()
l_q.enqueue(3)
l_q.enqueue(5)
print(l_q.peek()) # 输出:3
```
阅读全文