To fully utilize the space and solve the "false overflow" problem of sequential queues, two methods can be used: one is to move the data forward and leave the empty storage unit at the end of the team; the other is to construct an ordered queue into a ring space, that is, data in the data partition of the queue [0 .... MAXSIZE-1] is seen as a loop structure that connects the head and end, so that data [0] is connected after data [MAXSIZE-1], which is a loop queue.
This section is used to implement the cyclic ordered queue.
The free space in the cyclic queue can be used. The array space will not overflow unless it is actually occupied by all queue elements. Therefore, in addition to this simple application, the actually practical ordered queue is a cyclic queue.
When the team is in the queue, the team's tail pointer goes forward to catch up with the team's head pointer; when the team is out, the team's head pointer goes forward to catch up with the team's tail pointer. Therefore, the empty team and the full team pointer are equal to the team's tail pointer. Q-> front = Q-> rear cannot be used to determine whether the ordered queue is empty or full.
To solve this problem, there are three methods: one is to set a flag to distinguish whether the ordered queue is empty or full; the other is to use a counter to record the total number of elements in the queue; the third is to use less element space. It is agreed to test whether adding 1 to the end pointer of the team is equal to the head pointer of the team in the circular sense before entering the team. If they are equal, the team is considered full (note: the Unit indicated by rear is always null ).
This section uses the second method to set a counter to record the total number of elements in the queue to determine whether the queue is empty or full.
Sequence Queue (cyclic queue) type description:
// Type description of the ordered queue (set up the Counter) # define maxsize 100 typedef int elemtype; typedef struct {elemtype * data; int front, rear, count;} sqqueue;Basic operations:
1. initialize the sequential Queue (cyclic queue) init_sqqueue (sqqueue * q)
// Initialize the ordered queue void init_sqqueue (sqqueue * q) {q-> DATA = (sqqueue *) malloc (sizeof (sqqueue) * maxsize ); q-> front = Q-> rear = Q-> COUNT = 0 ;}
2. Destroy the sequential Queue (cyclic queue) destroy_sqqueue (sqqueue * q)
// Destroy the void destroy_sqqueue (sqqueue * q) {If (Q-> data) {free (Q-> data ); q-> front = Q-> rear = Q-> COUNT = 0 ;}}
3. Clear the sequential Queue (cyclic queue) clear_sqqueue (sqqueue * q)
// Clear the void clear_sqqueue (sqqueue * q) {q-> front = Q-> rear = Q-> COUNT = 0 ;}
4. Determine whether the sequential Queue (cyclic queue) is empty isempty_sqqueue (sqqueue * q)
// Determine whether the ordered queue is empty. Int isempty_sqqueue (sqqueue * q) {return (Q-> COUNT = 0 );}
5. Determine the ordered queue(Cyclic queue)Isfull_sqqueue (sqqueue * q)
// Determine whether the ordered queue is full int isfull_sqqueue (sqqueue * q) {return (Q-> COUNT = maxsize );}
6. Obtain the ordered queue(Cyclic queue)Getlength_sqqueue (sqqueue * q)
// Obtain the length of the ordered queue int getlength_sqqueue (sqqueue * q) {return Q-> count ;}
7. Get the ordered queue(Cyclic queue)Gethead_sqqueue (sqqueue * q, elemtype * X)
// Obtain the queue header void gethead_sqqueue (sqqueue * q, elemtype * X) {If (isempty_sqqueue (q) {printf ("the queue is empty! \ N "); exit (0);} else {* x = Q-> data [q-> front];}
8. Get the ordered queue(Cyclic queue)Getrear_sqqueue (sqqueue * q, elemtype * X)
// Obtain the void getrear_sqqueue (sqqueue * q, elemtype * X) {If (isempty_sqqueue (q) {printf ("the ordered queue is empty! \ N "); exit (0);} else {* x = Q-> data [q-> rear-1];}
9. In-order queue(Cyclic queue)En_sqqueue (sqqueue * q, elemtype X)
// Input sequence queue void en_sqqueue (sqqueue * q, elemtype X) {If (isfull_sqqueue (q) {printf ("sequence queue is full! \ N "); exit (0);} else {q-> data [q-> rear] = x; q-> rear = (Q-> rear + 1) % maxsize; q-> count ++ ;}}
10. Out-of-order queue(Cyclic queue)De_sqqueue (sqqueue * q, elemtype * X)
// Output sequence queue void de_sqqueue (sqqueue * q, elemtype * X) {If (isempty_sqqueue (q) {printf ("sequence queue is empty! \ N "); exit (0);} else {* x = Q-> data [q-> front]; q-> front = (Q-> front + 1) % maxsize; q-> count --;}}
11. Print the ordered queue(Cyclic queue)Print_sqqueue (sqqueue * q)
// Print the void print_sqqueue (sqqueue * q) {int I = 0; Int J = Q-> front; If (isempty_sqqueue (q )) {printf ("the ordered queue is empty! \ N "); exit (0) ;}else {While (I <q-> count) {printf (" % d \ t ", q-> data [J]); j = (J + 1) % maxsize; I ++;} printf ("\ n ");}}
A counter pointer is attached to realize the cyclic ordered queue.
Data structure (C implementation) ------- ordered queue (counter Implementation of cyclic queue)