Structure and algorithm (5)-----Queue

Source: Internet
Author: User

As we explained earlier, not as an array of storage data function, but as the idea of the algorithm of the auxiliary tool data structure-stack, this article we introduce another such tool-queue. The stack is LIFO, and the queue is just the opposite, FIFO.

1, the basic concept of the queue

A queue is a special linear table , except that it allows for deletion only at the front end of the table (front), but in the back-end (rear) of the table, and, like the stack, the queue is an operation A constrained linear table . The end of the insert operation is called the tail of the queue, and the end of the delete operation is called the team header. When there are no elements in the queue, it is called an empty queue.

The data element of a queue is also known as a queue element. Inserting a queue element in a queue is called queuing, and removing a queue element from a queue is called an out-of-band. Because the queue is only allowed to be inserted at one end and deleted at the other end, only the first elements that enter the queue are first removed from the queue, so the queue is also known as the FIFO (Fifo-first in first out) linear table.

For example, we go to the cinema to buy tickets, the first to enter the queue is the first to buy tickets out of the queue, and finally into the queue queue is the last to buy tickets.

In the computer operating system, for example, there are various queues that work quietly, such as printers waiting to be printed in a print queue.

Queues are divided into:

①, one-way queuing (queue): Data can only be inserted at the end, and data is deleted at the other end.

②, bidirectional queue (Deque): Inserts data and deletes data operations at each end.

Here we will also introduce a queue-priority queue, the priority queue is more specific than the stack and the data structure of the queue, in the priority queue, the data items are sorted by the keyword, the minimum (or maximum) keyword data items are often at the front of the queue, The data item is inserted into the appropriate position at insertion time to ensure that the queue is orderly.

2. Java simulation One-way queue implementation

Before implementing this, let's look at the following questions:

①, unlike stacks, the data in the queue does not always start with the 0 subscript of the array, and after removing some of the team header front data, the team head pointer points to a higher subscript position, such as:

Figure 1 Figure 2

②, we re-design, 1, when a new data in the queue, the tail of the pointer rear will move upward, that is, the direction of the big downward. When you remove a data item, the team head pointer front also moves down. So the design seems to be the opposite of the reality, such as queuing to buy movie tickets, the team head to buy tickets to leave, and then the whole team move forward. After a number is removed from the queue in the computer, the queue moves forward, but it is inefficient. Our choice is to move the hands of the team head and the tail of the team.

From the demonstration in Figure 2, we can see that (a) when the operation is empty queue at this time front and rear are 1, while we can find that although we add front and rear variables to the order table after the use of the team operation time to reduce the complexity of the reduced to O (1), But there is another serious problem, that is, space waste , (d) and (e) operations can be found, after 20 and 30 out of the team, the remaining space is not re-use, but is empty , so lead to perform (f) operation, There is a false phenomenon that the queue is full , this false phenomenon we call false overflow , the reason for this false overflow is because the storage unit of the sequential table queue has no reuse mechanism , and the most appropriate way to solve this problem is to design the sequential queue as loop Structure , the next step is to implement the sequential queue through the Loop order table.

③, if you move the pointer to step ②, I believe that the tail pointer quickly moved to the end of the data, this time may remove the data, then the team head will have a vacant position, and then a new data item, because the end of the team can not move upward, then how to do? Such as:

  

In order to avoid queue dissatisfaction but not to insert new data, we can have the tail pointer around the beginning of the array, which is also known as the " circular queue ."

After the principle is understood, the Java implementation code is as follows:

 Public classMyqueue {Privateobject[] Quearray; //Total Queue Size    Private intmaxSize; //Front End    Private intFront; //back end    Private intRear; //actual number of elements in the queue    Private intNitems;  PublicMyqueue (ints) {maxSize=s; Quearray=NewObject[maxsize]; Front= 0; Rear=-1; Nitems= 0; }    //new data in queue     Public voidInsertintvalue) {        if(Isfull ()) {System.out.println ("The queue is full!!!" "); } Else {            //if the tail of the queue points to the top, then loop back and execute the first element of the queue            if(Rear = = MaxSize-1) {Rear=-1; }            //The tail pointer adds 1 and then inserts the new data at the tail of the team.Quearray[++rear] =value; Nitems++; }    }    //Removing Data     PublicObject Remove () {Object RemoveValue=NULL; if(!IsEmpty ()) {RemoveValue=Quearray[front]; Quearray[front]=NULL; Front++; if(Front = =maxSize) {Front= 0; } nitems--; returnRemoveValue; }        returnRemoveValue; }    //View Enemy Data     PublicObject Peekfront () {returnQuearray[front]; }    //determine if the queue is full     Public BooleanIsfull () {return(Nitems = =maxSize); }    //determine if the queue is empty     Public BooleanIsEmpty () {return(Nitems = = 0); }    //returns the size of the queue     Public intGetSize () {returnNitems; }}
 Public classTest { Public Static voidMain (string[] args) {myqueue queue=NewMyqueue (3); Queue.insert (1); Queue.insert (2); Queue.insert (3);//quearray Array data as [+/-]System.out.println (Queue.peekfront ());//1Queue.remove ();//quearray array data is [null,2,3]System.out.println (Queue.peekfront ());//2Queue.insert (4);//quearray array data is [4,2,3]Queue.insert (5);//queue is full, Quearray array data is [4,2,3]    }}

Test results:

1
2
Queue is full!!!

3. Double-ended queue

A double-ended queue is a queue with both ends or starts, and each end of the queue can insert data items and remove data items, which can be called:

Insertright (), Insertleft (), Removeleft (), Removeright ()

If you strictly prohibit calls to Insertleft () and Removeleft () (or disable right-side operations), then the dual-ended queue function is the same as in the previous stack.

If you strictly prohibit calls to Insertleft () and removeright (or the opposite of another pair of methods), then the function of the double-ended queue is the same as the one-way queue.

4. Priority queue

Priority queue is a more specialized data structure than stacks and queues, in which items are sorted by keyword, the smallest (or largest) items of a keyword tend to be at the front of the queue, and data items are inserted into the appropriate position at insertion time to ensure orderly queuing.

The priority queue is a collection of 0 or more elements, each with a priority, and the actions performed on the priority queue are:

(1) Find

(2) inserting a new element

(3) Delete

In general, a find operation is used to search for the element with the highest priority, and the delete operation is used to delete the element. For elements with the same priority, they can be processed in FIFO order or at any priority.

Here we use an array to implement the priority queue, this method of insertion is relatively slow, but it is relatively simple, suitable for small data volume and is not a special focus on the insertion speed situation.

Later we will explain the heap, using the heap data structure to implement the priority queue, you can insert data fairly quickly.

  The array implements the priority queue, declared as an array of type int, and the keyword is the element inside the array, which is arranged in order from large to small when inserted, that is, the smaller the priority of the element becomes.

 Public classPriorityque {Private intmaxSize; Private int[] priquearray; Private intNitems;  PublicPriorityque (ints) {maxSize=s; Priquearray=New int[MaxSize]; Nitems= 0; }    //Inserting Data     Public voidInsertintvalue) {        intJ; if(Nitems = = 0) {Priquearray[nitems++] =value; } Else{J= NItems-1; //The sorting method chosen is to insert the sort, in order from large to small, the smaller the more at the top of the queue             while(J >= 0 && value >Priquearray[j]) {Priquearray[j+ 1] =Priquearray[j]; J--; } priquearray[j+ 1] =value; Nitems++; }    }    //Remove the data, because it is sorted by size, so we move the pointer down//The place is removed because it is of type int and cannot be set to NULL, the practice here is set to-1     Public intRemove () {intK = nItems-1; intValue =Priquearray[k]; PRIQUEARRAY[K]=-1;//1 indicates that the data for this location has been removednitems--; returnvalue; }    //view the highest-priority elements     Public intpeekmin () {returnPriquearray[nitems-1]; }    //determines whether the empty     Public BooleanIsEmpty () {return(Nitems = = 0); }    //Judging if it's full.     Public BooleanIsfull () {return(Nitems = =maxSize); }}

The Insert () method first checks if there are data items in the queue, and if not, inserts them directly into the cell labeled 0, otherwise, starting at the top of the array, finding a location smaller than the insertion value, and adding the Nitems to 1.

The Remove method gets the top element directly.

The priority queue's insert operation requires an O (N) time, while the delete operation requires an O (1) time, which is explained later by the heap to improve the insertion time.

5. Summary

This blog we introduce the three forms of the queue, namely one-way queue, two-way queue, and priority queue. In fact, we can hear the name of the difference between them, one-way queue to follow the principle of first-out, and the end can only be inserted, the other end can only be deleted. Two-way queues can be inserted and deleted at both ends, and the same functionality can be achieved with one-way queues if the method of restricting a segment of a bidirectional queue is restricted. The last priority queue, when the element is inserted, is prioritized, and the single queue and priority queue are used more often in real-world applications. After explaining the data structure of the heap, we use the heap to implement the priority queue and improve the time that the priority queue inserts the element.

With the above stack and the two data structures in this article, let's summarize a little bit about:

①, Stacks, queues (one-way queues), and priority queues are often the data structures used to simplify some program operations, rather than primarily as storage data.

②, in these data structures, only one data item can be accessed.

③, stacks allow to press (insert) data at the top of the stack, eject (remove) data at the top of the stack, but only access the last inserted data item, which is the top element of the stack.

④, queues (one-way queues) can only insert data at the end of a queue, remove data to the head, and access only the data to the opponent. And the queue can also implement circular queues, which are based on arrays, and array subscripts can be wrapped back from the end of the array to the beginning of the array.

The ⑤, priority queue is an ordered insert of data and can only access elements of the highest (or smallest) priority in the current element.

⑥, these data structures can be implemented by arrays, but can be implemented using other mechanisms (linked lists, heaps, and so on).

Structure and algorithm (5)-----Queue

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.