A queue is a linear table that allows insertion at one end and deletion at the other end. It is a first-in-first-out linear table (FIFO ). The end that can be inserted is called the team end, and the end that can be deleted is called the team header. We found in the stack sequence storage structure that the top pointer of stack operations increases while pushing and decreases when Pop is used. Stack space can be reused, the front and rear pointers of the queue keep increasing. Although the elements in the queue are already out of the queue, the storage space occupied by the queue cannot be reused. However, most programs do not use the queue in this way. Normally, the elements that leave the queue will no longer save value. The storage space of these elements should be recycled, therefore, the queue is transformed into a circular Queue (Circular
Queue): think of the queue array as a circle, and the front and rear pointers keep increasing. When it comes to the end of the array, it will automatically return to the beginning of the array, just like two people race around the playground, as they run, the queue is an effective element between the front and the rear, and an empty storage location between the rear and the front. If the front catches up with the rear, the queue is empty, if rear catches up with front, the storage space of the queue is full. Therefore, we generally implement it as a cyclic queue. When the queue is out, we do not need to move it all. We only need to modify the header pointer of the queue and solve the "false overflow" problem.
Example program: (adapted from big talk data structure)
C ++ code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
# Include <iostream> Using namespace STD;# Define maxsize 20 Typedef int elemtype; Typedef struct { Elemtype data [maxsize]; Int front;/* Header pointer */ Int rear;/* tail pointer. If the queue is not empty, it points to the next position of the element at the end of the queue */ } Sqqueue; Bool initqueue (sqqueue * PQ) { Cout <"init queue..." <Endl; PQ-> front = 0; PQ-> rear = 0; Return true; } Bool clearqueue (sqqueue * PQ) { Cout <"Clear queue..." <Endl; PQ-> front = 0; PQ-> rear = 0; Return true; } Bool queueempty (sqqueue sq) { Return (sq. Front = SQ. Rear);/* sign of empty queue */ } Int queuelength (sqqueue sq) { /* Current queue length */ Return (sq. Rear-sq. Front + maxsize) % maxsize; } /* Return Header element */ Bool gethead (sqqueue SQ, elemtype * PE) { If (queueempty (SQ )) Return false; Else { * Pe = SQ. Data [sq. Front]; Cout <"get head item" <* PE <Endl; Return true; } } Bool enqueue (sqqueue * PQ, elemtype ELEM) {/* Queue full */ If (queuelength (* PQ) = maxsize) Return false; Cout <"enqueue item" <ELEM <Endl; PQ-> data [PQ-> rear] = ELEM;/* assign the element to the end of the Team */ PQ-> rear = (PQ-> rear + 1) % maxsize;/* move the rear pointer to the back position ,*/ /* Go To The array header if it reaches the end */ Return true; } Bool dequeue (sqqueue * PQ, elemtype * PE) { If (queueempty (* PQ )) Return false; * Pe = PQ-> data [PQ-> front];/* assign the team Header element to * Pe */ Cout <"dequeue item" <* PE <Endl; PQ-> front = (PQ-> front + 1) % maxsize;/* move the front pointer to the back position ,*/ /* Go To The array header if it reaches the end */ Return true; } Bool queuetraverse (sqqueue sq) { Cout <"queue traverse..." <Endl; For (INT I = 0; sq. Front + I <sq. Rear; I = (I + 1) % maxsize) Cout <sq. Data [I + sq. Front] <''; Cout <Endl; Return true; } Int main (void) { Sqqueue sq; Initqueue (& sq ); For (INT I = 0; I <5; I ++) Enqueue (& SQ, I ); Queuetraverse (SQ ); Int result; Gethead (SQ, & result ); Dequeue (& SQ, & result ); Dequeue (& SQ, & result ); If (! Queueempty (SQ )) Cout <"Queue Length:" <queuelength (SQ) <Endl; Queuetraverse (SQ ); Return 0; } |
Output:
Sequential storage alone. If it is not a cyclic queue, the algorithm's time performance is not high, but the cyclic queue also faces the problem that arrays may overflow.