學習java資料結構基礎知識之隊列

來源:互聯網
上載者:User

標籤:java資料結構   資料結構   隊列   

隊列是先進先出。
利用java語言實現隊列代碼:
/* * 隊列 */public class Queue {private int maxSize;          //最大隊列private long[] queArray;      //隊列數組private int front;            //隊頭private int rear;             //隊尾private int nItems;           //當前隊列元素個數//建構函式public Queue(int s) {super();this.maxSize = s;this.queArray = new long[maxSize];this.front = 0;this.rear = -1;this.nItems = 0;}//插入元素public void insert(long j){if(rear ==(maxSize-1)){rear =-1;}queArray[++rear] =j;nItems++;}//刪除元素public long remove(){long temp =queArray[front++];if (front ==maxSize) {front =0;}nItems--;return temp;}//隊列頭public long peekFront() {return queArray[front];}//隊列是否為空白public boolean isEmpty(){return (nItems ==0);}//隊列是否滿了public boolean isFull() {return(nItems==maxSize);}//隊列長度public int size() {return nItems;}}

<pre name="code" class="java">public class QueueApp {public static void main (String[] arg) {Queue theQueue =new Queue(5);theQueue.insert(10);theQueue.insert(20);theQueue.insert(30);theQueue.insert(40);theQueue.remove();theQueue.remove();theQueue.remove();theQueue.insert(50);theQueue.insert(60);theQueue.insert(70);theQueue.insert(80);while (!theQueue.isEmpty()) {long n = theQueue.remove();System.out.print(n);System.out.print("  ");}System.out.print(" ");}}




學習java資料結構基礎知識之隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.