定义一个包含学生信息的顺序队列和链队列代码
时间: 2023-05-24 19:05:04 浏览: 112
C语言:定义一个包含学生信息(学号,姓名,成绩)的顺序表和链表.zip
5星 · 资源好评率100%
顺序队列的代码实现:
```python
class SeqQueue:
def __init__(self, capacity):
self.capacity = capacity # 队列的最大容量
self.front = 0 # 队头指针
self.rear = 0 # 队尾指针
self.data = [None] * capacity # 队列数组
def is_empty(self):
return self.front == self.rear
def is_full(self):
return (self.rear + 1) % self.capacity == self.front
def enqueue(self, value):
if self.is_full():
return False
self.data[self.rear] = value # 将元素插入队尾
self.rear = (self.rear + 1) % self.capacity # 更新队尾指针
return True
def dequeue(self):
if self.is_empty():
return None
value = self.data[self.front] # 取出队头元素
self.front = (self.front + 1) % self.capacity # 更新队头指针
return value
```
链队列的代码实现:
```python
class Node:
def __init__(self, value=None, next=None):
self.value = value # 节点的值
self.next = next # 指向下一个节点的指针
class LinkQueue:
def __init__(self):
self.front = self.rear = Node() # 哨兵节点
def is_empty(self):
return self.front == self.rear
def enqueue(self, value):
self.rear.next = Node(value) # 创建新节点
self.rear = self.rear.next # 更新队尾指针
def dequeue(self):
if self.is_empty():
return None
value = self.front.next.value # 取出队头元素
self.front.next = self.front.next.next # 更新队头指针
if self.front.next is None: # 队列为空时,更新队尾指针
self.rear = self.front
return value
```
阅读全文