将列表当做堆栈使用
列表方法使得列表可以很方便的作为一个堆栈来使用,堆栈作为特定的数据结构,
最先进入的元素最后一个被释放(后进先出)。用 append() 方法可以把一个元素添加到堆栈顶。
用不指定索引的 pop() 方法可以把一个元素从堆栈顶释放出来。
解堆栈 - 堆栈是一种线性数据结构,适用于LIFO机制,即后进先出 (这意味着最后插入的项将被首先移除(弹出))。 Thus, to implement a stack, basically we have to do two things: 因此,要实现堆栈,基本上我们必须做两件事: Inserting (PUSH) elements at the end of the list 在列表末尾插入(PUSH)元素 Removing (POP) elements from the end of the list 从列表末尾删除(POP)元素 i.e. both operations should be done from one end. 即两种操作都应从一端完成。 In Python, we can implement a stack by using list methods as they have the capability to insert or remove/pop elements from the end of the list. 在Python中,我们可以使用列表方法来实现堆栈,因为它们具有从列表末尾插入或删除/弹出元素的功能。 Method that will be used: 将使用的方法: append(x) : Appends x at the end of the list 追加(X):X附加在列表的末尾 pop() : Removes last elements of the list pop() :删除列表的最后一个元素
实例: