From 53cc651af2ac03f69f7b68d21fbecbbda747cf85 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 29 Nov 2022 23:35:51 +0800 Subject: [PATCH] Update python code of stack. --- .../chapter_stack_and_queue/array_stack.py | 37 ++++++----- .../linkedlist_stack.py | 64 +++++++++---------- codes/python/chapter_stack_and_queue/stack.py | 19 +++--- 3 files changed, 60 insertions(+), 60 deletions(-) diff --git a/codes/python/chapter_stack_and_queue/array_stack.py b/codes/python/chapter_stack_and_queue/array_stack.py index 58a7355..ad899d9 100644 --- a/codes/python/chapter_stack_and_queue/array_stack.py +++ b/codes/python/chapter_stack_and_queue/array_stack.py @@ -4,46 +4,45 @@ Created Time: 2022-11-29 Author: Peng Chen (pengchzn@gmail.com) ''' -import os.path as osp -import sys - +import sys, os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * """ 基于数组实现的栈 """ class ArrayStack: def __init__(self): - self._stack = [] - self._size = 0 + self.__stack = [] """ 获取栈的长度 """ def size(self): - return self._size + return len(self.__stack) """ 判断栈是否为空 """ def is_empty(self): - return self._stack == [] + return self.__stack == [] """ 入栈 """ def push(self, item): - self._stack.append(item) - self._size += 1 + self.__stack.append(item) """ 出栈 """ def pop(self): - pop = self._stack.pop() - self._size -= 1 - return pop + return self.__stack.pop() """ 访问栈顶元素 """ def peek(self): - return self._stack[-1] + return self.__stack[-1] """ 访问索引 index 处元素 """ def get(self, index): - return self._stack[index] + return self.__stack[index] + + """ 返回列表用于打印 """ + def toList(self): + return self.__stack +""" Driver Code """ if __name__ == "__main__": """ 初始化栈 """ stack = ArrayStack() @@ -54,20 +53,20 @@ if __name__ == "__main__": stack.push(2) stack.push(5) stack.push(4) - print("栈 stack = ", stack._stack) + print("栈 stack =", stack.toList()) """ 访问栈顶元素 """ peek = stack.peek() - print("栈顶元素 peek = ", peek) + print("栈顶元素 peek =", peek) """ 元素出栈 """ pop = stack.pop() - print("出栈元素 pop = ", pop) - print("出栈后 stack = ", stack._stack) + print("出栈元素 pop =", pop) + print("出栈后 stack =", stack.toList()) """ 获取栈的长度 """ size = stack.size() - print("栈的长度 size = ", size) + print("栈的长度 size =", size) """ 判断是否为空 """ 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 19ffdc3..dce1e35 100644 --- a/codes/python/chapter_stack_and_queue/linkedlist_stack.py +++ b/codes/python/chapter_stack_and_queue/linkedlist_stack.py @@ -4,58 +4,58 @@ Created Time: 2022-11-29 Author: Peng Chen (pengchzn@gmail.com) ''' -import os.path as osp -import sys - +import sys, os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * """ 基于链表实现的栈 """ class LinkedListStack: def __init__(self): - self.head = None + self.__head = None + self.__size = 0 """ 获取栈的长度 """ def size(self): - cnt = 0 - temp = self.head - while temp is not None: - temp = temp.next - cnt += 1 - return cnt + return self.__size """ 判断栈是否为空 """ def is_empty(self): - if not self.head.val and not self.head.next: - return True - return False + return not self.__head """ 入栈 """ def push(self, val): - temp = ListNode(val) - temp.next = self.head - self.head = temp + node = ListNode(val) + node.next = self.__head + self.__head = node + self.__size += 1 """ 出栈 """ def pop(self): - pop = self.head.val - self.head = self.head.next + # 判空处理 + if not self.__head: return None + pop = self.__head.val + self.__head = self.__head.next + self.__size -= 1 return pop """ 访问栈顶元素 """ def peek(self): - return self.head.val + # 判空处理 + if not self.__head: return None + return self.__head.val - def to_array(self): - stack = [] - temp = self.head - while temp: - stack.append(temp.val) - temp = temp.next - stack.reverse() - return stack + """ 转化为列表用于打印 """ + def to_list(self): + arr = [] + node = self.__head + while node: + arr.append(node.val) + node = node.next + arr.reverse() + return arr +""" Driver Code """ if __name__ == "__main__": """ 初始化栈 """ stack = LinkedListStack() @@ -66,20 +66,20 @@ if __name__ == "__main__": stack.push(2) stack.push(5) stack.push(4) - print("栈 stack = ", stack.to_array()) + print("栈 stack =", stack.to_list()) """ 访问栈顶元素 """ peek = stack.peek() - print("栈顶元素 peek = ", peek) + print("栈顶元素 peek =", peek) """ 元素出栈 """ pop = stack.pop() - print("出栈元素 pop = ", pop) - print("出栈后 stack = ", stack.to_array()) + print("出栈元素 pop =", pop) + print("出栈后 stack =", stack.to_list()) """ 获取栈的长度 """ size = stack.size() - print("栈的长度 size = ", size) + print("栈的长度 size =", size) """ 判断是否为空 """ isEmpty = stack.is_empty() diff --git a/codes/python/chapter_stack_and_queue/stack.py b/codes/python/chapter_stack_and_queue/stack.py index 30028d0..80e7fcc 100644 --- a/codes/python/chapter_stack_and_queue/stack.py +++ b/codes/python/chapter_stack_and_queue/stack.py @@ -4,14 +4,15 @@ Created Time: 2022-11-29 Author: Peng Chen (pengchzn@gmail.com) ''' -import os.path as osp -import sys - +import sys, os.path as osp sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) from include import * + +""" Driver Code """ if __name__ == "__main__": """ 初始化栈 """ + # Python 没有内置的栈类,可以把 list 当作栈来使用 stack = [] """ 元素入栈 """ @@ -20,20 +21,20 @@ if __name__ == "__main__": stack.append(2) stack.append(5) stack.append(4) - print("栈 stack = ", stack) + print("栈 stack =", stack) """ 访问栈顶元素 """ peek = stack[-1] - print("栈顶元素 peek = ", peek) + print("栈顶元素 peek =", peek) """ 元素出栈 """ pop = stack.pop() - print("出栈元素 pop = ", pop) - print("出栈后 stack = ", stack) + print("出栈元素 pop =", pop) + print("出栈后 stack =", stack) """ 获取栈的长度 """ size = len(stack) - print("栈的长度 size = ", size) + print("栈的长度 size =", size) """ 判断是否为空 """ - isEmpty = (stack == []) + isEmpty = len(stack) == 0