Package queue.sequencequeue;/** * Queue definition: A queue is a linear table that allows only one end of an insert operation, while a delete operation at the other end is a line-in, first-in-A-class, FIFO, short-form, Allow one end of the insertion * to be called the tail of the queue, allowing the deleted end to be called the team header * @author WL * */public class Sequencequeue {private object[] elementdata;//use an array to save the elements of the queues private int front;//queue's head pointer private int rear;//queue tail pointer//parameterless constructor public Sequencequeue () {elementdata=new object[10];front=0;rear=0 ;} With the parameter constructor, specify the initialization length of the array public sequencequeue (int capacity) {elementdata=new object[capacity];front=0;rear=0;} The size of the queue public int size () {return rear-front;} Determines whether the queue is empty public boolean isEmpty () {int size=size (); return size==0;} Determines whether the queue is full public boolean isfull () {int size=size (); return size==elementdata.length;} into queue public void enQueue (int data) {if (Isfull ()) {throw new Indexoutofboundsexception ("queue full");} Elementdata[rear++]=data;} Out of queue public Object DeQueue () {if (IsEmpty ()) {throw new Indexoutofboundsexception ("Queue is Empty");} Get the opponent Element Object data=elementdata[front];//release the head element Elementdata[front++]=null;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{for (int i=front;i<rear;i++) {System.out.print (elementdata[i]+ "");}} System.out.println ();}}
Java Data Structure Series--Queue (1): Sequential storage structure of queue and its implementation