Queue (array emulation)

Source: Internet
Author: User

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)

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.