栈的python代码
This commit is contained in:
parent
b47f54886e
commit
f8d44be73d
@ -1,7 +1,7 @@
|
||||
'''
|
||||
File: array_stack.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
|
||||
import os.path as osp
|
||||
@ -11,43 +11,35 @@ 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]
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
'''
|
||||
File: linkedlist_stack.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
|
||||
import os.path as osp
|
||||
@ -11,14 +11,11 @@ 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
|
||||
@ -28,28 +25,24 @@ class LinkedListStack:
|
||||
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
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
'''
|
||||
File: stack.py
|
||||
Created Time: 2022-11-25
|
||||
Author: Krahets (krahets@163.com)
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
'''
|
||||
|
||||
import os.path as osp
|
||||
|
Loading…
Reference in New Issue
Block a user