十一 队列 Queue
队列: 一种先进先出的数据结构 FIFO
数组队列的实现:
队列的实现:
package com.lt.datastructure.Queue;
public class ArrayQueue<E> implements Queue<E> {
private Array1<E> array;
public ArrayQueue(int capacity){
array = new Array1<>(capacity);
}
public ArrayQueue(){
array = new Array1<>();
}
@Override
public int getSize() {
return array.getSize();
}
@Override
public boolean isEmpty() {
return array.isEmpty();
}
@Override
public void enqueue(E e) {
array.addLast(e);
}
@Override
public E dequeue() {
return array.removeFirst();
}
@Override
//查看队首
public E getFront(E e) {
return array.getFirst();
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Queue:");
res.append("front:[");
for(int i=0; i<array.getSize();i++){
res.append(array.get(i));
if(i!=array.getSize()-1){
res.append(",");
}
}
res.append("] tail");
return res.toString();
}
public static void main(String[] args) {
ArrayQueue<Integer> queue = new ArrayQueue<>();
for(int i =0; i<10; i++){
queue.enqueue(i);
System.out.println(queue);
if(i%3==2){
queue.dequeue();
System.out.println(queue);
}
}
}
}
测试:

复杂度;
dequeue:出列操作时,移除队首,其余元素向前移一个位置 O(n)
循环队列:犹如钟表
- front指向队首,tail指向队尾
- 出队时,移动front的指向。
- font==tail时,队列为空
- (tail+1)%Capacity=front,队列满,会有一个元素浪费

循环队列复杂度分析:

循环队列的实现:
package com.lt.datastructure.Queue;
import com.lt.datastructure.Array.Array;
/*
* 循环队列的实现
*/
public class LoopQueue<E> implements Queue<E> {
private int front,tail;
private int size;
E[] data;
public LoopQueue(int capacity){
data = (E[])new Object[capacity+1];
front = 0;
tail = 0;
size = 0;
}
public LoopQueue(){
this(10);
}
//由于空出一个空间,所以容量-1
public int getCapacity() {
return data.length-1;
}
@Override
public boolean isEmpty() {
return front==tail;
}
@Override
public int getSize() {
return size;
}
@Override
public void enqueue(E e) {
//队列满,扩容
if((tail+1) % data.length ==front){
resize(getCapacity()*2);
}
data[tail] = e;
tail = (tail+1)%data.length;
size++;
}
private void resize(int newCapacity) {
E[] newData =(E[])new Object[newCapacity+1];
for(int i=0; i < size; i++){
newData[i] = data[(i+front)%data.length];
}
data = newData;
front = 0;
tail=size;
}
@Override
public E dequeue() {
//查看的队列是否为空
if(isEmpty()){
throw new IllegalAccessError("cannot dequeue from an empty queue.");
}
E ret = data[front];
data[front] = null;
front = (front+1)%data.length;
size--;
if(size==getCapacity()/4 && getCapacity()/2!=0){
resize(getCapacity()/2);
}
return ret;
}
@Override
public E getFront(E e) {
//查看的队列是否为空
if(isEmpty()){
throw new IllegalAccessError("cannot getFront from an empty queue.");
}
return data[front];
}
@Override//覆盖父类方法
public String toString(){
StringBuilder res = new StringBuilder();
res.append(String.format("LoopQueue:size = %d , capacity = %d\n",size,getCapacity()));
res.append("front [");
for(int i = front; i!=tail ; i=(i+1)%data.length){
res.append(data[i]);
if((i+1)%data.length != tail)
res.append(",");
}
res.append("]");
return res.toString();
}
public static void main(String[] args) {
LoopQueue<Integer> queue = new LoopQueue<>();
for(int i =0; i<10; i++){
queue.enqueue(i);
System.out.println(queue);
if(i%3==2){
queue.dequeue();
System.out.println(queue);
}
}
}
}
测试用例:
package com.lt.datastructure.Queue;
import java.util.Random;
public class Main {
/*
* 测试ArrayQueue和LoopQueue
*/
private static double testQueue(Queue<Integer> q , int opCount){
long startTime = System.nanoTime();
//..
Random random = new Random();
for(int i =0; i<opCount;i++){
q.enqueue(random.nextInt(Integer.MAX_VALUE));
}
for(int i=0; i<opCount; i++){
q.dequeue();
}
long endTime = System.nanoTime();
return (endTime-startTime)/1000000000.0;
}
public static void main(String[] args){
int opCount = 100000;
ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
System.out.println("ArrayQueue:"+testQueue(arrayQueue,opCount));
LoopQueue<Integer> loopQueue = new LoopQueue<>();
System.out.println("LoopQueue:"+testQueue(loopQueue, opCount));
}
}
测试结果:

十一 队列 Queue的更多相关文章
- jQuery 源码分析(十一) 队列模块 Queue详解
队列是常用的数据结构之一,只允许在表的前端(队头)进行删除操作(出队),在表的后端(队尾)进行插入操作(入队).特点是先进先出,最先插入的元素最先被删除. 在jQuery内部,队列模块为动画模块提供基 ...
- Python进阶【第二篇】多线程、消息队列queue
1.Python多线程.多进程 目的提高并发 1.一个应用程序,可以有多进程和多线程 2.默认:单进程,单线程 3.单进程,多线程 IO操作,不占用CPU python的多线程:IO操作,多线程提供并 ...
- Java中的队列Queue,优先级队列PriorityQueue
队列Queue 在java5中新增加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口. Queue使用时要尽量避免Collecti ...
- jquery 的队列queue
使用示列代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...
- Windows Azure Service Bus (2) 队列(Queue)入门
<Windows Azure Platform 系列文章目录> Service Bus 队列(Queue) Service Bus的Queue非常适合分布式应用.当使用Service Bu ...
- Windows Azure Service Bus (3) 队列(Queue) 使用VS2013开发Service Bus Queue
<Windows Azure Platform 系列文章目录> 在之前的Azure Service Bus中,我们已经介绍了Service Bus 队列(Queue)的基本概念. 在本章中 ...
- (C#)使用队列(Queue)解决简单的并发问题
(C#)使用队列(Queue)解决简单的并发问题 2015-07-16 13:04 13265人阅读 评论(8) 收藏 举报 分类: Asp.Net(8) 版权声明:本文为博主原创文章,未经博主允 ...
- STL中的单向队列queue
转载自:http://blog.csdn.net/morewindows/article/details/6950917 stl中的queue指单向队列,使用时,包含头文件<queue>. ...
- java09 队列Queue与Deque
队列Queue与Deque. Enumeration Hashtable与Hashtable子类Properties(资源配置文件) 引用类型(强.软.弱.虚)与WeakHashMap Identit ...
随机推荐
- hadoop中block副本的放置策略
下面的这种是针对于塔式服务器的副本的放置策略
- Django项目配置数据库时,已安装mysqlclient,却提示 Did you install mysqlclient错误,后右报错ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3
错误信息如下: 解决方案是: 找到自己的项目文件夹下的__init__.py 添加如下代码 解决这个问题后,右报错django2.2/mysql ImproperlyConfigured: mysq ...
- EF 查询表达式 join
数据源: 1.无into,相当于 inner join var query = from p in context.P join s in context.S on p.PID equals s.PI ...
- 移除微信昵称中的emoji字符
移除微信昵称中的emoji字符: /** * 移除微信昵称中的emoji字符 * @param type $nickname * @return type */ function removeEmoj ...
- 「题解」「JOISC 2014 Day1」历史研究
目录 题目 考场思考 思路分析及标程 题目 点这里 考场思考 大概是标准的莫队吧,离散之后来一个线段树加莫队就可以了. 时间复杂度 \(\mathcal O(n\sqrt n\log n)\) . 然 ...
- 《Web安全攻防 渗透测试实战指南》 学习笔记 (四)
Web安全攻防 渗透测试实战指南 学习笔记 (四) Nmap Network Mapper 是一款开放源代码的网 ...
- java代码向kafka集群发送消息报org.apache.kafka.common.errors.TimeoutException: Batch Expired
由于项目是springboot项目,在application.properties加入 logging.level.root=debug debug日志报错信息为kafka集群ip别名访问失败 在wi ...
- ubuntu循环登录
ubuntu12.04管理员账户登录不了桌面,只能客人会话登录 登录管理员账户时,输入密码后,一直在登录界面循环 费了好大劲啊,一上午的时间,终于搞定了,哈哈哈 ctrl+alt+f1 ,切换到tty ...
- EAP认证
EAP信息交换: 上图中展示的是OTP(一次性密码)实现EAP交换过程,具体的EAP交换过程如下: 步骤1:请求方向认证方发送EAPOL-Start消息,通知对方已经做到了认证准备(注意:若会话由认证 ...
- 学习笔记(5)- ubuntu对话语料
The Ubuntu Dialogue Corpus: A Large Dataset for Research in Unstructured Multi-Turn Dialogue Systems ...