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)
'''
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,7 +53,7 @@ if __name__ == "__main__":
stack.push(2)
stack.push(5)
stack.push(4)
print("栈 stack = ", stack._stack)
print("栈 stack =", stack.toList())
""" 访问栈顶元素 """
peek = stack.peek()
@ -63,7 +62,7 @@ if __name__ == "__main__":
""" 元素出栈 """
pop = stack.pop()
print("出栈元素 pop =", pop)
print("出栈后 stack = ", stack._stack)
print("出栈后 stack =", stack.toList())
""" 获取栈的长度 """
size = stack.size()

View File

@ -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,7 +66,7 @@ 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()
@ -75,7 +75,7 @@ if __name__ == "__main__":
""" 元素出栈 """
pop = stack.pop()
print("出栈元素 pop =", pop)
print("出栈后 stack = ", stack.to_array())
print("出栈后 stack =", stack.to_list())
""" 获取栈的长度 """
size = stack.size()

View File

@ -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 = []
""" 元素入栈 """
@ -36,4 +37,4 @@ if __name__ == "__main__":
print("栈的长度 size =", size)
""" 判断是否为空 """
isEmpty = (stack == [])
isEmpty = len(stack) == 0