class Stack:
def __init__(self):
self._first=None
def push(self,item):
self._first=_Node(item,self._first)
def pop(self):
if not self._first==None:
old_item=self._first.item
self._first=self._first.next
return old_item
else:
print("your stack is empty now")
def isEmpty(self):
return self._first==None
class _Node:
def __init__(self,item,next):
self.item=item
self.next=next
def main():
stack=Stack()
stack.push("to")
stack.push("be")
stack.push("or")
print(stack.pop())
while not stack.isEmpty():
print(stack.pop())
if __name__=="__main__":main()
print(stack.pop())
stack.push("to")
stack.push("be")
print(len(stack))
python 用链表实现栈
最新推荐文章于 2022-06-24 13:45:07 发布