Stack Algorithm (in Python)
栈算法,携Sample Test部分
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items)-1]
def isEmpty(self):
return self.items == []
def size(self):
return len(self.items)
def show(self):
print(self.items)
def __str__(self):
stackAsString = 'bottom -> '
for item in self.items:
stackAsString += item + ' '
stackAsString += '<- top'
return stackAsString
Sample Test:
abc = Stack()
abc.push("cde")
abc.push("sdd")
abc.push("jhi")
abc.push("fff")
abc.show()
while not abc.isEmpty():
abc.pop()
abc.show()