Skip to content

Latest commit

 

History

History
95 lines (85 loc) · 3.19 KB

一行代码让Python实现栈?!.md

File metadata and controls

95 lines (85 loc) · 3.19 KB

@TOC

作为一种常见的数据结构,栈在计算机科学中得到广泛应用。Python 作为一种非常流行的编程语言,也提供了简单的方法来实现栈结构。

一行代码

许多人谈到使用 Python 实现栈时,大多会使用 Python 列表(List)来初始化一个栈,并使用其内置方法来模拟栈的操作。但是,实际上 Python 还提供了一种更加直接的方法来实现栈,这也是一种非常简单和优雅的解决方案。

下面就让我们看看如何使用 Python 一行代码来实现栈结构。

stack = []

没错,只需要这一行代码就可以实现一个栈了!接下来,我们可以使用 Python 列表的 append() 和 pop() 方法来完成入栈和出栈操作。

入栈

stack.append("One")
stack.append("Two")
stack.append("Three")

出栈

print(stack.pop())   # 输出 "Three"
print(stack.pop())   # 输出 "Two"
print(stack.pop())   # 输出 "One"

如上所示,我们可以用 append() 方法将多个元素入栈,然后使用 pop() 方法将元素逐个出栈。

当然,这只是简单的入栈和出栈示例。实际应用中,我们还需要处理一些特殊情况,例如栈溢出、栈为空等等。

其他栈实现

内置模块queue

put(),get(),empty()

from queue import LifoQueue
# Last In First Out 后进先出
stack=LifoQueue()
for i in range(5):
    stack.put(i)
while not stack.empty():
    print(stack.get(),end=' ')

自己实现

其实明白原理和需求后,我们有很多种实现方式,这里给出两种

class Stack():
    def __init__(self):
        self.stk=[]
    def put(self,x):
        self.stk.append(x)
    def get(self):
        return self.stk.pop()
    def empty(self):
        return False if self.stk else True
class Stack1():
    def __init__(self,N=10**5):
        self.N=N
        self.stk=[0]*N
        self.cur=0
    def put(self,x):
        self.stk[self.cur]=x
        self.cur+=1
    def get(self):
        self.cur-=1
        return self.stk[self.cur]
    def empty(self):
        return False if self.cur else True

stack=Stack()
for i in range(5):
    stack.put(i)
while not stack.empty():
    print(stack.get(),end=' ')

stack1=Stack1()
for i in range(5):
    stack1.put(i)
while not stack1.empty():
    print(stack1.get(),end=' ')

总之,使用 Python 来实现栈是一种非常简单和优雅的解决方案。相信你也可以通过这种方法来轻松实现栈的操作!

总结

🐋 🐬 🐶 🐳 🐰 🦀☝️ ⭐ 👉 👀

如果你对这篇文章感兴趣,欢迎在评论区留言,分享你的想法和建议。如果你喜欢我的博客,请记得点赞、收藏和关注我,我会持续更新更多有用的网页技巧和教程。谢谢大家!


更多宝藏

🍇🍉🍊🍏🍋🍅🥝🥥🫒🫕🥗 项目仓库看这里🤗: https://github.com/w-x-x-w https://gitee.com/w-_-x 博客文章看这里🤭: https://blog.csdn.net/weixin_62650212 视频推送看这里🤤: https://space.bilibili.com/1909782963