From b47f54886e609c17518053487272ac6d17ed4c0f Mon Sep 17 00:00:00 2001 From: pengchzn Date: Tue, 29 Nov 2022 13:58:23 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=A0=88=E7=9A=84python=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chapter_stack_and_queue/array_stack.py | 73 +++++++++++++++- .../linkedlist_stack.py | 83 ++++++++++++++++++- codes/python/chapter_stack_and_queue/stack.py | 31 ++++++- 3 files changed, 184 insertions(+), 3 deletions(-) diff --git a/codes/python/chapter_stack_and_queue/array_stack.py b/codes/python/chapter_stack_and_queue/array_stack.py index 0853efa26..0867651f7 100644 --- a/codes/python/chapter_stack_and_queue/array_stack.py +++ b/codes/python/chapter_stack_and_queue/array_stack.py @@ -4,7 +4,78 @@ Created Time: 2022-11-25 Author: Krahets (krahets@163.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 ArrayStack: + def __init__(self): + self._stack = [] + self._size = 0 + + """ 获取栈的长度 """ + + def size(self): + return self._size + + """ 判断栈是否为空 """ + + def isEmpty(self): + return self._stack == [] + + """ 入栈 """ + + def push(self, item): + self._stack.append(item) + self._size += 1 + + """ 出栈 """ + + def pop(self): + pop = self._stack.pop() + self._size -= 1 + return pop + + """ 访问栈顶元素 """ + + def peek(self): + return self._stack[-1] + + """ 访问索引 index 处元素 """ + + def get(self, index): + return self._stack[index] + + +if __name__ == "__main__": + """ 初始化栈 """ + stack = ArrayStack() + + """ 元素入栈 """ + stack.push(1) + stack.push(3) + stack.push(2) + stack.push(5) + stack.push(4) + print("栈 stack = ", stack._stack) + + """ 访问栈顶元素 """ + peek = stack.peek() + print("栈顶元素 peek = ", peek) + + """ 元素出栈 """ + pop = stack.pop() + print("出栈元素 pop = ", pop) + print("出栈后 stack = ", stack._stack) + + """ 获取栈的长度 """ + size = stack.size() + print("栈的长度 size = ", size) + + """ 判断是否为空 """ + isEmpty = (stack == []) diff --git a/codes/python/chapter_stack_and_queue/linkedlist_stack.py b/codes/python/chapter_stack_and_queue/linkedlist_stack.py index 252b2c727..eed307fb0 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_stack.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_stack.py @@ -4,7 +4,88 @@ Created Time: 2022-11-25 Author: Krahets (krahets@163.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 LinkedListStack: + def __init__(self): + self.head = None + + """ 获取栈的长度 """ + + def size(self): + cnt = 0 + temp = self.head + while temp is not None: + temp = temp.next + cnt += 1 + return cnt + + """ 判断栈是否为空 """ + + def is_empty(self): + if not self.head.val and not self.head.next: + return True + return False + + """ 入栈 """ + + def push(self, val): + temp = ListNode(val) + temp.next = self.head + self.head = temp + + """ 出栈 """ + + def pop(self): + pop = self.head.val + self.head = self.head.next + return pop + + """ 访问栈顶元素 """ + + def peek(self): + return self.head.val + + def to_array(self): + stack = [] + temp = self.head + while temp: + stack.insert(0, temp.val) + temp = temp.next + return stack + + +if __name__ == "__main__": + """ 初始化栈 """ + stack = LinkedListStack() + + """ 元素入栈 """ + stack.push(1) + stack.push(3) + stack.push(2) + stack.push(5) + stack.push(4) + print("栈 stack = ", stack.to_array()) + + """ 访问栈顶元素 """ + peek = stack.peek() + print("栈顶元素 peek = ", peek) + + """ 元素出栈 """ + pop = stack.pop() + print("出栈元素 pop = ", pop) + print("出栈后 stack = ", stack.to_array()) + + """ 获取栈的长度 """ + size = stack.size() + print("栈的长度 size = ", size) + + """ 判断是否为空 """ + isEmpty = (stack == []) diff --git a/codes/python/chapter_stack_and_queue/stack.py b/codes/python/chapter_stack_and_queue/stack.py index 334160088..c512198ec 100644 --- a/codes/python/chapter_stack_and_queue/stack.py +++ b/codes/python/chapter_stack_and_queue/stack.py @@ -4,7 +4,36 @@ Created Time: 2022-11-25 Author: Krahets (krahets@163.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 * +if __name__ == "__main__": + """ 初始化栈 """ + stack = [] + + """ 元素入栈 """ + stack.append(1) + stack.append(3) + stack.append(2) + stack.append(5) + stack.append(4) + print("栈 stack = ", stack) + + """ 访问栈顶元素 """ + peek = stack[-1] + print("栈顶元素 peek = ", peek) + + """ 元素出栈 """ + pop = stack.pop() + print("出栈元素 pop = ", pop) + print("出栈后 stack = ", stack) + + """ 获取栈的长度 """ + size = len(stack) + print("栈的长度 size = ", size) + + """ 判断是否为空 """ + isEmpty = (stack == []) From f8d44be73ddd45d28a205c74b0f73375df8f86a7 Mon Sep 17 00:00:00 2001 From: pengchzn Date: Tue, 29 Nov 2022 21:42:59 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=A0=88=E7=9A=84python=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/python/chapter_stack_and_queue/array_stack.py | 12 ++---------- .../chapter_stack_and_queue/linkedlist_stack.py | 11 ++--------- codes/python/chapter_stack_and_queue/stack.py | 4 ++-- 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/codes/python/chapter_stack_and_queue/array_stack.py b/codes/python/chapter_stack_and_queue/array_stack.py index 0867651f7..d9243d512 100644 --- a/codes/python/chapter_stack_and_queue/array_stack.py +++ b/codes/python/chapter_stack_and_queue/array_stack.py @@ -1,7 +1,7 @@ ''' File: array_stack.py -Created Time: 2022-11-25 -Author: Krahets (krahets@163.com) +Created Time: 2022-11-29 +Author: Peng Chen (pengchzn@gmail.com) ''' import os.path as osp @@ -11,43 +11,35 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * """ 基于数组实现的栈 """ - - class ArrayStack: def __init__(self): self._stack = [] self._size = 0 """ 获取栈的长度 """ - def size(self): return self._size """ 判断栈是否为空 """ - def isEmpty(self): return self._stack == [] """ 入栈 """ - def push(self, item): self._stack.append(item) self._size += 1 """ 出栈 """ - def pop(self): pop = self._stack.pop() self._size -= 1 return pop """ 访问栈顶元素 """ - def peek(self): return self._stack[-1] """ 访问索引 index 处元素 """ - def get(self, index): return self._stack[index] diff --git a/codes/python/chapter_stack_and_queue/linkedlist_stack.py b/codes/python/chapter_stack_and_queue/linkedlist_stack.py index eed307fb0..d28f37705 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_stack.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_stack.py @@ -1,7 +1,7 @@ ''' File: linkedlist_stack.py -Created Time: 2022-11-25 -Author: Krahets (krahets@163.com) +Created Time: 2022-11-29 +Author: Peng Chen (pengchzn@gmail.com) ''' import os.path as osp @@ -11,14 +11,11 @@ sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * """ 基于链表实现的栈 """ - - class LinkedListStack: def __init__(self): self.head = None """ 获取栈的长度 """ - def size(self): cnt = 0 temp = self.head @@ -28,28 +25,24 @@ class LinkedListStack: return cnt """ 判断栈是否为空 """ - def is_empty(self): if not self.head.val and not self.head.next: return True return False """ 入栈 """ - def push(self, val): temp = ListNode(val) temp.next = self.head self.head = temp """ 出栈 """ - def pop(self): pop = self.head.val self.head = self.head.next return pop """ 访问栈顶元素 """ - def peek(self): return self.head.val diff --git a/codes/python/chapter_stack_and_queue/stack.py b/codes/python/chapter_stack_and_queue/stack.py index c512198ec..30028d096 100644 --- a/codes/python/chapter_stack_and_queue/stack.py +++ b/codes/python/chapter_stack_and_queue/stack.py @@ -1,7 +1,7 @@ ''' File: stack.py -Created Time: 2022-11-25 -Author: Krahets (krahets@163.com) +Created Time: 2022-11-29 +Author: Peng Chen (pengchzn@gmail.com) ''' import os.path as osp From 60cb2ffc975fbfff0bfa91046ccc9917348281a6 Mon Sep 17 00:00:00 2001 From: pengchzn Date: Tue, 29 Nov 2022 22:58:16 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=A0=88=E7=9A=84python=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/python/chapter_stack_and_queue/array_stack.py | 4 ++-- codes/python/chapter_stack_and_queue/linkedlist_stack.py | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/codes/python/chapter_stack_and_queue/array_stack.py b/codes/python/chapter_stack_and_queue/array_stack.py index d9243d512..58a73556a 100644 --- a/codes/python/chapter_stack_and_queue/array_stack.py +++ b/codes/python/chapter_stack_and_queue/array_stack.py @@ -21,7 +21,7 @@ class ArrayStack: return self._size """ 判断栈是否为空 """ - def isEmpty(self): + def is_empty(self): return self._stack == [] """ 入栈 """ @@ -70,4 +70,4 @@ if __name__ == "__main__": print("栈的长度 size = ", size) """ 判断是否为空 """ - isEmpty = (stack == []) + isEmpty = stack.is_empty() diff --git a/codes/python/chapter_stack_and_queue/linkedlist_stack.py b/codes/python/chapter_stack_and_queue/linkedlist_stack.py index d28f37705..88029474d 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_stack.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_stack.py @@ -50,8 +50,9 @@ class LinkedListStack: stack = [] temp = self.head while temp: - stack.insert(0, temp.val) + stack.append(temp.val) temp = temp.next + stack = stack[::-1] return stack @@ -81,4 +82,4 @@ if __name__ == "__main__": print("栈的长度 size = ", size) """ 判断是否为空 """ - isEmpty = (stack == []) + isEmpty = stack.is_empty() From b1b50944360a8a4c3d0d71571820e7d025ca2881 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 29 Nov 2022 23:11:54 +0800 Subject: [PATCH 4/4] Update linkedlist_stack.py --- codes/python/chapter_stack_and_queue/linkedlist_stack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codes/python/chapter_stack_and_queue/linkedlist_stack.py b/codes/python/chapter_stack_and_queue/linkedlist_stack.py index 88029474d..19ffdc39a 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_stack.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_stack.py @@ -52,7 +52,7 @@ class LinkedListStack: while temp: stack.append(temp.val) temp = temp.next - stack = stack[::-1] + stack.reverse() return stack