码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 数据结构 | Python实现栈的基本操作 | 源码和示例


    1. # Stack class with basic stack operations
    2. class Stack:
    3. def __init__(self):
    4. # Initialize an empty list to store stack elements
    5. self.items = []
    6. def is_empty(self):
    7. # Check if the stack is empty
    8. return len(self.items) == 0
    9. def push(self, item):
    10. # Push (add) an item onto the top of the stack
    11. self.items.append(item)
    12. def pop(self):
    13. if not self.is_empty():
    14. # Pop (remove and return) the item from the top of the stack
    15. return self.items.pop()
    16. else:
    17. # If the stack is empty, return a message indicating so
    18. return "Stack is empty"
    19. def peek(self):
    20. if not self.is_empty():
    21. # Peek at (view) the item on the top of the stack without removing it
    22. return self.items[-1]
    23. else:
    24. # If the stack is empty, return a message indicating so
    25. return "Stack is empty"
    26. def size(self):
    27. # Get the number of elements in the stack
    28. return len(self.items)
    29. def output_stack(self):
    30. # Output the elements of the stack
    31. print("Stack elements:", self.items)
    32. def main():
    33. # Create a new stack
    34. my_stack = Stack()
    35. # Check if the stack is empty
    36. print("Is the stack empty?", my_stack.is_empty())
    37. # Push elements onto the stack
    38. my_stack.push(1)
    39. my_stack.push(2)
    40. my_stack.push(3)
    41. # Output the elements of the stack
    42. my_stack.output_stack()
    43. # Check the size of the stack
    44. print("Stack size:", my_stack.size())
    45. # Peek at the top element of the stack
    46. print("Top element:", my_stack.peek())
    47. # Pop elements from the stack
    48. popped_item = my_stack.pop()
    49. print("Popped item:", popped_item)
    50. # Check the size of the stack after popping
    51. print("Stack size after pop:", my_stack.size())
    52. # Check if the stack is empty again
    53. print("Is the stack empty now?", my_stack.is_empty())
    54. # Output the elements of the stack
    55. my_stack.output_stack()
    56. if __name__ == "__main__":
    57. main()

    结果:

    Is the stack empty? True
    Stack elements: [1, 2, 3]
    Stack size: 3
    Top element: 3
    Popped item: 3
    Stack size after pop: 2
    Is the stack empty now? False
    Stack elements: [1, 2]

  • 相关阅读:
    华为OD机试 - 等和子数组最小和 - 深度优先搜索(Java 2022 Q4 100分)
    【Python练习】task-06 函数的练习和实验
    Android字母、数字版本、API级别对照表2022
    商城项目09_品牌管理菜单、快速显示开关、阿里云进行文件上传、结合Alibaba管理OSS、服务端签名后直传
    为什么使用RedisDesktopManager可以连上redis,微服务似乎无法访问redis
    Java集成支付宝支付流程
    锁机制之 Condition 接口
    题目 1311: 数字三角形
    Babel 插件:30分钟从入门到实战
    如何用Redis实现分布式锁?
  • 原文地址:https://blog.csdn.net/L040514/article/details/134013958
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号