Skip to main content
 首页 » 编程设计

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

2025年05月04日9bhlsheji

我正在尝试编写一个使用堆栈中的 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