环形队列的各种基本运算
时间: 2023-12-09 10:04:22 浏览: 102
java数组实现队列及环形队列实现过程解析
以下是环形队列的各种基本运算:
1.初始化队列
```python
def __init__(self, maxsize=None):
self.maxsize = maxsize
self._items = [None] * maxsize
self.front = 0
self.rear = 0
self.tag = 0
```
2.判断队列是否为空
```python
def is_empty(self):
return self.front == self.rear and not self.tag
```
3.判断队列是否已满
```python
def is_full(self):
return self.front == self.rear and self.tag
```
4.入队操作
```python
def enqueue(self, item):
if self.is_full():
raise Exception('Queue is full')
self._items[self.rear] = item
self.rear = (self.rear + 1) % self.maxsize
self.tag = 1
```
5.出队操作
```python
def dequeue(self):
if self.is_empty():
raise Exception('Queue is empty')
item = self._items[self.front]
self.front = (self.front + 1) % self.maxsize
self.tag = 0
return item
```
阅读全文