Tag:java Queue array to implement queue
Queue (array emulation) class queue{private int[] queue;//queuing function int length; int head; Head pointer int tail; tail pointer int num; The number of elements in the Drop Column public queue () {} public queue (int s) {//construct queue function length=s; Queue=new Int[length]; S for queue Length head=0; Tail=-1; num=0;} public void InQueue (int e) {//In queue if (tail+1==length) {System.out.println ("overflow, element" +e+ "not in queue!!!) "); return;} Queue[++tail]=e; num++;} public int Outqueue () {//Out queue if (head==length) {System.out.println ("There are no elements in the queue!!! "); }int Temp=queue[head++];num--;return temp;} public int Readqueue () {//Out of queue (read-only non-operational queue) if (head==length) {System.out.println ("There are no elements in the queue!!!") "); }return Queue[head];} public void IsEmpty () {//Is empty if (num==0) {System.out.println ("Empty queue"!!! "); }} public void Isfull () {//Is full if (tail==length-1) {System.out.println ("The queue is full!!!") ");} else{System.out.println ("The queue is not full!!! "); }}public int count () {//number of elements in Queue return num;}} Class main{public static void Main (String args[]) {Queue q=new queue (5); Q.isempty (); Q.inqueue (2); SYSTEM.OUT.PRINTLN ("The read-out queue element is:" +q.readqueue ()); System.out.println ("The head element of the fetched queue is:" +q.outqueue ()); Q.inqueue (3); SYSTEM.OUT.PRINTLN ("The read-out queue element is:" +q.readqueue ()); System.out.println ("The head element of the fetched queue is:" +q.outqueue ()); Q.inqueue (4); SYSTEM.OUT.PRINTLN ("The read-out queue element is:" +q.readqueue ()); System.out.println ("The head element of the fetched queue is:" +q.outqueue ()); Q.inqueue (5); Q.isempty (); SYSTEM.OUT.PRINTLN ("The read-out queue element is:" +q.readqueue ()); System.out.println ("The head element of the fetched queue is:" +q.outqueue ()); Q.inqueue (6); Q.isfull (); SYSTEM.OUT.PRINTLN ("The read-out queue element is:" +q.readqueue ()); System.out.println ("The head element of the fetched queue is:" +q.outqueue ()); Q.isempty ();}}
Queue (array emulation)