This article describes the array implementation method of bidirectional loop queues of Java data structures and algorithms. Share it for your reference, as follows:
It should be noted that I have not tested this algorithm. The algorithm idea given here is equivalent to pseudo-code, so it 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[] myDequ; //constructor public Deque(int maxSize){ this.maxSize = maxSize; this.myDequ = 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.myDequ[--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.myDequ[++this.right] = n; this.nItems++; } //remove from left public long removeLeft(){ long temp = this.myDequ[this.left++]; if(this.left==this.maxSize) this.left = 0; this.nItems--; return temp; } //remove from right public long removeRight(){ long temp = this.myDequ[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: Bidirectional loop queues are of great use. They can be used as ordinary queues or as stacks!
For more information about Java algorithms, readers who are interested in this site can view the topics: "Java Data Structure and Algorithm Tutorial", "Summary of Java Operation DOM Node Tips", "Summary of Java File and Directory Operation Tips" and "Summary of Java Cache Operation Tips"
I hope this article will be helpful to everyone's Java programming.