From a2b7586c986733f81c6549b9bff7c3fbb98dd267 Mon Sep 17 00:00:00 2001 From: pengchzn Date: Wed, 30 Nov 2022 12:15:50 +0800 Subject: [PATCH 1/2] Add python code for queue --- .../chapter_stack_and_queue/array_queue.py | 91 ++++++++++++++++++- codes/python/chapter_stack_and_queue/deque.py | 43 ++++++++- .../linkedlist_queue.py | 88 +++++++++++++++++- codes/python/chapter_stack_and_queue/queue.py | 38 +++++++- 4 files changed, 248 insertions(+), 12 deletions(-) diff --git a/codes/python/chapter_stack_and_queue/array_queue.py b/codes/python/chapter_stack_and_queue/array_queue.py index 7bc6a32..0e173fe 100644 --- a/codes/python/chapter_stack_and_queue/array_queue.py +++ b/codes/python/chapter_stack_and_queue/array_queue.py @@ -1,10 +1,95 @@ ''' File: array_queue.py -Created Time: 2022-11-25 -Author: Krahets (krahets@163.com) +Created Time: 2022-11-29 +Author: Peng Chen (pengchzn@gmail.com) ''' -import sys, os.path as osp +import os.path as osp +import sys + sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * +""" 基于环形数组实现的队列 """ +class ArrayQueue: + def __init__(self, size): + self.nums = [None] * size # 用于存储队列元素的数组 + self.front = 0 # 头指针,指向队首 + self.rear = 0 # 尾指针,指向队尾 + 1 + + """ 获取队列的容量 """ + def capacity(self): + return len(self.nums) + + """ 获取队列的长度 """ + def size(self): + # 由于将数组看作为环形,可能 rear < front ,因此需要取余数 + return (self.capacity() + self.rear - self.front) % self.capacity() + + """ 判断队列是否为空 """ + def is_empty(self): + return len(self.nums)==0 + + """ 入队 """ + def put(self, val): + if self.size() == self.capacity(): + print("队列已满") + return False + # 尾结点后添加 num + self.nums[self.rear] = val + # 尾指针向后移动一位,越过尾部后返回到数组头部 + self.rear = (self.rear + 1) % self.capacity() + + """ 出队 """ + def get(self): + # 删除头结点 + if self.is_empty(): + print("队列为空") + return False + num = self.nums[self.front] + self.nums[self.front] = None + # 队头指针向后移动,越过尾部后返回到数组头部 + self.front = (self.front + 1) % self.capacity() + return num + + """ 访问队首元素 """ + def peek(self): + # 删除头结点 + if self.is_empty(): + return 1 + return self.nums[self.front] + + """ 访问指定位置元素 """ + def get_index(self, index): + if index >= self.size(): + return -1 + return self.nums[(self.front + index) % self.capacity()] + +if __name__ == "__main__": + """ 初始化队列 """ + queue = ArrayQueue(5) + + """ 元素入队 """ + queue.put(1) + queue.put(3) + queue.put(2) + queue.put(5) + queue.put(4) + print("队列 queue = ", queue.nums) + + """ 访问队首元素 """ + peek = queue.peek() + print("队首元素 peek = ", peek) + + """ 元素出队 """ + get = queue.get() + print("出队元素 get = ", get) + print("出队后 queue = ", queue.nums) + + """ 获取队列的长度 """ + size = queue.size() + print("队列长度 size = ", size) + + """ 判断队列是否为空 """ + is_empty = queue.is_empty() + print("队列是否为空 = ", is_empty) diff --git a/codes/python/chapter_stack_and_queue/deque.py b/codes/python/chapter_stack_and_queue/deque.py index 9894975..0bddd0d 100644 --- a/codes/python/chapter_stack_and_queue/deque.py +++ b/codes/python/chapter_stack_and_queue/deque.py @@ -1,10 +1,47 @@ ''' File: deque.py -Created Time: 2022-11-25 -Author: Krahets (krahets@163.com) +Created Time: 2022-11-29 +Author: Peng Chen (pengchzn@gmail.com) ''' -import sys, os.path as osp +import os.path as osp +import sys + sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * +from collections import deque + +if __name__ == "__main__": + """ 初始化双向队列 """ + duque = deque() + + """ 元素入队 """ + duque.append(2) # 添加至队尾 + duque.append(5) + duque.append(4) + duque.appendleft(3) # 添加至队首 + duque.appendleft(1) + print("双向队列 duque = ", duque) + + """ 访问队首元素 """ + peekFirst = duque[0] # 队首元素 + print("队首元素 peekFirst = ", peekFirst) + peekLast = duque[-1] # 队尾元素 + print("队尾元素 peekLast = ", peekLast) + + """ 元素出队 """ + popFirst = duque.pop() # 队首元素出队 + print("队首出队元素 popFirst= ", popFirst) + print("队首出队后 duque = ", duque) + popLast = duque.popleft() # 队尾元素出队 + print("队尾出队元素 popLast= ", popLast) + print("队尾出队后 duque = ", duque) + + """ 获取队列的长度 """ + size = len(duque) + print("队列长度 size = ", size) + + """ 判断队列是否为空 """ + is_empty = len(duque) == 0 + print("队列是否为空 = ", is_empty) diff --git a/codes/python/chapter_stack_and_queue/linkedlist_queue.py b/codes/python/chapter_stack_and_queue/linkedlist_queue.py index 3606c8a..53f3b9a 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_queue.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_queue.py @@ -1,10 +1,92 @@ ''' File: linkedlist_queue.py -Created Time: 2022-11-25 -Author: Krahets (krahets@163.com) +Created Time: 2022-11-29 +Author: Peng Chen (pengchzn@gmail.com) ''' -import sys, os.path as osp +import os.path as osp +import sys + sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * +""" 基于链表实现的队列 """ +class LinkedListQueue: + def __init__(self): + self.__front = 0 # 头结点 front + self.__rear = 0 # 尾结点 rear + self.__size = 0 + + """ 获取队列的长度 """ + def size(self): + return self.__size + + """ 判断队列是否为空 """ + def is_empty(self): + return not self.__front + + """ 入队 """ + def put(self, num): + # 尾结点后添加 num + node = ListNode(num) + # 如果队列为空,则令头、尾结点都指向该结点 + if self.__front == 0: + self.__front = node + self.__rear = node + # 如果队列不为空,则将该结点添加到尾结点后 + else: + self.__rear.next = node + self.__rear = node + self.__size += 1 + + """ 出队 """ + def get(self): + num = self.peek() + # 删除头结点 + self.__front = self.__front.next + self.__size -= 1 + return num + + """ 访问队首元素 """ + def peek(self): + if self.size() == 0: + return -1 + return self.__front.val + + def to_array(self): + queue = [] + temp = self.__front + while temp: + queue.append(temp.val) + temp = temp.next + return queue + + +if __name__ == "__main__": + """ 初始化队列 """ + queue = LinkedListQueue() + + """ 元素入队 """ + queue.put(1) + queue.put(3) + queue.put(2) + queue.put(5) + queue.put(4) + print("队列 queue = ", queue.to_array()) + + """ 访问队首元素 """ + peek = queue.peek() + print("队首元素 front = ", peek) + + """ 元素出队 """ + get = queue.get() + print("出队元素 get = ", get) + print("出队后 queue = ", queue.to_array()) + + """ 获取队列的长度 """ + size = queue.size() + print("队列长度 size = ", size) + + """ 判断队列是否为空 """ + is_empty = queue.is_empty() + print("队列是否为空 = ", is_empty) diff --git a/codes/python/chapter_stack_and_queue/queue.py b/codes/python/chapter_stack_and_queue/queue.py index 52855b2..3f39d2d 100644 --- a/codes/python/chapter_stack_and_queue/queue.py +++ b/codes/python/chapter_stack_and_queue/queue.py @@ -1,10 +1,42 @@ ''' File: queue.py -Created Time: 2022-11-25 -Author: Krahets (krahets@163.com) +Created Time: 2022-11-29 +Author: Peng Chen (pengchzn@gmail.com) ''' -import sys, os.path as osp +import os.path as osp +import sys + sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * +import queue + +if __name__ == "__main__": + """ 初始化队列 """ + queue = queue.Queue() + + """ 元素入队 """ + queue.put(1) + queue.put(3) + queue.put(2) + queue.put(5) + queue.put(4) + print("队列 queue = ", queue.queue) + + """ 访问队首元素 """ + peek = queue.queue[0] + print("队首元素 peek = ", peek) + + """ 元素出队 """ + get = queue.get() + print("出队元素 get = ", get) + print("出队后 queue = ", queue.queue) + + """ 获取队列的长度 """ + size = queue.qsize() + print("队列长度 size = ", size) + + """ 判断队列是否为空 """ + is_empty = queue.empty() + print("队列是否为空 = ", is_empty) From 59e46ada826ec0588e59f386c0a13da2fa741d7f Mon Sep 17 00:00:00 2001 From: pengchzn Date: Thu, 1 Dec 2022 06:53:08 +0800 Subject: [PATCH 2/2] Add python code for queue --- .../chapter_stack_and_queue/array_queue.py | 53 ++++++++++++------- codes/python/chapter_stack_and_queue/deque.py | 8 +-- .../linkedlist_queue.py | 12 +++-- 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/codes/python/chapter_stack_and_queue/array_queue.py b/codes/python/chapter_stack_and_queue/array_queue.py index 0e173fe..c4284d8 100644 --- a/codes/python/chapter_stack_and_queue/array_queue.py +++ b/codes/python/chapter_stack_and_queue/array_queue.py @@ -1,6 +1,6 @@ ''' File: array_queue.py -Created Time: 2022-11-29 +Created Time: 2022-12-01 Author: Peng Chen (pengchzn@gmail.com) ''' @@ -13,22 +13,22 @@ from include import * """ 基于环形数组实现的队列 """ class ArrayQueue: def __init__(self, size): - self.nums = [None] * size # 用于存储队列元素的数组 - self.front = 0 # 头指针,指向队首 - self.rear = 0 # 尾指针,指向队尾 + 1 + self.__nums = [None] * size # 用于存储队列元素的数组 + self.__front = 0 # 头指针,指向队首 + self.__rear = 0 # 尾指针,指向队尾 + 1 """ 获取队列的容量 """ def capacity(self): - return len(self.nums) + return len(self.__nums) """ 获取队列的长度 """ def size(self): # 由于将数组看作为环形,可能 rear < front ,因此需要取余数 - return (self.capacity() + self.rear - self.front) % self.capacity() + return (self.capacity() + self.__rear - self.__front) % self.capacity() """ 判断队列是否为空 """ def is_empty(self): - return len(self.nums)==0 + return (self.__rear - self.__front) == 0 """ 入队 """ def put(self, val): @@ -36,9 +36,9 @@ class ArrayQueue: print("队列已满") return False # 尾结点后添加 num - self.nums[self.rear] = val + self.__nums[self.__rear] = val # 尾指针向后移动一位,越过尾部后返回到数组头部 - self.rear = (self.rear + 1) % self.capacity() + self.__rear = (self.__rear + 1) % self.capacity() """ 出队 """ def get(self): @@ -46,28 +46,39 @@ class ArrayQueue: if self.is_empty(): print("队列为空") return False - num = self.nums[self.front] - self.nums[self.front] = None + num = self.__nums[self.__front] # 队头指针向后移动,越过尾部后返回到数组头部 - self.front = (self.front + 1) % self.capacity() + self.__front = (self.__front + 1) % self.capacity() return num """ 访问队首元素 """ def peek(self): # 删除头结点 if self.is_empty(): - return 1 - return self.nums[self.front] + print("队列为空") + return False + return self.__nums[self.__front] """ 访问指定位置元素 """ def get_index(self, index): if index >= self.size(): - return -1 - return self.nums[(self.front + index) % self.capacity()] + print("索引越界") + return False + return self.__nums[(self.__front + index) % self.capacity()] + + """ 返回列表用于打印 """ + def to_list(self): + res = [None] * self.size() + j = self.__front + for i in range(self.size()): + res[i] = self.__nums[(j % self.capacity())] + j += 1 + return res + if __name__ == "__main__": """ 初始化队列 """ - queue = ArrayQueue(5) + queue = ArrayQueue(10) """ 元素入队 """ queue.put(1) @@ -75,16 +86,20 @@ if __name__ == "__main__": queue.put(2) queue.put(5) queue.put(4) - print("队列 queue = ", queue.nums) + print("队列 queue = ", queue.to_list()) """ 访问队首元素 """ peek = queue.peek() print("队首元素 peek = ", peek) + """ 访问索引 index 处元素 """ + num = queue.get_index(3) + print("队列索引 3 处的元素为 num =", num) + """ 元素出队 """ get = queue.get() print("出队元素 get = ", get) - print("出队后 queue = ", queue.nums) + print("出队后 queue = ", queue.to_list()) """ 获取队列的长度 """ size = queue.size() diff --git a/codes/python/chapter_stack_and_queue/deque.py b/codes/python/chapter_stack_and_queue/deque.py index 0bddd0d..a2929df 100644 --- a/codes/python/chapter_stack_and_queue/deque.py +++ b/codes/python/chapter_stack_and_queue/deque.py @@ -38,10 +38,10 @@ if __name__ == "__main__": print("队尾出队元素 popLast= ", popLast) print("队尾出队后 duque = ", duque) - """ 获取队列的长度 """ + """ 获取双向队列的长度 """ size = len(duque) - print("队列长度 size = ", size) + print("双向队列长度 size = ", size) - """ 判断队列是否为空 """ + """ 判断双向队列是否为空 """ is_empty = len(duque) == 0 - print("队列是否为空 = ", is_empty) + print("双向队列是否为空 = ", is_empty) diff --git a/codes/python/chapter_stack_and_queue/linkedlist_queue.py b/codes/python/chapter_stack_and_queue/linkedlist_queue.py index 53f3b9a..d5a2202 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_queue.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_queue.py @@ -1,6 +1,6 @@ ''' File: linkedlist_queue.py -Created Time: 2022-11-29 +Created Time: 2022-12-01 Author: Peng Chen (pengchzn@gmail.com) ''' @@ -50,10 +50,12 @@ class LinkedListQueue: """ 访问队首元素 """ def peek(self): if self.size() == 0: - return -1 + print("队列为空") + return False return self.__front.val - def to_array(self): + """ 转化为列表用于打印 """ + def to_list(self): queue = [] temp = self.__front while temp: @@ -72,7 +74,7 @@ if __name__ == "__main__": queue.put(2) queue.put(5) queue.put(4) - print("队列 queue = ", queue.to_array()) + print("队列 queue = ", queue.to_list()) """ 访问队首元素 """ peek = queue.peek() @@ -81,7 +83,7 @@ if __name__ == "__main__": """ 元素出队 """ get = queue.get() print("出队元素 get = ", get) - print("出队后 queue = ", queue.to_array()) + print("出队后 queue = ", queue.to_list()) """ 获取队列的长度 """ size = queue.size()