队列(queue),是一种操作受限的线性表。只允许在队列的一端添加元素,在队列的另一端删除元素。能添加元素的一端称为队尾,能删除元素的一端称为队头。

队列最大的特性是:先进先出(FIFO,first in first out)

下面为普通队列的python实现:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = "hsz" class Queue(object):
"""队列"""
def __init__(self, maxsize=0):
"""maxsize<=0代表队列不限定大小"""
self.queue = []
self.maxsize = maxsize def is_empty(self):
return len(self.queue) == 0 def is_full(self):
if self.maxsize <= 0:
return False
else:
return len(self.queue) == self.maxsize def enqueue(self, item):
"""入队"""
if self.is_full():
raise Exception("Queue is full!")
else:
self.queue.append(item) def dequeue(self):
"""出队"""
if self.is_empty():
raise IndexError("Queue is empty!")
else:
return self.queue.pop(0) def size(self):
return len(self.queue) def get_front(self):
"""
返回队头元素
:return:
"""
if self.is_empty():
raise IndexError("Queue is empty!")
else:
return self.queue[0] def get_rear(self):
"""
返回队尾元素
:return:
"""
if self.is_empty():
raise IndexError("Queue is empty!")
else:
return self.queue[-1] def print_queue(self):
return self.queue if __name__ == "__main__":
# 实例化对象队列
queue = Queue()
# 入队四个数
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
queue.enqueue(4)
print(queue.queue) # [1, 2, 3, 4]
print(queue.is_empty()) # False 队列不为空
print(queue.is_full()) # False 队列没有到最大元素
# 出队 先入先出 出的是1 所有剩 [2,3,4]
queue.dequeue()
print(queue.queue)
# 返回队列的元素个数 3
print(queue.size())
# 获取队列的头数据 2
print(queue.get_front())
# 获取队列的尾数据 4
print(queue.get_rear())
# 打印队列所有数据 [2,3,4]
print(queue.print_queue())

打印结果为:

[1, 2, 3, 4]
False
False
[2, 3, 4]
3
2
4
[2, 3, 4]

其他类型待续。。。

队列的python实现的更多相关文章

  1. Python队列服务 Python RQ Functions from the __main__ module cannot be processed by workers.

    在使用Python队列服务 Python RQ 时候的报错: Functions from the __main__ module cannot be processed by workers. 原因 ...

  2. python基础一 ------顺序结构队列的python实现

    队列:先进先出的线性表(FIFO),只允许在一段插入并在另一端取出 以下是python实现 #-*-coding:utf-8-*- #顺序存储队列的python实现 class Queue(objec ...

  3. 数据结构之队列(Python 版)

    数据结构之队列(Python 版) 队列的特点:先进先出(FIFO) 使用链表技术实现 使用单链表技术,在表首尾两端分别加入指针,就很容易实现队列类. 使用顺序表list实现 # 队列类的实现 cla ...

  4. RbbitMQ消息队列及python实现

    1.简介 RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(亦称面向消息的中间件).RabbitMQ服务器是用Erlang语言编写的,而集群和故障转移是构建在开放电信平台框架上的 ...

  5. 【391】栈与队列,Python实现

    参考:python实现stack(栈)和队列(queue) - hjhmpl123的博客 - CSDN博客 参考:Python3 数据结构 | 菜鸟教程 栈和队列是两种基本的数据结构,同为容器类型.两 ...

  6. 栈和队列在python中的实现

    栈和队列是两种基本的数据结构,同为容器类型.两者根本的区别在于: stack:后进先出 queue:先进先出 PS:stack和queue是不能通过查询具体某一个位置的元素而进行操作的.但是他们的排列 ...

  7. Python与数据结构[2] -> 队列/Queue[0] -> 数组队列的 Python 实现

    队列 / Queue 数组队列 数组队列是队列基于数组的一种实现,其实现类似于数组栈,是一种FIFO的线性数据结构. Queue: <--| 1 | 2 | 3 | 4 | 5 |<-- ...

  8. 数据结构-循环队列(Python实现)

    今天我们来到了循环队列这一节,之前的文章中,我介绍过了用python自带的列表来实现队列,这是最简单的实现方法. 但是,我们都知道,在列表中删除第一个元素和删除最后一个元素花费的时间代价是不一样的,删 ...

  9. 06.队列、python标准库中的双端队列、迷宫问题

    class QueueUnderflow(ValueError): """队列为空""" pass class SQueue: def __ ...

随机推荐

  1. Windows下编译Google.Protobuf在Qt(C++)中使用与Unity3d(C#)交互

    1.首先从Github-Protobuf下载代码,本文下载的版本号是3.1.0. 2.仔细查看各个README,有相关的资源下载和编译说明. 3.在一个方便的地方创建一个Install类型的文件夹,放 ...

  2. 简单实现一个Unity3d的Timer

    数量使用的不太多,没有实现对象池. using System.Collections; using System.Collections.Generic; using UnityEngine; usi ...

  3. sql server和eclipse连接问题

    最近学习java,需要用的数据库sql sever,这就有一个连接问题需要设置 首先需要下载sql server,可查看我的博客sql sever下载教程: 连接教程:eclipse连接sql ser ...

  4. layui之表单验证

    这篇文章的表单验证我只是随便记录下,望各位看官理解 1.排序 验证 html代码 <div class="layui-form-item"> <label cla ...

  5. 当用命令导入csv文件时提示错误[Err] 1290 - The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

        安装之后没有my.ini配置文件怎么办,因为自己安装的是zip压缩版的mysql,所以再5.7之后就没有my.ini配置文件,所以有时候需要去自己创建一个叫my.ini的配置文件,但是特别 要 ...

  6. JS高级---案例:贪吃蛇小游戏

    案例:贪吃蛇小游戏 可以玩的小游戏,略复杂,过了2遍,先pass吧 先创建构造函数,再给原型添加方法.分别创建食物,小蛇和游戏对象. 食物,小蛇的横纵坐标,设置最大最小值,运动起来的函数,按上下左右键 ...

  7. JS生成简单随机答案选择器,小抽奖器

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. Codeforces Round #598 (Div. 3) C. Platforms Jumping

    There is a river of width nn. The left bank of the river is cell 00 and the right bank is cell n+1n+ ...

  9. Spring-Aop编程(三)-AspectJ

    AspectJ 1. 介绍 AspectJ是一个基于Java语言的AOP框架,Spring2.0以后新增了对AspectJ切点表达式支持,@AspectJ 是AspectJ1.5新增功能,通过JDK5 ...

  10. go key-value缓存go-cache实现

    Cache类型 Cache封装了一个cache类型,cache类型的参数解析: 1.defaultExpiration time.Duration 每个键值的默认过期时间. 2.items map[s ...