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:
- Sequential queue structure
- Chain-Queue structure
Basic operation:
- Into the queue
- 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.
- Queuelen the size specified by the symbol constant to request a memory space to hold the data in the column.
- 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