队列是一种线性数据结构,是一种运算受限的线性表,只允许在队尾插入,在队头删除。运算规则是先进先出。恰好和栈相反。栈是先进后出。因为栈只在栈顶做删除和插入。

队列按照存储结构可以分为顺序队列和链式队列。顺序队列采用数组实现,链式队列采用节点的方式实现。

//顺序队列

 package queue;
//1.队列是一种运算受限的线性表,运算规则是先进先出。只能在队头和队尾进行操作
//2.队列由数据域,队头,队尾组成
//3.队列在队尾进行插入操作,在队头进行删除操作
public class Queue<E> {
private Object[] data = null; //数据域
private int front = 0; //队头,允许删除
private int rear = 0; //队尾,允许插入
private int maxSize = 0; //队列容量 //初始化队列
public Queue(){
this(10);
} public Queue(int initialSize) {
if(initialSize >= 0){
this.data = new Object[initialSize]; //初始化数据域
this.maxSize = initialSize;
}else {
throw new RuntimeException("初始化大小不能小于0:" + initialSize);
}
} //判断队列是否为空
public boolean isEmpty(){
return this.front == this.rear; //如果队头队尾相等说明为空
} //判断队列是否已满
public boolean isMaxSize(){
return this.rear == this.maxSize ? true : false; //如果队尾指针大于最大容量,说明队列已经满了
} //入队,从队尾插入,从队头删除
public boolean push(E e){
//判断队列是否已满
if(this.isMaxSize()){
System.err.println("队列已满,无法插入");
return false;
}
data[rear] = e; //将元素插入到队尾 data[0] = 插入值
rear++ ; //更新队尾指针
return true;
} //弹出队列
@SuppressWarnings("unchecked")
public E pop(){
//判断是否为空,为空将无法出队
if(isEmpty()){
System.err.println("队列为空,无法插入");
return null;
}
//在队头弹出
E e = (E) data[front]; //对头引用
data[front] = null; //将弹出的对头内存销毁
front ++; //更新队头指针为后面的一位元素
return e;
} //返回队首元素,但不删除
@SuppressWarnings("unchecked")
public E peek(){
if(isEmpty()){
System.err.println("队列为空");
return null;
} return (E) this.data[front];
} //遍历队列
public void display(){
while(front < rear){
System.out.println(data[front]);
front ++;
}
} //返回队列实际长度
public int getSize(){
return this.data.length;
} public static void main(String args[]){
Queue<Integer> queue = new Queue<Integer>();
int i = 0;
while(i < 10){
queue.push(i);
i ++;
}
// queue.pop();
queue.display();
} }

//链式队列

 package queue;

 //链式队列,由队头和队尾节点组成,节点中包含数据域和指针域
public class LinkedQueue<E> {
//1.定义队列结构
//节点类
@SuppressWarnings("hiding")
private class Node<E>{
public E data = null; //数据域
public Node<E> next = null; //指针域 //构造方法
@SuppressWarnings("unused")
public Node(){} public Node(E data, Node<E> next){
this.data = data;
this.next = next;
} }/*Node*/ private Node<E> front = null; //队头
private Node<E> rear = null; //队尾
private int size = 0; //队列长度 //2.判断队列是否为空
public boolean isEmpty(){
return size == 0;
} //3.入队
public boolean push(E e){
Node<E> node = new Node<E>(e, null);
//队列为空的情况下
if(isEmpty()){
this.rear = node; //尾节点赋值为新插入的节点
this.front = this.rear; //在只有一个节点的情况下,头尾节点相等
size++;
return true;
}
//不为空的情况下
this.rear.next = node; //尾节点指向新节点
this.rear = node; //更新尾节点
size ++; //队列节点数+1
return true;
} //4.出队
public E pop(){
//判断队列是否为空
if(isEmpty()){
System.err.println("队列为空");
return null;
}
//弹出队头,更新队头指针
Node<E> temp = this.front; //获取队头引用
this.front = this.front.next; //更新队头指针
temp.next = null; //释放原队头节点引用
return temp.data; } //5.返回队头但不删除
public E peek(){
//判断是否为空
if(isEmpty()){
System.err.println("为空,不能返回");
return null;
}
return this.front.data;
} //6.求长
public int getSize(){
return this.size;
} //7.打印队列
public void display(){
for (int i = 0; i < this.size; i++) {
if(this.front == null){
return;
}
System.out.println(this.front.data);
this.front = this.front.next;
}
} public static void main(String args[]){
LinkedQueue<Integer> queue = new LinkedQueue<Integer>();
//1.入队
int i = 0;
while(i < 10){
queue.push(i);
i++;
} //出队
int j = 0;
while(j < 8){
System.out.println(queue.pop());
j ++;
} // queue.display(); } }

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

  1. java中使用队列:java.util.Queue (转)

    Queue接口与List.Set同一级别,都是继承了Collection接口.LinkedList实现了Queue接 口.Queue接口窄化了对LinkedList的方法的访问权限(即在方法中的参数类 ...

  2. 队列-java代码

    public class QueueDemo { private int maxSize; private long[] queueArray; // 队列的头,实际是数组的尾 private int ...

  3. java中使用队列:java.util.Queue

    在java5中新添加了java.util.Queue接口,用以支持队列的常见操作.该接口扩展了java.util.Collection接口.Queue使用时要尽量避免Collection的add()和 ...

  4. 阻塞队列 - java基于链表的简单实现

    1.阻塞队列的原理 阻塞队列与普通队列的区别在于:阻塞队列为空时,从队列中获取元素的操作将会被阻塞,当队列为满时,往队列里添加元素的操作会被阻塞. 试图从空的阻塞队列中获取元素的线程将会被阻塞,直到其 ...

  5. 剑指offer【05】- 用两个栈实现队列(java)

    题目:用两个栈实现队列 考点:栈和队列 题目描述:用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 解题思路:每次psuh是时先将stack2清空放入stck1(保 ...

  6. java中使用队列:java.util.Queue(转)

    队列是一种特殊的线性表,是运算受到限制的一种线性表,只允许在表的一端进行插入,而在另一端进行删除元素的线性表.队尾(rear)是允许插入的一端.队头(front)是允许删除的一端.空队列是不含元素的空 ...

  7. 网站架构:消息队列 Java后端架构

    2017-01-13  一.消息队列概述 消息队列中间件是分布式系统中重要的组件,主要解决应用耦合,异步消息,流量削锋等问题.实现高性能,高可用,可伸缩和最终一致性架构.是大型分布式系统不可缺少的中间 ...

  8. redis实现消息队列-java代码实现

    转:https://blog.csdn.net/qq_42175986/article/details/88576849 pom.xml <!-- redis依赖 --> <depe ...

  9. 05.用两个栈实现队列 Java

    题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路 进栈: 直接进stack1 出栈: 若stack2不为空,则出栈. 否则,当stack1不为空时, ...

  10. (超详细)动手编写 — 栈、队列 ( Java实现 )

    目录 前言 栈 概念 栈的设计 编码实现 小结 队列 概念 队列的设计 编码实现 双端队列 概念 设计 编码 循环队列 循环队列 循环双端队列 声明 前言 栈 概念 什么是栈? **栈 **:是一种特 ...

随机推荐

  1. CSS快速入门-箭头和图标

    一.三步搞懂箭头产生的原理 在前面的盒子模型一文中,我们已经知道了一个元素空间占位.为了弄明白箭头的产生,我们可以三步走: #demo12 { border: 100px solid; border- ...

  2. SpringBoot日记——SpringMvc自动配置与扩展篇

    为了让SpringBoot保持对SpringMVC的全面支持和扩展,而且还要维持SpringBoot不写xml配置的优势,我们需要添加一些简单的配置类即可实现: 通常我们使用的最多的注解是: @Bea ...

  3. Web开发框架趋势

    Node.js增长很快,已经冒尖了 ASP.NET MVC 发展平稳(平稳很重要) Spring MVC沾着Spring的光,渐渐超越了Struts 2 Struts作为一个整体(Struts 1 和 ...

  4. 北美KubeCon新风,正把K8S魔力带向边缘计算

    作者:DJ 审校:Kevin·Wang 1. 容器生态圈新的创新方向 2018年容器技术圈的年终盛典北美KubeCon终于在西雅图落下了帷幕.这次北美KubeCon总共吸引了8000多观众参会,创下历 ...

  5. 【TestNG测试】TestNG、Maven、testng.xml构建测试工程

    创建一个maven工程 使用Idea创建maven工程 ​ 建立类似如上的工程结构,src/main/java,src/test/java,pom.xml,testng.xml,这里由于我们使用工程是 ...

  6. CF刷题-Codeforces Round #481-F. Mentors

    题目链接:https://codeforces.com/contest/978/problem/F 题目大意: n个程序员,k对仇家,每个程序员有一个能力值,当甲程序员的能力值绝对大于乙程序员的能力值 ...

  7. 【RabbitMQ】三种Exchange模式——订阅、路由、通配符模式

    https://blog.csdn.net/ww130929/article/details/72842234

  8. C#例题集

    收集一些从网上看到的例题 1.抽象类 抽象类不能被实例化一个抽象类只能通过接口和作为其它类的基类使用 抽象方法的声明只能在抽象类中 抽象方法必定不能实现(方法带一对{}都不行) 当一个子类集成自抽象类 ...

  9. python-gevent模块实现socket大并发

    服务器端:gevent_server.py 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  10. 10.openldap备份与恢复

    备份方式 一.使用slapcat指令备份 使用slapcat备份后的数据 经过相关无用条目处理,即可实现数据上的条目备份 备份指令如下 #备份 #slapcat -v -l openldap-back ...