Data structure (C implementation) ------- ordered queue (Circular queue is implemented with less than one storage space ).
As mentioned in the previous section, there are three methods to implement the cyclic ordered queue. The first method is not commonly used to set up the ID. The most common method is the last two, the previous section has discussed how to use counters to implement cyclic ordered queues. This section uses the third method, that is, to use less storage space to implement cyclic ordered queues, the basic operations are similar to the Count implementation. The specific implementation is as follows:
Sequence Queue (cyclic queue) type description:
// Type description of the ordered queue # define MAXSIZE 5 typedef int ElemType; typedef struct {ElemType * data; int front, rear;} SqQueue;
Basic operations:
1. initialize the sequential Queue (cyclic queue) Init_SqQueue (SqQueue * Q)
void Init_SqQueue(SqQueue* Q){ Q->data = (SqQueue*)malloc(sizeof(SqQueue) * MAXSIZE);Q->front = Q->rear = 0;}
2. Destroy the sequential Queue (cyclic queue) Destroy_SqQueue (SqQueue * Q)
void Destroy_SqQueue(SqQueue* Q){if(Q->data){free(Q->data);Q->front = Q->rear = 0;}}
3. Clear the sequential Queue (cyclic queue) Clear_SqQueue (SqQueue * Q)
void Clear_SqQueue(SqQueue* Q){Q->front = Q->rear = 0;}
4. Determine whether the sequential Queue (cyclic queue) is empty IsEmpty_SqQueue (SqQueue * Q)
int IsEmpty_SqQueue(SqQueue* Q){return (Q->rear == Q->front);}
5. Determine the ordered queue(Cyclic queue)ISFull_SqQueue (SqQueue * Q)
int iSFull_SqQueue(SqQueue* Q){return ((Q->rear + 1) % MAXSIZE == Q->front);}
6. Obtain the ordered queue(Cyclic queue)GetLength_SqQueue (SqQueue * Q)
int GetLength_SqQueue(SqQueue* Q){return (Q->rear - Q->front + MAXSIZE) % MAXSIZE;}
7. Get the ordered queue(Cyclic queue)GetHead_SqQueue (SqQueue * Q, ElemType * x)
Void GetHead_SqQueue (SqQueue * Q, ElemType * x) {if (IsEmpty_SqQueue (Q) {printf ("the ordered 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
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 + MAXSIZE) % MAXSIZE] ;}}
9. In-order queue(Cyclic queue)En_SqQueue (SqQueue * Q, ElemType x)
Void En_SqQueue (SqQueue * Q, ElemType x) {if (iSFull_SqQueue (Q) {printf ("the ordered queue is full! \ N "); exit (0);} else {Q-> data [Q-> rear] = x; Q-> rear = (Q-> rear + 1) % MAXSIZE ;}}
10. Out-of-order queue(Cyclic queue)De_SqQueue (SqQueue * Q, ElemType * x)
Void De_SqQueue (SqQueue * Q, ElemType * x) {if (IsEmpty_SqQueue (Q) {printf ("the ordered queue is empty! \ N "); exit (0);} else {* x = Q-> data [Q-> front]; Q-> front = (Q-> front + 1) % MAXSIZE ;}}
11. Print the ordered queue(Cyclic queue)Print_SqQueue (SqQueue * Q)
Void Print_SqQueue (SqQueue * Q) {int I = 0; int j = Q-> front; if (IsEmpty_SqQueue (Q) {printf ("the sequence queue is empty! \ N "); exit (0);} else {while (I <GetLength_SqQueue (Q) {printf (" % d \ t ", q-> data [j]); j = (j + 1) % MAXSIZE; I ++;} printf ("\ n ");}}
The above is another implementation method of the cyclic ordered queue, that is, the main difference between using less storage space and using counters to implement the cyclic ordered queue is determining that the queue is full.