Python全系列 教程
3567个小节阅读:5929.4k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
xxxxxxxxxx
class ListNode:
def __init__(self, x:int):
self.val = x
self.next = None
class LinkedQueue:
def __init__(self):
self.head = None # 队头指针
self.tail = None # 队尾指针
self.size = 0 # 队列长度
def is_empty(self) -> bool:
return self.size == 0
def push(self,val:int) -> None:
'''
入队
'''
# 创建节点
node = ListNode(val)
# 判断队列是否为空
if self.head is None:
self.head = node
self.tail = node
else:
# 队尾后增加节点
self.tail.next = node
# 队尾指针后移
self.tail = node
# 队列长度+1
self.size += 1
def peek(self) -> int:
'''
查看队头元素
'''
if self.head is None:
return None
else:
return self.head.val
def pop(self) -> int:
'''
出队
'''
# 获取队头元素
data = self.peek()
# 更新队头指针
self.head = self.head.next
# 队的数量减1
self.size -= 1
return data
def to_list(self):
if self.is_empty():
return []
else:
rs = []
temp = self.head
while temp:
rs.append(temp.val)
temp = temp.next
return rs
if __name__ =='__main__':
queue = LinkedQueue()
queue.push(1)
queue.push(2)
queue.push(3)
print(queue.to_list())
print(queue.peek())
print(queue.pop())
print(queue.to_list())