2020/2/4栈的链式存储

栈的链式存储

#一侧操作的列表
class StackError(Exception):
    pass
class Node(object):
    def __init__(self,val,next=None):
        self.val=val
        self.next=next
class LStack:
    def __init__(self):
        self._top=None
    def is _empty(self):
        return self._top is None
    def push(self,elem):
        self._top=Node(elem,self._top)
    def pop(self):
        if self._top is None:
            raise StackError("stack is empty")
        p=self._top
        self._top=p.next
        return p.val
    def top(self):
        if self._top is None:
            raise StackError("stack is empty")
        return self._top.val
if __name__=="__main__":
    st=LStack()
    print(st.is_empty())
    st.push(10)
    st.push(20)
    st.push(30)
    while not st.is_empty():
        print(st.pop())
    print(st.top())



发布了18 篇原创文章 · 获赞 0 · 访问量 267

猜你喜欢

转载自blog.csdn.net/dongxieaitonglao/article/details/104165526