Data structure (C implementation) ------- ordered queue (less than one storage space for cyclic queues)., ------- queue
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.
How can we use only the head pointer of the queue to implement sequential cyclic queues?
Handsome guy, you can go to the bookstore to find Yan Weimin's teaching material for "Data Structure ".
I can give you some ideas about the algorithm:
Generally, an ordered queue must be designed as a data structure of an ordered cyclic queue. Otherwise, "false overflow" may occur.
False overflow means that such overflow is not caused by insufficient storage space, however, the tail pointer of the queue cannot be automatically converted from the defined maximum value to the minimum value (that is, the tail pointer is switched to the head of the queue)
The method to prevent false overflow is to use "sequential cyclic queue"
Assume that the front pointer and the back pointer are rear.
Then let the team tail pointer + 1 automatically point to the opposite-this is a loop
This is implemented by the % operation:
(Rear + 1) % N = front, where N is the size of the bucket
In an ordered cyclic queue, how can we determine the statuses of "Full queue" and "Empty queue?
The general practice is to use less storage space. In this case:
Condition for empty queue: rear = front
Conditions for full queue: (rear + 1) % N = front
I personally think that this implementation is unrealistic and meaningless. What do you think is queue ne? Isn't it the first or last line? You don't even have a tail pointer. Every time you insert data, you need to traverse the entire queue through the traversal algorithm. This efficiency is really .... garbage can be used. Instead of using only the first pair pointer, it is better to use arrays instead. What do you say ??
Also, I see your supplement. Isn't the essence of the counter actually the team-end pointer? The opposite pointer always points to the opposite. Isn't front + Count (Count is the counter value) The position of the team's tail pointer? So the counter is not a new form of team-end pointer, isn't it?
Questions about a Data Structure cyclic queue!
The queue length should be :( rear-front + m) % m
There are two main situations of cyclic queue:
1. The front is above, for example, a 5-member cyclic queue:-Indicates null, and x indicates data.
| ------- 5 |
| ------- 4 | <-rear
| Xxxxxx3 |
| Xxxxxx2 | <-front
| ------- 1 |
The queue length is rear-front.
2. Because it is a loop queue, it enters from the tail when it is in the queue, and comes out from the front when it comes out. If case 1 comes in with two more elements, it becomes
| Xxxxx5 |
| Xxxxx4 |
| Xxxxx3 |
| Xxxxx2 | <-front
| ------- | <-Rear
Obviously, in this case, the result obtained by using rear-front is incorrect, so the above formula is displayed.
Actually, I think it's okay to take the absolute value.