This paper illustrates the method of array implementation of bidirectional cyclic queue of Java data structure and algorithm. Share to everyone for your reference, specific as follows:
Need to explain this algorithm I have not tested, here is the equivalent of pseudo-code algorithm thought, so can only be used as a reference!
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 the 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 TrueIf DeQue is empty public boolean isempty () {return (this.nitems==0);
//return size of the DeQue public int size () {return this.nitems;
}
}
PS: Bidirectional loop queue is of great use, can be used as a normal queue, can also do stack to use!
More about Java algorithms Interested readers can view the site topics: "Java data structure and algorithms tutorial", "Java Operation DOM node skills summary", "Java file and directory operation tips Summary" and "Java Cache operation Tips"
I hope this article will help you with Java programming.