java數據構造與算法之雙向輪回隊列的數組完成辦法。本站提示廣大學習愛好者:(java數據構造與算法之雙向輪回隊列的數組完成辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是java數據構造與算法之雙向輪回隊列的數組完成辦法正文
本文實例講述了java數據構造與算法之雙向輪回隊列的數組完成辦法。分享給年夜家供年夜家參考,詳細以下:
須要解釋的是此算法我並沒有測試過,這裡給出的相當於偽代碼的算法思惟,所以只能用來作為參考!
package source; public class Deque { private int maxSize; private int left; private int right; private int nItems; private long[] myDeque; //constructor public Deque(int maxSize){ this.maxSize = maxSize; this.myDeque = new long[this.maxSize]; this.nItems = 0; this.left = this.maxSize; this.right = -1; } //insert a number into left side public void insertLeft(long n){ if(this.left==0) this.left = this.maxSize; this.myDeque[--this.left] = n; this.nItems++; } //insert a number into right side public void insertRight(long n){ if(this.right==this.maxSize-1) this.right = -1; this.myDeque[++this.right] = n; this.nItems++; } //remove from left public long removeLeft(){ long temp = this.myDeque[this.left++]; if(this.left==this.maxSize) this.left = 0; this.nItems--; return temp; } //remove from right public long removeRight(){ long temp = this.myDeque[this.right--]; if(this.left==-1) this.left = this.maxSize-1; this.nItems--; return temp; } //return true if deQue is empty public boolean isEmpty(){ return (this.nItems==0); } //return size of the deQue public int size(){ return this.nItems; } }
PS:雙向輪回隊列的用途很年夜,可以做為通俗隊列,也能夠用來做棧來用!
願望本文所述對年夜家java法式設計有所贊助。