Update python code of stack.

This commit is contained in:
Yudong Jin 2022-11-29 23:35:51 +08:00
parent 3edfe649af
commit 53cc651af2
3 changed files with 60 additions and 60 deletions

View File

@ -4,46 +4,45 @@ Created Time: 2022-11-29
Author: Peng Chen (pengchzn@gmail.com) Author: Peng Chen (pengchzn@gmail.com)
''' '''
import os.path as osp import sys, os.path as osp
import sys
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import * from include import *
""" 基于数组实现的栈 """ """ 基于数组实现的栈 """
class ArrayStack: class ArrayStack:
def __init__(self): def __init__(self):
self._stack = [] self.__stack = []
self._size = 0
""" 获取栈的长度 """ """ 获取栈的长度 """
def size(self): def size(self):
return self._size return len(self.__stack)
""" 判断栈是否为空 """ """ 判断栈是否为空 """
def is_empty(self): def is_empty(self):
return self._stack == [] return self.__stack == []
""" 入栈 """ """ 入栈 """
def push(self, item): def push(self, item):
self._stack.append(item) self.__stack.append(item)
self._size += 1
""" 出栈 """ """ 出栈 """
def pop(self): def pop(self):
pop = self._stack.pop() return self.__stack.pop()
self._size -= 1
return pop
""" 访问栈顶元素 """ """ 访问栈顶元素 """
def peek(self): def peek(self):
return self._stack[-1] return self.__stack[-1]
""" 访问索引 index 处元素 """ """ 访问索引 index 处元素 """
def get(self, index): def get(self, index):
return self._stack[index] return self.__stack[index]
""" 返回列表用于打印 """
def toList(self):
return self.__stack
""" Driver Code """
if __name__ == "__main__": if __name__ == "__main__":
""" 初始化栈 """ """ 初始化栈 """
stack = ArrayStack() stack = ArrayStack()
@ -54,20 +53,20 @@ if __name__ == "__main__":
stack.push(2) stack.push(2)
stack.push(5) stack.push(5)
stack.push(4) stack.push(4)
print("栈 stack = ", stack._stack) print("栈 stack =", stack.toList())
""" 访问栈顶元素 """ """ 访问栈顶元素 """
peek = stack.peek() peek = stack.peek()
print("栈顶元素 peek = ", peek) print("栈顶元素 peek =", peek)
""" 元素出栈 """ """ 元素出栈 """
pop = stack.pop() pop = stack.pop()
print("出栈元素 pop = ", pop) print("出栈元素 pop =", pop)
print("出栈后 stack = ", stack._stack) print("出栈后 stack =", stack.toList())
""" 获取栈的长度 """ """ 获取栈的长度 """
size = stack.size() size = stack.size()
print("栈的长度 size = ", size) print("栈的长度 size =", size)
""" 判断是否为空 """ """ 判断是否为空 """
isEmpty = stack.is_empty() isEmpty = stack.is_empty()

View File

@ -4,58 +4,58 @@ Created Time: 2022-11-29
Author: Peng Chen (pengchzn@gmail.com) Author: Peng Chen (pengchzn@gmail.com)
''' '''
import os.path as osp import sys, os.path as osp
import sys
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import * from include import *
""" 基于链表实现的栈 """ """ 基于链表实现的栈 """
class LinkedListStack: class LinkedListStack:
def __init__(self): def __init__(self):
self.head = None self.__head = None
self.__size = 0
""" 获取栈的长度 """ """ 获取栈的长度 """
def size(self): def size(self):
cnt = 0 return self.__size
temp = self.head
while temp is not None:
temp = temp.next
cnt += 1
return cnt
""" 判断栈是否为空 """ """ 判断栈是否为空 """
def is_empty(self): def is_empty(self):
if not self.head.val and not self.head.next: return not self.__head
return True
return False
""" 入栈 """ """ 入栈 """
def push(self, val): def push(self, val):
temp = ListNode(val) node = ListNode(val)
temp.next = self.head node.next = self.__head
self.head = temp self.__head = node
self.__size += 1
""" 出栈 """ """ 出栈 """
def pop(self): 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 return pop
""" 访问栈顶元素 """ """ 访问栈顶元素 """
def peek(self): def peek(self):
return self.head.val # 判空处理
if not self.__head: return None
return self.__head.val
def to_array(self): """ 转化为列表用于打印 """
stack = [] def to_list(self):
temp = self.head arr = []
while temp: node = self.__head
stack.append(temp.val) while node:
temp = temp.next arr.append(node.val)
stack.reverse() node = node.next
return stack arr.reverse()
return arr
""" Driver Code """
if __name__ == "__main__": if __name__ == "__main__":
""" 初始化栈 """ """ 初始化栈 """
stack = LinkedListStack() stack = LinkedListStack()
@ -66,20 +66,20 @@ if __name__ == "__main__":
stack.push(2) stack.push(2)
stack.push(5) stack.push(5)
stack.push(4) stack.push(4)
print("栈 stack = ", stack.to_array()) print("栈 stack =", stack.to_list())
""" 访问栈顶元素 """ """ 访问栈顶元素 """
peek = stack.peek() peek = stack.peek()
print("栈顶元素 peek = ", peek) print("栈顶元素 peek =", peek)
""" 元素出栈 """ """ 元素出栈 """
pop = stack.pop() pop = stack.pop()
print("出栈元素 pop = ", pop) print("出栈元素 pop =", pop)
print("出栈后 stack = ", stack.to_array()) print("出栈后 stack =", stack.to_list())
""" 获取栈的长度 """ """ 获取栈的长度 """
size = stack.size() size = stack.size()
print("栈的长度 size = ", size) print("栈的长度 size =", size)
""" 判断是否为空 """ """ 判断是否为空 """
isEmpty = stack.is_empty() isEmpty = stack.is_empty()

View File

@ -4,14 +4,15 @@ Created Time: 2022-11-29
Author: Peng Chen (pengchzn@gmail.com) Author: Peng Chen (pengchzn@gmail.com)
''' '''
import os.path as osp import sys, os.path as osp
import sys
sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__)))) sys.path.append(osp.dirname(osp.dirname(osp.abspath(__file__))))
from include import * from include import *
""" Driver Code """
if __name__ == "__main__": if __name__ == "__main__":
""" 初始化栈 """ """ 初始化栈 """
# Python 没有内置的栈类,可以把 list 当作栈来使用
stack = [] stack = []
""" 元素入栈 """ """ 元素入栈 """
@ -20,20 +21,20 @@ if __name__ == "__main__":
stack.append(2) stack.append(2)
stack.append(5) stack.append(5)
stack.append(4) stack.append(4)
print("栈 stack = ", stack) print("栈 stack =", stack)
""" 访问栈顶元素 """ """ 访问栈顶元素 """
peek = stack[-1] peek = stack[-1]
print("栈顶元素 peek = ", peek) print("栈顶元素 peek =", peek)
""" 元素出栈 """ """ 元素出栈 """
pop = stack.pop() pop = stack.pop()
print("出栈元素 pop = ", pop) print("出栈元素 pop =", pop)
print("出栈后 stack = ", stack) print("出栈后 stack =", stack)
""" 获取栈的长度 """ """ 获取栈的长度 """
size = len(stack) size = len(stack)
print("栈的长度 size = ", size) print("栈的长度 size =", size)
""" 判断是否为空 """ """ 判断是否为空 """
isEmpty = (stack == []) isEmpty = len(stack) == 0