problem-solving-with-algorithms-and-data-structure-usingpython(使用python解决算法和数据结构) -- 基本数据结构 -- 队列
1. 什么是队列?
队列是项的有序结合,其中添加新项的一端称为队尾,移除项的一端称为队首。
FIFO:先进先出
2. 队列抽象数据类型
队列操作如下:
Queue() 创建一个空的新队列。 它不需要参数,并返回一个空队列。
enqueue(item) 将新项添加到队尾。 它需要 item 作为参数,并不返回任何内容。
dequeue() 从队首移除项。它不需要参数并返回 item。 队列被修改。
isEmpty() 查看队列是否为空。它不需要参数,并返回布尔值。
size() 返回队列中的项数。它不需要参数,并返回一个整数。
3. python实现队列
class Queue:
def __init__(self):
self.items = [] def isEmpty(self):
return self.items == [] def enqueue(self,item):
self.items.insert(0,item) def dequeue(self):
return self.items.pop() def size(self):
return len(self.items)
4. 模拟:烫手山芋
在这个游戏中,孩子们围成一个圈,并尽可能快的将一个山芋递给旁边的孩子。在某一个时间,动作结束,有山芋的孩子从圈中移除。游戏继续开始直到剩下最后一个孩子。
烫手山芋游戏介绍
假设拿着山芋的孩子在队列的前面。当拿到山芋的时候,这个孩子将先出列再入队列,把他放在队列的最后。经过 num 次的出队入队后,前面的孩子将被永久移除队列。并且另一个周期开始,继续此过程,直到只剩下一个名字(队列的大小为 1)。
from pythonds.basic.queue import Queue def hotPotato(nameList,num):
simqueue = Queue()
for name in nameList:
simqueue.enqueue(name)
while simqueue.size() > 1:
for i in range(num):
simqueue.enqueue(simqueue.dequeue())
simqueue.dequeue()
return simqueue.dequeue() print(hotPotato(["Bill", "David", "Susan", "Jane", "Kent", "Brad"], 7))

problem-solving-with-algorithms-and-data-structure-usingpython(使用python解决算法和数据结构) -- 基本数据结构 -- 队列的更多相关文章
- 学习笔记之Problem Solving with Algorithms and Data Structures using Python
Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...
- [Algorithms] Tree Data Structure in JavaScript
In a tree, nodes have a single parent node and may have many children nodes. They never have more th ...
- CDOJ 483 Data Structure Problem DFS
Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- hdu-5929 Basic Data Structure(双端队列+模拟)
题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 5929 Basic Data Structure 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- hdu 4217 Data Structure? 树状数组求第K小
Data Structure? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- (*medium)LeetCode 211.Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- Add and Search Word - Data structure design
https://leetcode.com/problems/add-and-search-word-data-structure-design/ Design a data structure tha ...
- 211. Add and Search Word - Data structure design
题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...
随机推荐
- git修改文件权限方式
查看Repository中文件权限 git ls-tree HEAD 100644 blob 018321abfbff52d175a788597f5b5f3f17f67dc7 .gitignore 1 ...
- Flask从入门到精通之模型定义
模型这个术语表示程序使用的持久化实体.在ORM 中,模型一般是一个Python 类,类中的属性对应数据库表中的列. Flask-SQLAlchemy 创建的数据库实例为模型提供了一个基类以及一系列辅助 ...
- vue-router进阶笔记
导航守卫 vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航,大白话,检测路由跳转过程中的具体的变化. 一.全局前置守卫——router.beforeEach 使用router. ...
- 【BZOJ3143】【HNOI2013】游走 高斯消元
题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3143 我们令$P_i$表示从第i号点出发的期望次数.则$P_n$显然为$0$. 对于$P ...
- 使用python+selenium对web进行自动化测试
想用python代码,对web网页进行自动化测试 web自动化测试和手动测试的区别: 手动测试:通过手动去对网页的功能进行点点点 web自动化:可以通过代码,自动对网页点点点 首先,将python+s ...
- 使用ggbio在R中制作弦图
分享一个制作弦图的R包:ggbio. 以下是一个简单的使用实例,效果图和代码如下. library(GenomicRanges) set.seed(1) N <- 100 gr <- GR ...
- zabbix 调用python脚本监控 磁盘剩余空间(创建模版,创建监控项,创建触发器)
主要 记录一下 使用zabbix 自己创建模版.监控项.触发器,并调用python脚本. 需求: 监控备份机磁盘剩余空间(windows系统) 一.安装zabbix_agent 比较简单 修改配置文 ...
- PMP备考指南之第一章:引论
本文已同步至 GitHub/Gitee/公众号,感兴趣的同学帮忙点波关注~ 第一章 引论 1."项目管理知识体系":应该包含所有行业.应用领域项目管理的具体知识.技能.方法和实践 ...
- 使用Apache Bench对网站性能进行测试
使用Apache Bench对网站性能进行测试
- Chrome插件下载地址
www.crx4chrome.com可以直接下载 Chrome Store 插件 在chrome web store好像只能安装插件.