Home>
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:a two-way circular queue is very useful,Can be used as an ordinary queue,Can also be used as a stack!

  • Previous PHP + Apache + Mysql environment building tutorial
  • Next AngularJS basic ng-cut instruction introduction and simple examples