30 lines
685 B
Python
Executable File
30 lines
685 B
Python
Executable File
class Stack(object):
|
|
def __init__(self, length):
|
|
self.item = []
|
|
self.length = length
|
|
|
|
def push(self, value):
|
|
if self.is_full():
|
|
raise Exception('栈已满')
|
|
else:
|
|
self.item.append(value)
|
|
|
|
def pop(self):
|
|
if self.is_empty():
|
|
raise Exception('栈为空')
|
|
return self.item.pop()
|
|
|
|
def top(self):
|
|
if self.is_empty():
|
|
raise Exception('栈为空')
|
|
return self.item[-1]
|
|
|
|
def is_empty(self) -> bool:
|
|
return len(self.item) == 0
|
|
|
|
def is_full(self) -> bool:
|
|
return len(self.item) >= self.length
|
|
|
|
def size(self):
|
|
return len(self.item)
|