java algorithm -- queue (cyclic sequential queue)

Algorithm is neither easy to get started nor easy to master. But as a salted fish with a dream, I decided to work hard.

Stack and Queue are two data structures respectively. Different languages have different declarations for stack and Queue. In java, stack class is a subclass inherited from Vector set, and Queue exists in the form of interface. The commonly used implementation class is LinkedList, a two-way Queue. In the standard template library of java, there are also specific classes defined by these two data structures.

The stack data structure is characterized by FILO (first in last out), that is, first in first out, and the queue is FIFO (first in first out), that is, first in first out.

It can be seen everywhere in daily real life, for example, students buy food in the canteen, queue up on the bus, etc. These queuing rules are first come, first deal with events. You can't jump in the queue. You should abide by certain norms. Actually. Our commonly used computers also have queues, processes, resource waiting queues, job queues, etc. in the operating system.

​​​​​​

1, Concept of queue:

Queue is also a special linear table. The data elements of queue and the logical relationship between data elements are exactly the same as that of linear table. The difference is that linear table allows insertion and deletion at any position, while queue only allows insertion at one end and deletion at the other end.

The end of the queue that allows insertion is called the end of the queue, and the end that allows deletion is called the head of the queue. The insertion of a queue is usually called entering a queue, and the deletion of a queue is usually called making a queue.

The following figure is a schematic diagram after inserting data elements a0,a1,...,an-1 into the queue in turn:

 

1. "False overflow" of sequential queue:

In the above figure, the front pointer points to the queue head element and the rear pointer points to the next position of the queue tail element. After b, c and D in figure (d) leave the queue, the front pointer points to element e, and the rear pointer is outside the array. Assuming that the total number of this queue does not exceed 5, but at present, if you join the queue, because the elements at the end of the array have been occupied, adding back will produce an array out of bounds error. In fact, the queue is still idle where the subscripts are 0, 1, 2, 3 and 4. We call this phenomenon "false overflow".

2. Circular sequence queue:

    Therefore, the way to solve the false overflow is to start from the beginning when the back is full, that is, the cycle from head to tail. We call this logically end-to-end sequential storage structure of the queue circular queue:

         When the queue is empty, the condition is from = rear. When the queue is full, we modify the condition to reserve an element space. That is, when the queue is full, there is another free cell in the array. As shown in the figure below, we think the queue is full,

 

Since the rear may be larger or smaller than the front, although they are full when they differ by only one position, they may also differ by a whole circle. Therefore, if the maximum size of the queue is QueueSize, the queue full condition is (rear + 1)% QueueSize = = front   (the purpose of taking the module "% is to integrate the size of real and front as a problem).

For example, in the above example, QueueSize = 5, when front=0 and rear = 4, (4 + 1)% 5 = 0, the queue is full. For another example, front = 2 and rear =1. (1 + 1)% 5 = 2, so the queue is also full. For the following figure, front = 2 and rear = 0, (0 + 1)% 5 = 1, 1! = 2, so the queue is not full.

In addition, when rear > front, the queue length is rear front. However, when rear < front, the queue length is divided into two segments, one is queuesize front and the other is 0 + rear. Together, the queue length is rear front + queuesize,

Therefore, the general formula for calculating queue length is:

(rear—front + QueueSize) % QueueSize

summary

Air condition: front == rear

Queue full condition: (rear + 1)% queuesize = = front

Queue length: (rear - front + queuesize)% queuesize

Implementation of basic operations of circular queue

Interface class

public interface IQueue {
    public void clear();
    public boolean isEmpty();
    public int length();
    public Object peek();
    public void offer(Object x)throws Exception;
    public Object poll();
    public void display();
}

Queue operation

    public void offer(Object x) throws Exception {
        if ((rear+1)%queueElem.length==front)            //Queue full
            throw  new Exception("The queue is full");            //Throw exception
        else {
            queueElem[rear]=x;
                                          //x is stored in the storage location of the group indexed by the rear, making it the end of the team
            rear=(rear+1)%queueElem.length;            //Modify end of queue pointer
        }
    }

Out of the team

 public Object poll() {
        if (front==rear)
        return null;
        else{
            Object t=queueElem[front];
            front=(front+1)%queueElem.length;
            return t;                        //Returns the first element of the queue
        }

Code summary

public class CircleSqQueue implements IQueue{
    private  Object[] queueElem;//Queue storage space
    private  int front;         //Reference to team leader
    private int rear;      //End of queue reference

    public CircleSqQueue(int maxSize) {
        front=rear=0;                  //End of queue, head of queue initialization 0
      queueElem = new Object[maxSize];//The tail queue allocates maxSize storage units
    }

    @Override
    public void clear() {
        front=rear=0;//
    }

    @Override
    public boolean isEmpty() {
        return front==rear;
    }

    @Override
    public int length() {
        return (rear-front+queueElem.length)%queueElem.length;
    }
        //Read queue header element
    @Override
    public Object peek() {
        if (front==rear)

        return null;
        else
            return queueElem[front];
    }
//Queue operation
    @Override
    public void offer(Object x) throws Exception {
        if ((rear+1)%queueElem.length==front)
            throw  new Exception("The queue is full");
        else {
            queueElem[rear]=x;
            //x is stored in the storage location of the group indexed by the rear, making it the end of the team
            rear=(rear+1)%queueElem.length;//Modify end of queue pointer
        }
    }

    @Override
    public void display() {
        //Simple traversal
        if(!isEmpty()) {
            for (int i=front;i!=rear;i=(i+1)%queueElem.length)
                System.out.print(queueElem[i].toString()+"\t");
        }else{
        }
        //The train of thought starts from front, and you can traverse as many elements as you want
        //The number of current queues is required
        
    }
//Out of line operation
    @Override
    public Object poll() {
        if (front==rear)
        return null;
        else{
            Object t=queueElem[front];
            front=(front+1)%queueElem.length;
            return t;        //Returns the first element of the queue
        }
        
        }
    }

Add switch with loop   case write a test class, the test is successful!

 

 

 

 

Tags: Java Algorithm data structure

Posted on Mon, 08 Nov 2021 02:47:08 -0500 by kittrellbj