仿写一个类实现栈

仿写自博客 http://www.cnblogs.com/chongyou/p/7099692.html

class
Node(object): def __init__(self,n): self.val = n self.next = None class mystack(object): def __init__(self): self.top = None def push(self,x): n = Node(x) n.next = self.top self.top = n return n.val def pop(self): if self.top: tmp = self.top.val self.top = self.top.next return tmp return None def peek(self): if self.top: return self.top.val return None if __name__=="__main__": s = mystack() s.push('程劲') s.push('陈培昌') s.push('徐晓冬') s.push('厉智') print(s.pop()) print(s.pop()) print(s.pop()) print(s.pop())

猜你喜欢

转载自www.cnblogs.com/saintdingspage/p/9100434.html