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

99 lines
2.6 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)
"""
2023-04-09 05:05:35 +08:00
2022-11-30 12:15:50 +08:00
class ArrayQueue:
2023-04-09 05:05:35 +08:00
"""基于环形数组实现的队列"""
2023-07-24 22:34:05 +08:00
def __init__(self, size: int):
2023-04-09 05:05:35 +08:00
"""构造方法"""
self.__nums: list[int] = [0] * size # 用于存储队列元素的数组
2023-04-09 05:05:35 +08:00
self.__front: int = 0 # 队首指针,指向队首元素
self.__size: int = 0 # 队列长度
2022-11-30 12:15:50 +08:00
def capacity(self) -> int:
2023-04-09 05:05:35 +08:00
"""获取队列的容量"""
2022-12-01 06:53:08 +08:00
return len(self.__nums)
2022-11-30 12:15:50 +08:00
def size(self) -> int:
2023-04-09 05:05:35 +08:00
"""获取队列的长度"""
2023-02-01 03:23:29 +08:00
return self.__size
2022-11-30 12:15:50 +08:00
def is_empty(self) -> bool:
2023-04-09 05:05:35 +08:00
"""判断队列是否为空"""
2023-02-01 03:23:29 +08:00
return self.__size == 0
2022-11-30 12:15:50 +08:00
2023-07-24 22:34:05 +08:00
def push(self, num: int):
2023-04-09 05:05:35 +08:00
"""入队"""
if self.__size == self.capacity():
raise IndexError("队列已满")
2023-02-01 03:23:29 +08:00
# 计算尾指针,指向队尾索引 + 1
2023-07-31 03:27:26 +08:00
# 通过取余操作,实现 rear 越过数组尾部后回到头部
rear: int = (self.__front + self.__size) % self.capacity()
# 将 num 添加至队尾
2023-02-01 03:23:29 +08:00
self.__nums[rear] = num
self.__size += 1
2022-11-30 12:15:50 +08:00
def pop(self) -> int:
2023-04-09 05:05:35 +08:00
"""出队"""
num: int = self.peek()
2023-02-01 03:23:29 +08:00
# 队首指针向后移动一位,若越过尾部则返回到数组头部
2022-12-01 06:53:08 +08:00
self.__front = (self.__front + 1) % self.capacity()
2023-02-01 03:23:29 +08:00
self.__size -= 1
2022-11-30 12:15:50 +08:00
return num
def peek(self) -> int:
2023-04-09 05:05:35 +08:00
"""访问队首元素"""
if self.is_empty():
raise IndexError("队列为空")
2022-12-01 06:53:08 +08:00
return self.__nums[self.__front]
2022-11-30 12:15:50 +08:00
def to_list(self) -> list[int]:
2023-04-09 05:05:35 +08:00
"""返回列表用于打印"""
res = [0] * self.size()
j: int = self.__front
2022-12-01 06:53:08 +08:00
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__":
2023-04-09 05:05:35 +08:00
# 初始化队列
2022-12-01 06:53:08 +08:00
queue = ArrayQueue(10)
2022-11-30 12:15:50 +08:00
2023-04-09 05:05:35 +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
2023-04-09 05:05:35 +08:00
# 访问队首元素
peek: int = queue.peek()
print("队首元素 peek =", peek)
2022-11-30 12:15:50 +08:00
2023-04-09 05:05:35 +08:00
# 元素出队
pop: int = queue.pop()
print("出队元素 pop =", pop)
print("出队后 queue =", queue.to_list())
2022-11-30 12:15:50 +08:00
2023-04-09 05:05:35 +08:00
# 获取队列的长度
size: int = queue.size()
print("队列长度 size =", size)
2022-11-30 12:15:50 +08:00
2023-04-09 05:05:35 +08:00
# 判断队列是否为空
is_empty: bool = queue.is_empty()
print("队列是否为空 =", is_empty)
2023-02-01 03:23:29 +08:00
2023-04-09 05:05:35 +08:00
# 测试环形数组
2023-02-01 03:23:29 +08:00
for i in range(10):
queue.push(i)
queue.pop()
print("", i, "轮入队 + 出队后 queue = ", queue.to_list())