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 == [])