Java implementation of the "Data structure" queue (i)

Source: Internet
Author: User

A queue is a linear table that is limited to operations that are allowed to be inserted at one end only, while the other is deleted at the other end.

(1) The one end that allows deletion is called the team header (Front).

(2) One end of the allowable insertion is called the tail of the queue (Rear).
(3) When there are no elements in the queue, it is called an empty queue.
(4) The queue is also referred to as the FIFO table, which is referred to as a linear table of first-in-a-out.
The modification of the queue is based on the FIFO principle. The new member always joins the tail of the team, each time leaving the member is always on the head of the queue (not allowed to drop off).

The storage structure and implementation of queue

Sequential storage structure of queues

(1) Definition of sequential queue:

The sequential storage structure of a queue is called a sequential queue, and the sequential queue is actually a sequential table with limited operations.

(2) The representation of the sequential queue:

As with sequential tables, sequential queues take advantage of a contiguous amount of storage space in memory to hold elements in the current queue.
Since the position of the queue's head and tail is changed, the two pointers front and rear indicate the team head element and the tail element respectively, and their initial value should be set to 0 when the queue is initialized.

(3) Basic operation of sequential queue


When enqueued: Inserts a new element into the last bit of the position referred to by rear.
When out of the team: Delete the element that front refers to, and then add the front to 1 and return the deleted element.

(4) Overflow of sequential tables

① "underflow" phenomenon
When the queue is empty, the overflow phenomenon generated by the team operation is made. "Underflow" is a normal phenomenon and is commonly used as a condition for program control transfer.

② "true Overflow" phenomenon
When the queue is full, doing a stack operation creates a space overflow phenomenon. A "true overflow" is an error state that should be managed to avoid.

③ "false Overflow" phenomenon
As a result of the queue and the operation of the team, the end-to-end pointer only increase not decrease, resulting in the deleted element of the space can never be re-use. When the actual number of elements in the queue is much smaller than the allocated space in memory, it is possible that the tail pointer is beyond the upper bounds of the vector space and cannot be queued. This phenomenon is called "false overflow" phenomenon. Such as

Loop queue:

As shown, this sequential storage structure of the tail-to-toe is called a cyclic queue (circular).

Several important issues to be aware of in a circular queue:

① Team empty decision condition, team empty condition is front=rear;

② conditions, (rear+1)%queuesize=front. Queuesize is the initial space size for the queue.

Java implementation code for the loop queue
 PackageStudy_02.datastructure.queue;/*** Loop Queue *@authorwwx*/ Public classCirqueue<e> {    //an array of objects in which the queue stores up to a.length-1 objectse[] A; //Default Initialization size    Private Static Final intdefault_size=10; //to the first subscript    intFront; //end of Team subscript    intRear;  PublicCirqueue () { This(default_size); }    /*** Initialize queue of specified length *@paramsize*/@SuppressWarnings ("Unchecked")     PublicCirqueue (intsize) {a= (e[]) (NewObject[size]); Front=0; Rear=0; }        /*** Append an object to the tail of the queue *@paramobj *@returnreturns False if the queue is full, otherwise true *@authorwwx*/     Public Booleanenqueue (E obj) {if((rear+1)%a.length==front) {            return false; }Else{A[rear]=obj; Rear= (rear+1)%a.length; return true; }    }        /*** Queue head out team *@return     * @authorwwx*/     PublicE dequeue () {if(rear==front)return NULL; Else{E obj=A[front]; Front= (front+1)%a.length; returnobj; }    }        /*** Queue Length *@return     * @authorwwx*/     Public  intsize () {return(Rear-front) & (a.length-1); }    //Queue Length (another method)     Public intLength () {if(rear>front) {            returnrear-Front; }Else            returnA.length-1; }        /*** To determine if it is empty *@return     * @authorwwx*/     Public BooleanIsEmpty () {returnrear==Front; }         Public Static voidMain (string[] args) {Cirqueue<String> queue=NewCirqueue<string> (4); Queue.enqueue ("1"); Queue.enqueue ("2"); Queue.enqueue ("3"); System.out.println ("Size=" +queue.size ()); intSize=queue.size (); System.out.println ("******* out stack operation *******");  for(inti=0; i<size;i++) {System.out.print (Queue.dequeue ()+" "); }            }    }

Java implementation of the "Data structure" queue (i)

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.