实现一个队列

队列的定义:

队列(Queue)是只容许在一端进行插入,而在另外一端进行删除的运算受限的线性表。java

(1)容许删除的一端称为队头(Front)。数组

(2)容许插入的一端称为队尾(Rear)。
(3)当队列中没有元素时称为空队列。
(4)队列亦称做先进先出(First In First Out)的线性表,简称为FIFO表。
   队列的修改是依先进先出的原则进行的。新来的成员老是加入队尾,每次离开的成员老是队列头上的(不容许中途离队)。app

队列的存储结构及实现

队列的顺序存储结构函数

(1) 顺序队列的定义:

 队列的顺序存储结构称为顺序队列,顺序队列其实是运算受限的顺序表。oop

(2)顺序队列的表示:

和顺序表同样,顺序队列利用内存中一段连续的存储空间来存放当前队列中的元素。
因为队列的队头和队尾的位置是变化的,设置两个指针front和rear分别指示队头元素和队尾元素,它们的初值在队列初始化时均应置为0。this

 

(3)顺序队列的基本操做


入队时:将新元素插入rear所指的位置的后一位。
出队时:删去front所指的元素,而后将front加1并返回被删元素。spa

(4)顺序表的溢出现象

 ①“下溢”现象
 当队列为空时,作出队运算产生的溢出现象。“下溢”是正常现象,经常使用做程序控制转移的条件。.net

② "真上溢"现象
当队列满时,作进栈运算产生空间溢出的现象。“真上溢”是一种出错状态,应设法避免。指针

③ "假上溢"现象
因为入队和出队操做中,头尾指针只增长不减少,导致被删元素的空间永远没法从新利用。当队列中实际的元素个数远远小于内存中本分配的空间时,也可能因为尾指针已超越向量空间的上界而不能作入队操做。该现象称为"假上溢"现象。以下图code

 循环队列:

 如上图所示,这种头尾相接的顺序存储结构称为循环队列(circular queue)。

循环队列中须要注意的几个重要问题:

①队空的断定条件,队空的条件是front=rear;

②队满的断定条件,(rear+1)%QueueSize=front。QueueSize为队列初始空间大小。

循环队列的java实现代码

[java] view plain copy

在CODE上查看代码片派生到个人代码片

  1. package study_02.datastructure.queue;  
  2.   
  3. /** 
  4.  * 循环队列 
  5.  *
  6.  */  
  7. public class CirQueue<E> {  
  8.     //对象数组,队列最多存储a.length-1个对象  
  9.     E[] a;  
  10.     //默认初始化大小  
  11.     private static final int DEFAULT_SIZE=10;  
  12.     //对首下标  
  13.     int front;  
  14.     //队尾下标  
  15.     int rear;  
  16.       
  17.     public CirQueue(){  
  18.         this(DEFAULT_SIZE);  
  19.     }  
  20.     /** 
  21.      * 初始化指定长度的队列 
  22.      * @param size 
  23.      */  
  24.     @SuppressWarnings("unchecked")  
  25.     public CirQueue(int size){  
  26.         a=(E[])(new Object[size]);  
  27.         front=0;  
  28.         rear=0;  
  29.     }  
  30.       
  31.     /** 
  32.      * 将一个对象追加到队列尾部 
  33.      * @param obj 
  34.      * @return 队列满时返回false,不然返回true 
  35.      * @author WWX 
  36.      */  
  37.     public boolean enqueue(E obj){  
  38.         if((rear+1)%a.length==front){  
  39.             return false;  
  40.         }else{  
  41.             a[rear]=obj;  
  42.             rear=(rear+1)%a.length;  
  43.             return true;  
  44.         }  
  45.     }  
  46.       
  47.     /** 
  48.      * 队列头部出队 
  49.      * @return 
  50.      * @author WWX 
  51.      */  
  52.     public E dequeue(){  
  53.         if(rear==front)  
  54.             return null;  
  55.         else{  
  56.             E obj =a[front];  
  57.             front=(front+1)%a.length;  
  58.             return obj;  
  59.         }  
  60.     }  
  61.       
  62.     /** 
  63.      * 队列长度 
  64.      * @return 
  65.      * @author WWX 
  66.      */  
  67.     public  int size(){  
  68.         return (rear-front)&(a.length-1);  
  69.     }  
  70.     //队列长度(另外一种方法)  
  71.     public int length(){  
  72.         if(rear>front){  
  73.             return rear-front;  
  74.         }else  
  75.             return a.length-1;  
  76.     }  
  77.       
  78.     /** 
  79.      * 判断是否为空  
  80.      * @return 
  81.      * @author WWX 
  82.      */  
  83.     public boolean isEmpty(){  
  84.         return rear==front;  
  85.     }  
  86.       
  87.   
  88.   
  89.     public static void main(String[] args) {  
  90.         CirQueue<String> queue=new CirQueue<String>(4);  
  91.         queue.enqueue("1");  
  92.         queue.enqueue("2");  
  93.         queue.enqueue("3");  
  94.         System.out.println("size="+queue.size());  
  95.         int size=queue.size();  
  96.         System.out.println("*******出栈操做*******");  
  97.         for(int i=0; i<size;i++){  
  98.             System.out.print(queue.dequeue()+" ");  
  99.         }  
  100.           
  101.     }  
  102.       

 

 

队列顺序存储结构实现

复制代码

public class Queue<E> {
    private Object[] data=null;
    private int maxSize; //队列容量
    private int front;  //队列头,容许删除
    private int rear;   //队列尾,容许插入

    //构造函数
    public Queue(){
        this(10);
    }
    
    public Queue(int initialSize){
        if(initialSize >=0){
            this.maxSize = initialSize;
            data = new Object[initialSize];
            front = rear =0;
        }else{
            throw new RuntimeException("初始化大小不能小于0:" + initialSize);
        }
    }
    
    //判空
    public boolean empty(){
        return rear==front?true:false;
    }
    
    //插入
    public boolean add(E e){
        if(rear== maxSize){
            throw new RuntimeException("队列已满,没法插入新的元素!");
        }else{
            data[rear++]=e;
            return true;
        }
    }
    
    //返回队首元素,但不删除
    public E peek(){
        if(empty()){
            throw new RuntimeException("空队列异常!");
        }else{
            return (E) data[front];
        }    
    }
    
    //出队
    public E poll(){
        if(empty()){
            throw new RuntimeException("空队列异常!");
        }else{
            E value = (E) data[front];  //保留队列的front端的元素的值
            data[front++] = null;     //释放队列的front端的元素                
            return value;
        }            
    }
    
    //队列长度
    public int length(){
        return rear-front;
    }
}

复制代码

循环队列的顺序存储结构实现

复制代码

import java.util.Arrays;

public class LoopQueue<E> {
    public Object[] data = null;
    private int maxSize; // 队列容量
    private int rear;// 队列尾,容许插入
    private int front;// 队列头,容许删除
    private int size=0; //队列当前长度

    public LoopQueue() {
        this(10);
    }

    public LoopQueue(int initialSize) {
        if (initialSize >= 0) {
            this.maxSize = initialSize;
            data = new Object[initialSize];
            front = rear = 0;
        } else {
            throw new RuntimeException("初始化大小不能小于0:" + initialSize);
        }
    }

    // 判空
    public boolean empty() {
        return size == 0;
    }

    // 插入
    public boolean add(E e) {
        if (size == maxSize) {
            throw new RuntimeException("队列已满,没法插入新的元素!");
        } else {
            data[rear] = e;
            rear = (rear + 1)%maxSize;
            size ++;
            return true;
        }
    }

    // 返回队首元素,但不删除
    public E peek() {
        if (empty()) {
            throw new RuntimeException("空队列异常!");
        } else {
            return (E) data[front];
        }
    }

    // 出队
    public E poll() {
        if (empty()) {
            throw new RuntimeException("空队列异常!");
        } else {
            E value = (E) data[front]; // 保留队列的front端的元素的值
            data[front] = null; // 释放队列的front端的元素
            front = (front+1)%maxSize;  //队首指针加1
            size--;
            return value;
        }
    }

    // 队列长度
    public int length() {
        return size;
    }

    //清空循环队列
    public void clear(){
        Arrays.fill(data, null);
        size = 0;
        front = 0;
        rear = 0;
    }
}

复制代码

队列链式存储结构实现

复制代码

public class LinkQueue<E> {
    // 链栈的节点
    private class Node<E> {
        E e;
        Node<E> next;

        public Node() {
        }

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }
    }
    
    private Node front;// 队列头,容许删除  
    private Node rear;// 队列尾,容许插入  
    private int size; //队列当前长度 
    
    public LinkQueue() {
        front = null;
        rear = null;
    }
    
    //判空
      public boolean empty(){
          return size==0;
      }
      
      //插入
      public boolean add(E e){
          if(empty()){    //若是队列为空
              front = new Node(e,null);//只有一个节点,front、rear都指向该节点
              rear = front;
          }else{
              Node<E> newNode = new Node<E>(e, null);
              rear.next = newNode; //让尾节点的next指向新增的节点
              rear = newNode; //以新节点做为新的尾节点
          }
          size ++;
          return true;
      }
      
      //返回队首元素,但不删除
      public Node<E> peek(){
          if(empty()){
              throw new RuntimeException("空队列异常!");
          }else{
              return front;
          }
      }
      
      //出队
      public Node<E> poll(){
          if(empty()){
              throw new RuntimeException("空队列异常!");
          }else{
              Node<E> value = front; //获得队列头元素
              front = front.next;//让front引用指向原队列头元素的下一个元素
              value.next = null; //释放原队列头元素的next引用
              size --;
              return value;
          }        
      }
      
      //队列长度
      public int length(){
          return size;
      }
}

复制代码

基于LinkedList实现队列结构

复制代码

/**
 * 使用java.util.Queue接口,其底层关联到一个LinkedList(双端队列)实例.
 */
import java.util.LinkedList;
import java.util.Queue;

public class QueueList<E> {
    private Queue<E> queue = new LinkedList<E>();
    
    // 将指定的元素插入此队列(若是当即可行且不会违反容量限制),在成功时返回 true,
    //若是当前没有可用的空间,则抛出 IllegalStateException。
    public boolean add(E e){
        return queue.add(e);
    }
    
    //获取,可是不移除此队列的头。
    public E element(){
        return queue.element();
    }
    
    //将指定的元素插入此队列(若是当即可行且不会违反容量限制),当使用有容量限制的队列时,
    //此方法一般要优于 add(E),后者可能没法插入元素,而只是抛出一个异常。
    public boolean offer(E e){
        return queue.offer(e);
    }
    
    //获取但不移除此队列的头;若是此队列为空,则返回 null
    public E peek(){
        return queue.peek();
    }
    
    //获取并移除此队列的头,若是此队列为空,则返回 null
    public E poll(){
        return queue.poll();
    }
    
    //获取并移除此队列的头
    public E remove(){
        return queue.remove();
    }
    
    //判空
    public boolean empty() {
        return queue.isEmpty();
    }
}
相关文章
相关标签/搜索