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解决算法和数据结构) -- 基本数据结构 -- 队列的更多相关文章

  1. 学习笔记之Problem Solving with Algorithms and Data Structures using Python

    Problem Solving with Algorithms and Data Structures using Python — Problem Solving with Algorithms a ...

  2. [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 ...

  3. CDOJ 483 Data Structure Problem DFS

    Data Structure Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/proble ...

  4. [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  5. hdu-5929 Basic Data Structure(双端队列+模拟)

    题目链接: Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  6. HDU 5929 Basic Data Structure 模拟

    Basic Data Structure Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  7. hdu 4217 Data Structure? 树状数组求第K小

    Data Structure? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. (*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 ...

  9. Add and Search Word - Data structure design

    https://leetcode.com/problems/add-and-search-word-data-structure-design/ Design a data structure tha ...

  10. 211. Add and Search Word - Data structure design

    题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...

随机推荐

  1. Django安装(第一个项目)

    day60 从系统中选择已存在的解释器. 新建Django项目         命令行创建:             django-admin startproject mysite          ...

  2. win8 下 intellij idea 13 中文输入覆盖的问题

    网上的解决方案好啰嗦,精简下如下: 设置环境变量 64位:IDEA_JDK_64 32位:IDEA_JDK 设置的value必须是JDK1.6(IDEA自带1.7版本有问题),和平时设置的JAVA_H ...

  3. apicloud 基础

    时间成本  人力成本  很多人想开发app  又碍于时间和金钱成本 . 本色对app  要求不高的话. 混合app 开发是一种很好的方式. apicloud  就是一种很好的方式. apicloud ...

  4. 【xsy1131】tortue FFT

    题目大意: 一次游戏要按N个按键.每个按键阿米巴有P[i]的概率按错.对于一串x个连续按对的按键,阿米巴可以得分 $f(x)=tan(\dfrac{x}{N})\times e^{arcsin(0.8 ...

  5. js06

    接着我们继续学习一些js的知识,这里主要讲js的一些库jq与prototype的一些基础. 1.jq函数    $():向其传递DOM对象,返回jQuery对象        jq允许通过CSS选择器 ...

  6. 03——Solr学习之Solr的使用(不会用)

    1.先放上次在linux搭建成功的solr管理UI界面 2.有个很蛋疼的问题我就要吐槽一下了 由于没接触过solr这玩意,在百度上一顿操作搜索怎么用,怎么导入数据,建索引库什么的,看了一大片别人的博客 ...

  7. Python生成gexf文件并导入gephi做网络图分析

    Gephi是一款优秀的复杂网络分析软件,支持导入多种格式的文件.gexf格式是Gephi 推荐的格式,基于 XML.本文是一个用python写的简单Demo,示例如何生成一个典型的gexf格式文件.代 ...

  8. Opserver 初探二《exceptions配置》

    上一节主要介绍Opserver的搭建以及redis.sqlserver监控的配置,本节主要介绍异常日志的记录和监控.要实现异常日志的监控我们需要在项目中引入StackExchange.Exceptio ...

  9. Vue的watch监听事件

    Vue的watch监听事件 相关Html: <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  10. Linux -- 使用笔记

    Linux新增分辨率1920x1080 sudo gedit /etc/default/grub 找到:#GRUB_GFXMODE=640x480 在这行下面加一行GRUB_GFXMODE=1920x ...