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