Java Data Structure Series--Queue (2): sequential storage structure of cyclic queue and its implementation

Source: Internet
Author: User

Package queue.circlesequencequeue;/** * Here we specify that there is an empty space in the array when the queue is full, that the space in the array is not allowed to be full * The condition of the loop queue full is: (rear+1)%queuesize==front * Length Calculation formula: (rear-front+queuesize)%queuesize * @author wl */public class Circlesequencequeue {object[] elementdata;// The array is used to hold the queue element int front;//the head pointer int rear;//the tail pointer int queuesize;//Queue Size//parameterless constructor for the default initialization of the queue public circlesequencequeue () { Elementdata=new object[10];front=0;rear=0; Queuesize=elementdata.length;} A parameter constructor that specifies the queue initialization length of public circlesequencequeue (int capacity) {elementdata=new object[capacity];front=0;rear=0; Queuesize=elementdata.length;} The size of the queue public int size () {return (rear-front+queuesize)%queuesize;} Determines whether the queue is empty public boolean isEmpty () {return front==rear;} Determines whether the queue is full public boolean isfull () {return (rear+1)%queuesize==front;} into queue public void enQueue (int data) {if (Isfull ()) {throw new Indexoutofboundsexception ("queue full");} elementdata[rear]=data;//adds the data element to the tail rear= (rear+1)%queuesize;//moves the rear pointer back one bit, and if rear to the end it is transferred to the array header}//out of the queue public Object DeQueue () {if (IsEmpty ()) {throw new Indexoutofboundsexception ("queue is Empty");} Get the enemy Element object data=elementdata[front];//release the head element elementdata[front]=null;//front the pointer moves backward one bit, if the end is transferred to the array header front= (front +1)%queuesize;return data;} Get the opponent element, do not delete public Object Getfront () {if (IsEmpty ()) {return null;} Else{return Elementdata[front];}} The element in the print queue public void traverse () {if (IsEmpty ()) {System.out.println ("null");} Else{int counter=0;//counter for (int i=front;counter<=size ();) {if (elementdata[i]!=null) {System.out.print ( elementdata[i]+ "");} I= (i+1)%queuesize;counter++;}} System.out.println ();}}

Java Data Structure Series--Queue (2): sequential storage structure of cyclic queue and its implementation

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.