mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-07 15:01:58 +08:00
Add python code of chapter queue to docs.
This commit is contained in:
@ -36,9 +36,10 @@ class MyList:
|
||||
assert index < self.__size, "索引越界"
|
||||
self.__nums[index] = num
|
||||
|
||||
""" 中间插入元素 """
|
||||
""" 中间插入(尾部添加)元素 """
|
||||
def add(self, num, index=-1):
|
||||
assert index < self.__size, "索引越界"
|
||||
# 若不指定索引 index ,则向数组尾部添加元素
|
||||
if index == -1:
|
||||
index = self.__size
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
|
@ -13,9 +13,9 @@ from include import *
|
||||
""" 基于环形数组实现的队列 """
|
||||
class ArrayQueue:
|
||||
def __init__(self, size):
|
||||
self.__nums = [None] * size # 用于存储队列元素的数组
|
||||
self.__front = 0 # 头指针,指向队首
|
||||
self.__rear = 0 # 尾指针,指向队尾 + 1
|
||||
self.__nums = [0] * size # 用于存储队列元素的数组
|
||||
self.__front = 0 # 头指针,指向队首
|
||||
self.__rear = 0 # 尾指针,指向队尾 + 1
|
||||
|
||||
""" 获取队列的容量 """
|
||||
def capacity(self):
|
||||
@ -31,7 +31,7 @@ class ArrayQueue:
|
||||
return (self.__rear - self.__front) == 0
|
||||
|
||||
""" 入队 """
|
||||
def put(self, val):
|
||||
def push(self, val):
|
||||
if self.size() == self.capacity():
|
||||
print("队列已满")
|
||||
return False
|
||||
@ -41,13 +41,10 @@ class ArrayQueue:
|
||||
self.__rear = (self.__rear + 1) % self.capacity()
|
||||
|
||||
""" 出队 """
|
||||
def get(self):
|
||||
def poll(self):
|
||||
# 删除头结点
|
||||
if self.is_empty():
|
||||
print("队列为空")
|
||||
return False
|
||||
num = self.__nums[self.__front]
|
||||
# 队头指针向后移动,越过尾部后返回到数组头部
|
||||
num = self.peek()
|
||||
# 队头指针向后移动一位,若越过尾部则返回到数组头部
|
||||
self.__front = (self.__front + 1) % self.capacity()
|
||||
return num
|
||||
|
||||
@ -60,7 +57,7 @@ class ArrayQueue:
|
||||
return self.__nums[self.__front]
|
||||
|
||||
""" 访问指定位置元素 """
|
||||
def get_index(self, index):
|
||||
def get(self, index):
|
||||
if index >= self.size():
|
||||
print("索引越界")
|
||||
return False
|
||||
@ -68,7 +65,7 @@ class ArrayQueue:
|
||||
|
||||
""" 返回列表用于打印 """
|
||||
def to_list(self):
|
||||
res = [None] * self.size()
|
||||
res = [0] * self.size()
|
||||
j = self.__front
|
||||
for i in range(self.size()):
|
||||
res[i] = self.__nums[(j % self.capacity())]
|
||||
@ -76,35 +73,36 @@ class ArrayQueue:
|
||||
return res
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
if __name__ == "__main__":
|
||||
""" 初始化队列 """
|
||||
queue = ArrayQueue(10)
|
||||
|
||||
""" 元素入队 """
|
||||
queue.put(1)
|
||||
queue.put(3)
|
||||
queue.put(2)
|
||||
queue.put(5)
|
||||
queue.put(4)
|
||||
print("队列 queue = ", queue.to_list())
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
print("队列 queue =", queue.to_list())
|
||||
|
||||
""" 访问队首元素 """
|
||||
peek = queue.peek()
|
||||
print("队首元素 peek = ", peek)
|
||||
print("队首元素 peek =", peek)
|
||||
|
||||
""" 访问索引 index 处元素 """
|
||||
num = queue.get_index(3)
|
||||
num = queue.get(3)
|
||||
print("队列索引 3 处的元素为 num =", num)
|
||||
|
||||
""" 元素出队 """
|
||||
get = queue.get()
|
||||
print("出队元素 get = ", get)
|
||||
print("出队后 queue = ", queue.to_list())
|
||||
poll = queue.poll()
|
||||
print("出队元素 poll =", poll)
|
||||
print("出队后 queue =", queue.to_list())
|
||||
|
||||
""" 获取队列的长度 """
|
||||
size = queue.size()
|
||||
print("队列长度 size = ", size)
|
||||
print("队列长度 size =", size)
|
||||
|
||||
""" 判断队列是否为空 """
|
||||
is_empty = queue.is_empty()
|
||||
print("队列是否为空 = ", is_empty)
|
||||
print("队列是否为空 =", is_empty)
|
||||
|
@ -12,36 +12,38 @@ from include import *
|
||||
|
||||
from collections import deque
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
if __name__ == "__main__":
|
||||
""" 初始化双向队列 """
|
||||
duque = deque()
|
||||
|
||||
""" 元素入队 """
|
||||
duque.append(2) # 添加至队尾
|
||||
duque.append(2) # 添加至队尾
|
||||
duque.append(5)
|
||||
duque.append(4)
|
||||
duque.appendleft(3) # 添加至队首
|
||||
duque.appendleft(1)
|
||||
print("双向队列 duque = ", duque)
|
||||
print("双向队列 duque =", duque)
|
||||
|
||||
""" 访问队首元素 """
|
||||
peekFirst = duque[0] # 队首元素
|
||||
print("队首元素 peekFirst = ", peekFirst)
|
||||
peekLast = duque[-1] # 队尾元素
|
||||
print("队尾元素 peekLast = ", peekLast)
|
||||
front = duque[0] # 队首元素
|
||||
print("队首元素 front =", front)
|
||||
rear = duque[-1] # 队尾元素
|
||||
print("队尾元素 rear =", rear)
|
||||
|
||||
""" 元素出队 """
|
||||
popFirst = duque.pop() # 队首元素出队
|
||||
print("队首出队元素 popFirst= ", popFirst)
|
||||
print("队首出队后 duque = ", duque)
|
||||
popLast = duque.popleft() # 队尾元素出队
|
||||
print("队尾出队元素 popLast= ", popLast)
|
||||
print("队尾出队后 duque = ", duque)
|
||||
pop_front = duque.popleft() # 队首元素出队
|
||||
print("队首出队元素 pop_front =", pop_front)
|
||||
print("队首出队后 duque =", duque)
|
||||
pop_rear = duque.pop() # 队尾元素出队
|
||||
print("队尾出队元素 pop_rear =", pop_rear)
|
||||
print("队尾出队后 duque =", duque)
|
||||
|
||||
""" 获取双向队列的长度 """
|
||||
size = len(duque)
|
||||
print("双向队列长度 size = ", size)
|
||||
print("双向队列长度 size =", size)
|
||||
|
||||
""" 判断双向队列是否为空 """
|
||||
is_empty = len(duque) == 0
|
||||
print("双向队列是否为空 = ", is_empty)
|
||||
print("双向队列是否为空 =", is_empty)
|
||||
|
@ -13,8 +13,8 @@ from include import *
|
||||
""" 基于链表实现的队列 """
|
||||
class LinkedListQueue:
|
||||
def __init__(self):
|
||||
self.__front = 0 # 头结点 front
|
||||
self.__rear = 0 # 尾结点 rear
|
||||
self.__front = None # 头结点 front
|
||||
self.__rear = None # 尾结点 rear
|
||||
self.__size = 0
|
||||
|
||||
""" 获取队列的长度 """
|
||||
@ -26,7 +26,7 @@ class LinkedListQueue:
|
||||
return not self.__front
|
||||
|
||||
""" 入队 """
|
||||
def put(self, num):
|
||||
def push(self, num):
|
||||
# 尾结点后添加 num
|
||||
node = ListNode(num)
|
||||
# 如果队列为空,则令头、尾结点都指向该结点
|
||||
@ -40,7 +40,7 @@ class LinkedListQueue:
|
||||
self.__size += 1
|
||||
|
||||
""" 出队 """
|
||||
def get(self):
|
||||
def poll(self):
|
||||
num = self.peek()
|
||||
# 删除头结点
|
||||
self.__front = self.__front.next
|
||||
@ -64,31 +64,32 @@ class LinkedListQueue:
|
||||
return queue
|
||||
|
||||
|
||||
""" Driver Code """
|
||||
if __name__ == "__main__":
|
||||
""" 初始化队列 """
|
||||
queue = LinkedListQueue()
|
||||
|
||||
""" 元素入队 """
|
||||
queue.put(1)
|
||||
queue.put(3)
|
||||
queue.put(2)
|
||||
queue.put(5)
|
||||
queue.put(4)
|
||||
print("队列 queue = ", queue.to_list())
|
||||
queue.push(1)
|
||||
queue.push(3)
|
||||
queue.push(2)
|
||||
queue.push(5)
|
||||
queue.push(4)
|
||||
print("队列 queue =", queue.to_list())
|
||||
|
||||
""" 访问队首元素 """
|
||||
peek = queue.peek()
|
||||
print("队首元素 front = ", peek)
|
||||
print("队首元素 front =", peek)
|
||||
|
||||
""" 元素出队 """
|
||||
get = queue.get()
|
||||
print("出队元素 get = ", get)
|
||||
print("出队后 queue = ", queue.to_list())
|
||||
pop_front = queue.poll()
|
||||
print("出队元素 poll =", pop_front)
|
||||
print("出队后 queue =", queue.to_list())
|
||||
|
||||
""" 获取队列的长度 """
|
||||
size = queue.size()
|
||||
print("队列长度 size = ", size)
|
||||
print("队列长度 size =", size)
|
||||
|
||||
""" 判断队列是否为空 """
|
||||
is_empty = queue.is_empty()
|
||||
print("队列是否为空 = ", is_empty)
|
||||
print("队列是否为空 =", is_empty)
|
||||
|
@ -1,5 +1,5 @@
|
||||
'''
|
||||
File: queue.py
|
||||
File: que.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
@ -10,33 +10,35 @@ import sys
|
||||
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
|
||||
from include import *
|
||||
|
||||
import queue
|
||||
|
||||
if __name__ == "__main__":
|
||||
""" Driver Code """
|
||||
if __name__ == "__main__":
|
||||
""" 初始化队列 """
|
||||
queue = queue.Queue()
|
||||
# 在 Python 中,我们一般将双向队列类 deque 看左队列使用
|
||||
# 虽然 queue.Queue() 是纯正的队列类,但不太好用,因此不建议
|
||||
que = collections.deque()
|
||||
|
||||
""" 元素入队 """
|
||||
queue.put(1)
|
||||
queue.put(3)
|
||||
queue.put(2)
|
||||
queue.put(5)
|
||||
queue.put(4)
|
||||
print("队列 queue = ", queue.queue)
|
||||
que.append(1)
|
||||
que.append(3)
|
||||
que.append(2)
|
||||
que.append(5)
|
||||
que.append(4)
|
||||
print("队列 que =", que)
|
||||
|
||||
""" 访问队首元素 """
|
||||
peek = queue.queue[0]
|
||||
print("队首元素 peek = ", peek)
|
||||
front = que[0];
|
||||
print("队首元素 front =", front);
|
||||
|
||||
""" 元素出队 """
|
||||
get = queue.get()
|
||||
print("出队元素 get = ", get)
|
||||
print("出队后 queue = ", queue.queue)
|
||||
pop = que.popleft()
|
||||
print("出队元素 pop =", pop)
|
||||
print("出队后 que =", que)
|
||||
|
||||
""" 获取队列的长度 """
|
||||
size = queue.qsize()
|
||||
print("队列长度 size = ", size)
|
||||
size = len(que)
|
||||
print("队列长度 size =", size)
|
||||
|
||||
""" 判断队列是否为空 """
|
||||
is_empty = queue.empty()
|
||||
print("队列是否为空 = ", is_empty)
|
||||
is_empty = len(que) == 0
|
||||
print("队列是否为空 =", is_empty)
|
||||
|
@ -1,5 +1,6 @@
|
||||
import copy
|
||||
import math
|
||||
import queue
|
||||
import random
|
||||
import functools
|
||||
import collections
|
||||
|
@ -4,6 +4,8 @@ Created Time: 2021-12-11
|
||||
Author: Krahets (krahets@163.com)
|
||||
'''
|
||||
|
||||
import copy
|
||||
import queue
|
||||
from .binary_tree import TreeNode, tree_to_list
|
||||
from .linked_list import ListNode, linked_list_to_list
|
||||
|
||||
@ -28,7 +30,6 @@ def print_linked_list(head):
|
||||
arr = linked_list_to_list(head)
|
||||
print(' -> '.join([str(a) for a in arr]))
|
||||
|
||||
|
||||
class Trunk:
|
||||
def __init__(self, prev=None, str=None):
|
||||
self.prev = prev
|
||||
|
Reference in New Issue
Block a user