|
|
|
@ -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
|
|
|
|
|
|
|
|
|
|
def to_array(self):
|
|
|
|
|
stack = []
|
|
|
|
|
temp = self.head
|
|
|
|
|
while temp:
|
|
|
|
|
stack.append(temp.val)
|
|
|
|
|
temp = temp.next
|
|
|
|
|
stack.reverse()
|
|
|
|
|
return stack
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 判空处理
|
|
|
|
|
if not self.__head: return None
|
|
|
|
|
return self.__head.val
|
|
|
|
|
|
|
|
|
|
""" 转化为列表用于打印 """
|
|
|
|
|
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()
|
|
|
|
|