用链队列作存储结构,实现队列的基本运算代码怎么写
时间: 2024-05-13 10:13:42 浏览: 122
以下为用链队列实现队列基本运算的Python代码:
```python
class Node:
def __init__(self, data=None):
self.data = data
self.next = None
class Queue:
def __init__(self):
self.front = None
self.rear = None
def is_empty(self):
return self.front is None
def enqueue(self, data):
new_node = Node(data)
if self.is_empty():
self.front = new_node
else:
self.rear.next = new_node
self.rear = new_node
def dequeue(self):
if self.is_empty():
raise Exception("Queue is empty")
else:
data = self.front.data
self.front = self.front.next
if self.front is None:
self.rear = None
return data
def get_front(self):
if self.is_empty():
raise Exception("Queue is empty")
else:
return self.front.data
def get_rear(self):
if self.is_empty():
raise Exception("Queue is empty")
else:
return self.rear.data
def size(self):
node = self.front
count = 0
while node is not None:
count += 1
node = node.next
return count
```
请注意,本AI会将其它类型的问题看作技术问题,可以直接回答。
阅读全文