栈的python代码
This commit is contained in:
parent
b47f54886e
commit
f8d44be73d
@ -1,7 +1,7 @@
|
|||||||
'''
|
'''
|
||||||
File: array_stack.py
|
File: array_stack.py
|
||||||
Created Time: 2022-11-25
|
Created Time: 2022-11-29
|
||||||
Author: Krahets (krahets@163.com)
|
Author: Peng Chen (pengchzn@gmail.com)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os.path as osp
|
import os.path as osp
|
||||||
@ -11,43 +11,35 @@ 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
|
self._size = 0
|
||||||
|
|
||||||
""" 获取栈的长度 """
|
""" 获取栈的长度 """
|
||||||
|
|
||||||
def size(self):
|
def size(self):
|
||||||
return self._size
|
return self._size
|
||||||
|
|
||||||
""" 判断栈是否为空 """
|
""" 判断栈是否为空 """
|
||||||
|
|
||||||
def isEmpty(self):
|
def isEmpty(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
|
self._size += 1
|
||||||
|
|
||||||
""" 出栈 """
|
""" 出栈 """
|
||||||
|
|
||||||
def pop(self):
|
def pop(self):
|
||||||
pop = self._stack.pop()
|
pop = self._stack.pop()
|
||||||
self._size -= 1
|
self._size -= 1
|
||||||
return pop
|
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]
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
'''
|
'''
|
||||||
File: linkedlist_stack.py
|
File: linkedlist_stack.py
|
||||||
Created Time: 2022-11-25
|
Created Time: 2022-11-29
|
||||||
Author: Krahets (krahets@163.com)
|
Author: Peng Chen (pengchzn@gmail.com)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os.path as osp
|
import os.path as osp
|
||||||
@ -11,14 +11,11 @@ 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
|
||||||
|
|
||||||
""" 获取栈的长度 """
|
""" 获取栈的长度 """
|
||||||
|
|
||||||
def size(self):
|
def size(self):
|
||||||
cnt = 0
|
cnt = 0
|
||||||
temp = self.head
|
temp = self.head
|
||||||
@ -28,28 +25,24 @@ class LinkedListStack:
|
|||||||
return cnt
|
return cnt
|
||||||
|
|
||||||
""" 判断栈是否为空 """
|
""" 判断栈是否为空 """
|
||||||
|
|
||||||
def is_empty(self):
|
def is_empty(self):
|
||||||
if not self.head.val and not self.head.next:
|
if not self.head.val and not self.head.next:
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
""" 入栈 """
|
""" 入栈 """
|
||||||
|
|
||||||
def push(self, val):
|
def push(self, val):
|
||||||
temp = ListNode(val)
|
temp = ListNode(val)
|
||||||
temp.next = self.head
|
temp.next = self.head
|
||||||
self.head = temp
|
self.head = temp
|
||||||
|
|
||||||
""" 出栈 """
|
""" 出栈 """
|
||||||
|
|
||||||
def pop(self):
|
def pop(self):
|
||||||
pop = self.head.val
|
pop = self.head.val
|
||||||
self.head = self.head.next
|
self.head = self.head.next
|
||||||
return pop
|
return pop
|
||||||
|
|
||||||
""" 访问栈顶元素 """
|
""" 访问栈顶元素 """
|
||||||
|
|
||||||
def peek(self):
|
def peek(self):
|
||||||
return self.head.val
|
return self.head.val
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
'''
|
'''
|
||||||
File: stack.py
|
File: stack.py
|
||||||
Created Time: 2022-11-25
|
Created Time: 2022-11-29
|
||||||
Author: Krahets (krahets@163.com)
|
Author: Peng Chen (pengchzn@gmail.com)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
import os.path as osp
|
import os.path as osp
|
||||||
|
Loading…
Reference in New Issue
Block a user