Data structure (C implementation) ------- ordered queue (Circular queue is implemented with less than one storage space ).

Source: Internet
Author: User

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.


 




 

 



 

 

 

 

 

Data structure (C implementation) ------- ordered queue (Circular queue is implemented with less than one storage space ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.