Two methods of implementing circular queue in Java array

Source: Internet
Author: User

To implement the loop queue method in Java:

1. Add an attribute size to record the current number of elements. The goal is to distinguish the queue as empty or the queue is full when head=rear, by size=0 or size= array length.

2, the array only stores the size of the array-1 elements, to ensure that rear will not be equal to head after a turn, that is, when the queue is full, rear+1=head, the middle is just empty an element.

When Rear=head, the queue must be empty.

The types of operations allowed on both sides of the queue (queues) are different:

The one end that can be deleted is called the team head, this kind of operation also calls out the team dequeue;

One end that can be inserted is called the tail of the queue, and this operation is also called the queue enqueue.

of the queue


When implementing the queue, pay attention to the false overflow phenomenon, such as the last picture.

The false overflow phenomenon


Workaround: Use chained storage, which is obviously possible. In sequential storage, our common solution is to bring it up and down to form the loop queue, which can take advantage of the queue's storage space.

Loop queue:


In, front points to the first element in the queue, and rear points to the next position in the queue's tail.

But there is still a problem: when front and rear point to the same position, does this mean the team is empty or the team is full? You can imagine this scenario.

The common practice of solving this problem is this:

Use a mark to differentiate this confusing situation.

Sacrifice an element space. When front and rear are equal, they are empty, and when the next position of rear is front, it is full.

Such as:


Below we give the loop queue, and in the second way, the sacrifice of an element space to distinguish between the team empty and the full line of code.

Several highlights:

1, front point to the team head, rear point at the end of the next position.

2, the team is empty judgment: Front==rear; The team is full of judgment: (rear+1)%maxsize==front.

Import java.io.*;  public class Queuearray {object[] A;//object array, the queue stores up to a.length-1 objects int front;   Team first subscript int rear; Tail subscript public Queuearray () {this (10);//Call other constructor method} public Queuearray (int size) {a =           New Object[size];           Front = 0;       Rear = 0; /** * Appends an object to the end of the queue * @param obj Object * @return returns False if the queue is full, otherwise true */public boolean E           Nqueue (Object obj) {if ((rear+1)%a.length==front) {return false;           } a[rear]=obj;           Rear = (rear+1)%a.length;       return true; }/** * The first object of the queue header * @return out of the queue object, the queues are empty when NULL is returned */public object Dequeue () {if (rear           ==front) {return null;           } Object obj = A[front];           Front = (front+1)%a.length;       return obj;       } public static void Main (string[] args) {Queuearray q = new Queuearray (4);    System.out.println (Q.enqueue ("Zhang San"));           System.out.println (Q.enqueue ("Reese"));           System.out.println (Q.enqueue ("Zhao Wu")); System.out.println (Q.enqueue ("Wang Yi"));//cannot enter queue, queue full for (int i=0;i<4;i++) {System.out.println (q.dequ           Eue ());  }       }   }

Two methods of implementing circular queue in Java array

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.