1.Queue接口:

  1. public interface Queue<E> {
  2. int getSize();
  3. boolean isEmpty();
  4. void enqueue(E e);
  5. E dequeue();
  6. E getFront();
  7. }

2.使用Array数组实现ArrayQueue:

  1. public class ArrayQueue<E> implements Queue<E> {
  2.  
  3. private Array<E> array;
  4.  
  5. public ArrayQueue(int capacity){
  6. array = new Array<>(capacity);
  7. }
  8.  
  9. public ArrayQueue(){
  10. array = new Array<>();
  11. }
  12.  
  13. @Override
  14. public int getSize(){
  15. return array.getSize();
  16. }
  17.  
  18. @Override
  19. public boolean isEmpty(){
  20. return array.isEmpty();
  21. }
  22.  
  23. public int getCapacity(){
  24. return array.getCapacity();
  25. }
  26.  
  27. @Override
  28. public void enqueue(E e){
  29. array.addLast(e);
  30. }
  31.  
  32. @Override
  33. public E dequeue(){
  34. return array.removeFirst();
  35. }
  36.  
  37. @Override
  38. public E getFront(){
  39. return array.getFirst();
  40. }
  41.  
  42. @Override
  43. public String toString(){
  44. StringBuilder res = new StringBuilder();
  45. res.append("Queue: ");
  46. res.append("front [");
  47. for(int i = 0 ; i < array.getSize() ; i ++){
  48. res.append(array.get(i));
  49. if(i != array.getSize() - 1)
  50. res.append(", ");
  51. }
  52. res.append("] tail");
  53. return res.toString();
  54. }
  55.  
  56. public static void main(String[] args){
  57. ArrayQueue<Integer> queue = new ArrayQueue<>();
  58. for(int i = 0 ; i < 10 ; i ++){
  59. queue.enqueue(i);
  60. System.out.println(queue);
  61. if(i % 3 == 2){
  62. queue.dequeue();
  63. System.out.println(queue);
  64. }
  65. }
  66. }
  67. }

3.使用LinkedList链表实现LinkedListQueue:

  1. public class LinkedListQueue<E> implements Queue<E> {
  2.  
  3. private class Node{
  4. public E e;
  5. public Node next;
  6.  
  7. public Node(E e, Node next){
  8. this.e = e;
  9. this.next = next;
  10. }
  11.  
  12. public Node(E e){
  13. this(e, null);
  14. }
  15.  
  16. public Node(){
  17. this(null, null);
  18. }
  19.  
  20. @Override
  21. public String toString(){
  22. return e.toString();
  23. }
  24. }
  25.  
  26. private Node head, tail;
  27. private int size;
  28.  
  29. public LinkedListQueue(){
  30. head = null;
  31. tail = null;
  32. size = 0;
  33. }
  34.  
  35. @Override
  36. public int getSize(){
  37. return size;
  38. }
  39.  
  40. @Override
  41. public boolean isEmpty(){
  42. return size == 0;
  43. }
  44.  
  45. @Override
  46. public void enqueue(E e){
  47. if(tail == null){
  48. tail = new Node(e);
  49. head = tail;
  50. }
  51. else{
  52. tail.next = new Node(e);
  53. tail = tail.next;
  54. }
  55. size ++;
  56. }
  57.  
  58. @Override
  59. public E dequeue(){
  60. if(isEmpty())
  61. throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
  62. Node retNode = head;
  63. head = head.next;
  64. retNode.next = null;
  65. if(head == null)
  66. tail = null;
  67. size --;
  68. return retNode.e;
  69. }
  70.  
  71. @Override
  72. public E getFront(){
  73. if(isEmpty())
  74. throw new IllegalArgumentException("Queue is empty.");
  75. return head.e;
  76. }
  77.  
  78. @Override
  79. public String toString(){
  80. StringBuilder res = new StringBuilder();
  81. res.append("Queue: front ");
  82. Node cur = head;
  83. while(cur != null) {
  84. res.append(cur + "->");
  85. cur = cur.next;
  86. }
  87. res.append("NULL tail");
  88. return res.toString();
  89. }
  90.  
  91. public static void main(String[] args){
  92. LinkedListQueue<Integer> queue = new LinkedListQueue<>();
  93. for(int i = 1 ; i < 11 ; i ++){
  94. queue.enqueue(i);
  95. System.out.println(queue);
  96. if(i % 3 == 0){
  97. queue.dequeue();
  98. System.out.println(queue);
  99. }
  100. }
  101. }
  102. }

4.实现循环队列LoopQueue:

  1. public class LoopQueue<E> implements Queue<E> {
  2.  
  3. private E[] data;
  4. private int front, tail;
  5. private int size;
  6.  
  7. public LoopQueue(int capacity){
  8. data = (E[])new Object[capacity + 1];
  9. front = 0;
  10. tail = 0;
  11. size = 0;
  12. }
  13.  
  14. public LoopQueue(){
  15. this(10);
  16. }
  17.  
  18. public int getCapacity(){
  19. return data.length - 1;
  20. }
  21.  
  22. @Override
  23. public boolean isEmpty(){
  24. return front == tail;
  25. }
  26.  
  27. @Override
  28. public int getSize(){
  29. return size;
  30. }
  31.  
  32. @Override
  33. public void enqueue(E e){
  34. if((tail + 1) % data.length == front)
  35. resize(getCapacity() * 2);
  36. data[tail] = e;
  37. tail = (tail + 1) % data.length;
  38. size ++;
  39. }
  40.  
  41. @Override
  42. public E dequeue(){
  43. if(isEmpty())
  44. throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
  45. E ret = data[front];
  46. data[front] = null;
  47. front = (front + 1) % data.length;
  48. size --;
  49. if(size == getCapacity() / 4 && getCapacity() / 2 != 0)
  50. resize(getCapacity() / 2);
  51. return ret;
  52. }
  53.  
  54. @Override
  55. public E getFront(){
  56. if(isEmpty())
  57. throw new IllegalArgumentException("Queue is empty.");
  58. return data[front];
  59. }
  60.  
  61. private void resize(int newCapacity){
  62. E[] newData = (E[])new Object[newCapacity + 1];
  63. for(int i = 0 ; i < size ; i ++)
  64. newData[i] = data[(i + front) % data.length];
  65. data = newData;
  66. front = 0;
  67. tail = size;
  68. }
  69.  
  70. @Override
  71. public String toString(){
  72. StringBuilder res = new StringBuilder();
  73. res.append(String.format("Queue: size = %d , capacity = %d\n", size, getCapacity()));
  74. res.append("front [");
  75. for(int i = front ; i != tail ; i = (i + 1) % data.length){
  76. res.append(data[i]);
  77. if((i + 1) % data.length != tail)
  78. res.append(", ");
  79. }
  80. res.append("] tail");
  81. return res.toString();
  82. }
  83.  
  84. public static void main(String[] args){
  85. LoopQueue<Integer> queue = new LoopQueue<>(5);
  86. for(int i = 0 ; i < 10 ; i ++){
  87. queue.enqueue(i);
  88. System.out.println(queue);
  89. if(i % 3 == 2){
  90. queue.dequeue();
  91. System.out.println(queue);
  92. }
  93. }
  94. }
  95. }

Queue实现的更多相关文章

  1. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  2. Azure Queue Storage 基本用法 -- Azure Storage 之 Queue

    Azure Storage 是微软 Azure 云提供的云端存储解决方案,当前支持的存储类型有 Blob.Queue.File 和 Table. 笔者在<Azure File Storage 基 ...

  3. C++ std::queue

    std::queue template <class T, class Container = deque<T> > class queue; FIFO queue queue ...

  4. 初识Message Queue之--基础篇

    之前我在项目中要用到消息队列相关的技术时,一直让Redis兼职消息队列功能,一个偶然的机会接触到了MSMQ消息队列.秉着技术还是专业的好为原则,对MSMQ进行了学习,以下是我个人的学习笔记. 一.什么 ...

  5. 搭建高可用的rabbitmq集群 + Mirror Queue + 使用C#驱动连接

    我们知道rabbitmq是一个专业的MQ产品,而且它也是一个严格遵守AMQP协议的玩意,但是要想骚,一定需要拿出高可用的东西出来,这不本篇就跟大家说 一下cluster的概念,rabbitmq是erl ...

  6. PriorityQueue和Queue的一种变体的实现

    队列和优先队列是我们十分熟悉的数据结构.提供了所谓的“先进先出”功能,优先队列则按照某种规则“先进先出”.但是他们都没有提供:“固定大小的队列”和“固定大小的优先队列”的功能. 比如我们要实现:记录按 ...

  7. C#基础---Queue(队列)的应用

       Queue队列,特性先进先出. 在一些项目中我们会遇到对一些数据的Check,如果数据不符合条件将会把不通过的信息返回到界面.但是对于有的数据可能会Check很多条件,如果一个数据一旦很多条件不 ...

  8. [LeetCode] Queue Reconstruction by Height 根据高度重建队列

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  9. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  10. 源码之Queue

    看源码可以把python看得更透,更懂,想必也是开发人员的必经之路. 现在有个任务,写个线程池.使用Queue就能写一个最简单的,下面就来学学Queue源码. 源码之Queue: class Queu ...

随机推荐

  1. Docker——概述

    出现原因:开发接替运维的工作,将jar包连同(mysql,jdk)等环境上线 实现:java -> jar(环境) -> 打包项目带上环境(镜像) -> (Docker仓库:商店) ...

  2. Linux巡检检查项

    不定时更新...... 1)服务器 1.1 SELINUX检查(sestatus) 1.2 资源限制检查(ulimit -a) 1.3 最近登录(last) 1.4 操作系统版本(cat /etc/r ...

  3. python写一个web目录扫描器

    用到的模块urliib error #coding = utf-8 #web目录扫描器 by qianxiao996 #博客地址:https://blog.csdn.net/qq_36374896 i ...

  4. 变量 Java day 5

    Java 第五天的学习 变量 变量注意事项 变量的底层 ASCII编码表 1.什么是变量? 概念:变量及代数. 在Java中,变量分为两种:基本类型的变量和引用类型的变量 1>基本类型的变量:必 ...

  5. Linux开发板(树莓派)和服务器进行双向通信(socket)

    前言 ​ 物联网是目前嵌入开发必备的属性之一,我们常常需要把自己开发板和云端进行交互,本篇博文就记录一下. ​ 使用Socket来实现Linux开发板和服务器进行双向通信,Python中是默认集成了s ...

  6. kafka 保证消息被消费和消息只消费一次

    1. 保证消息被消费 即使消息发送到了消息队列,消息也不会万无一失,还是会面临丢失的风险. 我们以 Kafka 为例,消息在Kafka 中是存储在本地磁盘上的, 为了减少消息存储对磁盘的随机 I/O, ...

  7. (转)Linux的文件权限与目录配置

    ref:https://www.cnblogs.com/ysocean/p/7712412.html#_label1_0 (转)Linux文件权限和目录配置 1.Linux命令的普遍语法格式 命令格式 ...

  8. SpringBoot与SpringCloud的关系与区别?

    一.SpringBoot和SpringCloud简介 1.SpringBoot:是一个快速开发框架,通过用MAVEN依赖的继承方式,帮助我们快速整合第三方常用框架,完全采用注解化(使用注解方式启动Sp ...

  9. linux发布常用命令

    一.linux发布常用命令 //启动Tomcat sh /opt/apache-tomcat-8.5.29/bin/startup.sh //停止tomcat sh /opt/apache-tomca ...

  10. Redis++:Redis做分布式锁真的靠谱吗

    Redis做分布式锁真的靠谱吗 Redis的分布式锁可以通过Lua进行实现,通过setnx和expire命令连用的方式 || 也可以使用高版本的方法同时设置失效时间,但是假如在以下情况下,就会造成无锁 ...