An array implementation method for bidirectional cyclic queues of Java data structure and algorithm _java

Source: Internet
Author: User
Tags int size

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.