python 列表当做堆栈和队列使用

将列表当做堆栈使用
列表方法使得列表可以很方便的作为一个堆栈来使用,堆栈作为特定的数据结构,
最先进入的元素最后一个被释放(后进先出)。用 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() :删除列表的最后一个元素

实例:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值