java資料結構–隊列

來源:互聯網
上載者:User

1. 用鏈表實現單向隊列

package com.jzm.stackQueueTree;public class LinkQueue<T>{   //鏈表實現單向隊列 private Node firstNode; private Node lastNode;   public LinkQueue(){     firstNode = null; lastNode = null;  }//end constructor  private class Node{ //內部節點      private  T data;     private  Node next;   private  Node(T data){    this.data = data;    this.next = null;    }//end constructor  private  T getData(){   return data;  }   private Node getNextNode(){   return next; }           private  void  setNextNode(Node n){      this.next =  n;       }  }//end class Node     private  boolean isEmpty(){         return  firstNode == null;  }//end isEmpty 判斷是否為空白  public void enqueue(T newEntry){  Node newNode = new Node(newEntry);    if (isEmpty()){
  firstNode = newNode;  lastNode  = newNode; }else{     lastNode.setNextNode(newNode);     lastNode = newNode;   }       } //end enqueue 從隊尾加入新元素    public T getFront(){  //檢索前端元素  T front = null;  if(!isEmpty())   front = firstNode.data; return front;  }//從隊列前端取元素     public T deletequeue(){ T front = null; if (!isEmpty()){ front = firstNode.getData(); firstNode = firstNode.getNextNode();  }  if (firstNode ==null) { //如果只有一個元素,刪除了為空白的話,尾節點應該也為空白  lastNode = null; }   return front; }//end deletequeue   public void clear(){ firstNode = null; lastNode = null;     } //end clear   public void display(){  if(isEmpty()){ System.out.println("隊列為空白"); }else {Node point = firstNode;while (point != null) {    System.out.println(point.getData());point = point.getNextNode();  } }  }   public static void main(String[] args) {          LinkQueue<Integer> linkQueue = new LinkQueue<Integer>();          linkQueue.enqueue(1);       linkQueue.enqueue(2);       linkQueue.enqueue(3);       linkQueue.display();       linkQueue.deletequeue();       linkQueue.deletequeue();       linkQueue.deletequeue();       linkQueue.display(); } }

2.   用數組實現隊列

 

package com.jzm.stackQueueTree;public class ArrayQueue<T> {     /**    * 用可變數組,實現迴圈隊列    */ private T [] queue; private int frontIndex; private int backIndex; private static  final  int  maxsize = 3;  //可以容納的大小   public ArrayQueue(){  queue = (T[])new Object[maxsize]; frontIndex = -1; backIndex =  -1;  }     public ArrayQueue(int  initsize){  queue = (T[]) new Object[initsize+1]; frontIndex = -1; backIndex =  -1;  }//end constructor   private boolean isEmpty(){   return ((frontIndex+backIndex==-2) && frontIndex==(backIndex%queue.length)); }// 判斷隊列是否為空白     private boolean isArrayFull(){    return  frontIndex == ((backIndex+1)%queue.length);    }//end full 判斷數組是否滿    public void enqueue(T newEntry){   if(isEmpty()){   System.out.println("為空白此時frontIndex="+frontIndex);   System.out.println("為空白此時backIndex="+backIndex);   frontIndex = (frontIndex+1)% queue.length;   queue[frontIndex] = newEntry;      backIndex = (backIndex+1)%queue.length;        }else{      if (isArrayFull()) {  System.out.println("加入"+newEntry+"時,隊列已經滿,等待分配");  System.out.println("滿此時frontIndex="+frontIndex);  System.out.println("滿此時backIndex="+backIndex);        doubleArray();         }                backIndex = (backIndex+1)%queue.length;        queue[backIndex] = newEntry;  } }  //end enqueue   public T getFront(){ T front = null; if (!isEmpty()) { front = queue[frontIndex]; }    return front; }//end getFront  得到隊列頭結點   public T  deletequeue(){  T  front  = null;  if (!isEmpty()) {front = queue[frontIndex];queue[frontIndex] = null;frontIndex = (frontIndex+1)%queue.length; }//end if    return front;  }//end deletequeue;刪除隊列頭結點   public void doubleArray(){  T [] oldQueue =  queue; int  oldsize =    queue.length;  System.out.println("oldsize=" + oldsize);  queue = (T[])new Object[2 * oldsize];  for(int i=0;i<oldsize;i++){  queue[i] = oldQueue[frontIndex];  frontIndex = (frontIndex+1)%oldsize;   }//end for frontIndex= 0; backIndex = oldsize-1;     }// end doubleArray  public void  display(){  if (frontIndex < backIndex){ //說明沒有迴圈     for(int i=frontIndex; i<=backIndex; i++)             System.out.println(queue[i]); }else{       for (int i = frontIndex; i < queue.length; i++) {  System.out.println(queue[i]); }       for (int i = 0; i <=backIndex;i++) {   System.out.println(queue[i]);}    } }  //輸出隊列的值  public static void main(String[] args) {        ArrayQueue<Integer> arrayQueue = new ArrayQueue<Integer>();     arrayQueue.enqueue(1);     arrayQueue.enqueue(2);          arrayQueue.enqueue(3);     arrayQueue.enqueue(4);     arrayQueue.enqueue(5);     arrayQueue.enqueue(6);     arrayQueue.enqueue(7);     arrayQueue.deletequeue();     arrayQueue.deletequeue();     arrayQueue.enqueue(8);          System.out.println("------------------");     arrayQueue.display();      }  }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.