码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 数据结构 | Python实现列表队列 | 源码和示例


    1. # List node class
    2. class Queue:
    3. def __init__(self):
    4. # Initialize an empty list to store queue elements
    5. self.items = []
    6. def is_empty(self):
    7. # Check if the queue is empty
    8. return len(self.items) == 0
    9. def enqueue(self, item):
    10. # Enqueue (add) an item to the end of the queue
    11. self.items.append(item)
    12. def dequeue(self):
    13. if not self.is_empty():
    14. # Dequeue (remove and return) the item from the front of the queue
    15. return self.items.pop(0)
    16. else:
    17. # If the queue is empty, return a message indicating so
    18. return "Queue is empty"
    19. def peek(self):
    20. if not self.is_empty():
    21. # Peek at (view) the item at the front of the queue without removing it
    22. return self.items[0]
    23. else:
    24. # If the queue is empty, return a message indicating so
    25. return "Queue is empty"
    26. def size(self):
    27. # Get the number of elements in the queue
    28. return len(self.items)
    29. def output_queue(self):
    30. # Output the elements of the queue
    31. print("Queue elements:", self.items)
    32. def main():
    33. # Create a new queue
    34. my_queue = Queue()
    35. # Check if the queue is empty
    36. print("Is the queue empty?", my_queue.is_empty())
    37. # Enqueue elements to the queue
    38. my_queue.enqueue(1)
    39. my_queue.enqueue(2)
    40. my_queue.enqueue(3)
    41. # Output the elements of the queue
    42. my_queue.output_queue()
    43. # Check the size of the queue
    44. print("Queue size:", my_queue.size())
    45. # Peek at the front element of the queue
    46. print("Front element:", my_queue.peek())
    47. # Dequeue elements from the queue
    48. dequeued_item = my_queue.dequeue()
    49. print("Dequeued item:", dequeued_item)
    50. # Check the size of the queue after dequeue
    51. print("Queue size after dequeue:", my_queue.size())
    52. # Check if the queue is empty again
    53. print("Is the queue empty now?", my_queue.is_empty())
    54. # Output the elements of the queue
    55. my_queue.output_queue()
    56. if __name__ == "__main__":
    57. main()

    结果:

    Is the queue empty? True
    Queue elements: [1, 2, 3]
    Queue size: 3
    Front element: 1
    Dequeued item: 1
    Queue size after dequeue: 2
    Is the queue empty now? False
    Queue elements: [2, 3]

  • 相关阅读:
    Observability:从零开始创建 Java 微服务并监控它 (一)
    商用车自动驾驶进入「拐点」时刻
    判断一个数组是否包含另一个
    网络安全人才缺口超百万,如今的就业情况怎样?
    贝壳找房上海研发全员被优化,公司回应来了!
    4.NVIDIA Deepstream开发指南中文版--C和C++示例应用程序源详细信息
    msvcr120.dll缺失怎么修复,快速修复msvcr120.dll丢失的三个有效方法
    前端面试,备考第 17 天—— 怎么对项目做性能优化(1):图片优化 | CDN | 懒加载 | 回流重绘 | 动画
    QML、C++ 和 JS 三者之间的交互
    孙荣辛|大数据穿针引线进阶必看——Google经典大数据知识
  • 原文地址:https://blog.csdn.net/L040514/article/details/134013990
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号