hello-algo/codes/python/chapter_stack_and_queue/array_queue.py

107 lines
2.9 KiB
Python
Raw Normal View History

'''
File: array_queue.py
2022-12-01 06:53:08 +08:00
Created Time: 2022-12-01
2022-11-30 12:15:50 +08:00
Author: Peng Chen (pengchzn@gmail.com)
'''
2022-11-30 12:15:50 +08:00
import os.path as osp
import sys
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import *
2022-11-30 12:15:50 +08:00
""" 基于环形数组实现的队列 """
class ArrayQueue:
def __init__(self, size):
self.__nums = [0] * size # 用于存储队列元素的数组
self.__front = 0 # 头指针,指向队首
self.__rear = 0 # 尾指针,指向队尾 + 1
2022-11-30 12:15:50 +08:00
""" 获取队列的容量 """
def capacity(self):
2022-12-01 06:53:08 +08:00
return len(self.__nums)
2022-11-30 12:15:50 +08:00
""" 获取队列的长度 """
def size(self):
# 由于将数组看作为环形,可能 rear < front ,因此需要取余数
2022-12-01 06:53:08 +08:00
return (self.capacity() + self.__rear - self.__front) % self.capacity()
2022-11-30 12:15:50 +08:00
""" 判断队列是否为空 """
def is_empty(self):
2022-12-01 06:53:08 +08:00
return (self.__rear - self.__front) == 0
2022-11-30 12:15:50 +08:00
""" 入队 """
def push(self, val):
2022-11-30 12:15:50 +08:00
if self.size() == self.capacity():
print("队列已满")
return False
# 尾结点后添加 num
2022-12-01 06:53:08 +08:00
self.__nums[self.__rear] = val
2022-11-30 12:15:50 +08:00
# 尾指针向后移动一位,越过尾部后返回到数组头部
2022-12-01 06:53:08 +08:00
self.__rear = (self.__rear + 1) % self.capacity()
2022-11-30 12:15:50 +08:00
""" 出队 """
def poll(self):
num = self.peek()
# 队头指针向后移动一位,若越过尾部则返回到数组头部
2022-12-01 06:53:08 +08:00
self.__front = (self.__front + 1) % self.capacity()
2022-11-30 12:15:50 +08:00
return num
""" 访问队首元素 """
def peek(self):
if self.is_empty():
2022-12-01 06:53:08 +08:00
print("队列为空")
return False
return self.__nums[self.__front]
2022-11-30 12:15:50 +08:00
""" 访问指定位置元素 """
def get(self, index):
2022-11-30 12:15:50 +08:00
if index >= self.size():
2022-12-01 06:53:08 +08:00
print("索引越界")
return False
return self.__nums[(self.__front + index) % self.capacity()]
""" 返回列表用于打印 """
def to_list(self):
res = [0] * self.size()
2022-12-01 06:53:08 +08:00
j = self.__front
for i in range(self.size()):
res[i] = self.__nums[(j % self.capacity())]
j += 1
return res
2022-11-30 12:15:50 +08:00
""" Driver Code """
2022-11-30 12:15:50 +08:00
if __name__ == "__main__":
""" 初始化队列 """
2022-12-01 06:53:08 +08:00
queue = ArrayQueue(10)
2022-11-30 12:15:50 +08:00
""" 元素入队 """
queue.push(1)
queue.push(3)
queue.push(2)
queue.push(5)
queue.push(4)
print("队列 queue =", queue.to_list())
2022-11-30 12:15:50 +08:00
""" 访问队首元素 """
peek = queue.peek()
print("队首元素 peek =", peek)
2022-11-30 12:15:50 +08:00
2022-12-01 06:53:08 +08:00
""" 访问索引 index 处元素 """
num = queue.get(3)
2022-12-01 06:53:08 +08:00
print("队列索引 3 处的元素为 num =", num)
2022-11-30 12:15:50 +08:00
""" 元素出队 """
poll = queue.poll()
print("出队元素 poll =", poll)
print("出队后 queue =", queue.to_list())
2022-11-30 12:15:50 +08:00
""" 获取队列的长度 """
size = queue.size()
print("队列长度 size =", size)
2022-11-30 12:15:50 +08:00
""" 判断队列是否为空 """
is_empty = queue.is_empty()
print("队列是否为空 =", is_empty)