python - Python单链表,pop和push怎么写?

标签 python stack

我正在尝试编写一个使用堆栈中的 Push 和 Pop 的类(使用单链表)。我不确定如何编写 push 和 pop 函数。我真的需要一个用 Python 编写的具有以下功能的简单示例。

Push
Pop
ifEmpty

最佳答案

来自 the docs Dyno Fu 链接到:

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. 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]

最后,到 check if a list is empty :

>>> my_list = []
>>> not my_list
True

关于python - Python单链表,pop和push怎么写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2160994/

相关文章:

python - 当类中的函数返回一个值时,它返回到哪里?

python - 打印不带分隔符(括号、引号和逗号)的字符串数组

python - sqlalchemy - 强制它永不提交?

python - 如何禁用向远程主机发送回溯?

java - 将字符串中的字符添加到堆栈中

java - Java 中 Stack 的 EmptyStackException 错误

java - 是否有 Java 8 等同于内置的 Python enumerate?

c++ - 堆栈溢出 Visual C++,可能是数组大小?

java - 循环和堆栈,回文家庭作业

c++ - 在 C++ 2003 中重新调整本地分配的对象