Using Lists as Stacks in Python----在Python中把链表当作堆栈使用

来源:互联网 发布:网络退票手续费 编辑:程序博客网 时间:2024/05/22 10:54

    The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (``last-in, first-out''). To add an item to the top of the stack, use append(). To retrieve an item from the top of the stack, use pop() without an explicit index.

    链表方法使得链表可以很方便的做为一个堆栈来使用,堆栈作为特定的数据结 构,最先进入的元素最后一个被释放(后进先出)。用 append() 方法可以把一 个元素添加到堆栈顶。用不指定索引的 pop() 方法可以把一个元素从堆栈顶释放出来。

For example:

>>> stack = [3, 4, 5]>>> stack.append(6)>>> stack.append(7)>>> stack[3, 4, 5, 6, 7]>>> stack.pop()7>>> stack[3, 4, 5, 6]>>> stack.pop()6>>> stack.pop()5>>> stack[3, 4]


0 0