Java implementation detailed code for the queue structure

Source: Internet
Author: User

First, what is the queue structure

A linear structure with a special algorithm "can only be removed at one end (team head) and inserted at the other end (tail of the queue)."

Classification:

    1. Sequential queue structure
    2. Chain-Queue structure

Basic operation:

    1. Into the queue
    2. Out queue

II. Preparation of data

1 Static Final intQueuelen = 15;2 3 classdata{4     5 String name;6     intAge ;7 }8 9 classsqtype{Tendata[] data =NewData[queuelen]; One     inthead; A     inttail;  -}

In the class Sqtype, data is an element, head is the first ordinal of the team, tail is the tail number, when head is 0 o'clock, when the tail is Queuelen, the queue is full.

Third, initialize the queue

Before using the queue, you first create an empty queue, that is, the queue is initialized.

    1. Queuelen the size specified by the symbol constant to request a memory space to hold the data in the column.
    2. Set head=0 and tail=0 to represent an empty queue.
1 Sqtype Sqtypeinit () {2 Sqtype Q;3     if((q=NewSqtype ())! =NULL){//Request Memory4Q.head = 0;5Q.tail = 0;6         returnQ;7     }8     Else9     {Ten         return NULL; One     } A}

Iv. Judging the empty queue

1 int sqtypeisempty (Sqtype q) {2     int temp = 0; 3     if (q.head==Q.tail) 4     {5         temp = 1; 6     }7     return  temp; 8 }

V. Judgment of the full queue

1 int sqtypeisfull (Sqtype q) {2     int temp = 0; 3     if (q.tail==Queuelen) 4         temp = 1; 5     return temp; 6 }

Six, please empty queue

1 void sqtypeclear (Sqtype q) {2     q.head = 0; 3     Q.tail = 0; 4 }

Seven, free space

1 void Sqtypefree (Sqtype q) {2     3     if (q!=null) {4         NULL ; 5     }6 }

Eight, into the queue

1 intInsqtype (sqtype q,data DATA) {2     if(Sqtypeisempty (q) ==1) {3System.out.println ("The queue is full, the operation failed!") ");4         return0;5     }6     Else7     {8q.data[q.tail++]=data;9         return1;Ten     } One      A}

Nine, out of the queue

1 DATA Outsqtype (Sqtype q) {2     if(Sqtypeisempty (q) ==1)3     {4System.out.println ("Queue is empty");5         return NULL;6     }7     Else 8     {9         returnq.data[q.head++];Ten     } One     return NULL A}

Ten, read the node

1 DATA Peeksqtype (Sqtype q) {2     if(Sqtypeisempty (q) ==1)3     {4System.out.println ("Queue is empty");5         return NULL;6     }7     Else 8     {9         returnQ.data[q.head];Ten     } One     return NULL A}

Xi. Calculate Queue Length

1 int Sqtypelen (Sqtype q) {2     int temp; 3     temp = q.tail- q.head; 4     return temp; 5 }

Java implementation detailed code for the queue structure

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.