Skip to content

Commit

Permalink
Update python code of stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Nov 29, 2022
1 parent 3edfe64 commit 53cc651
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 63 deletions.
37 changes: 18 additions & 19 deletions codes/python/chapter_stack_and_queue/array_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,45 @@
Author: Peng Chen ([email protected])
'''

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()
Expand All @@ -54,20 +53,20 @@ def get(self, index):
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()
70 changes: 35 additions & 35 deletions codes/python/chapter_stack_and_queue/linkedlist_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,58 @@
Author: Peng Chen ([email protected])
'''

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()
Expand All @@ -66,20 +66,20 @@ def to_array(self):
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()
19 changes: 10 additions & 9 deletions codes/python/chapter_stack_and_queue/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
Author: Peng Chen ([email protected])
'''

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

""" 元素入栈 """
Expand All @@ -20,20 +21,20 @@
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

0 comments on commit 53cc651

Please sign in to comment.