Implementation of cyclic queue operations

Source: Internet
Author: User

/*
This implementation of front always points to the current head node, rear always points to the End Node
Next Node
*/
# Include <stdio. h>
# Include <stdlib. h>

#define MAXNUM 10
#define datatype int

typedef struct queue{
        int queue[MAXNUM];
        int front;
        int rear;
}queue;

void initqueue(queue** p)
{
        *p = (queue* )malloc(sizeof(queue));

        if(*p != NULL){
                (*p)->front = 0;
                (*p)->rear = 0;
        }
}

int emptyqueue(queue** p)
{
        int val;

        val = ((*p)->front == (*p)->rear);

        return val;
}

Void enqueue (queue ** P, datatype X) // insert X to the end of the team

{
If (* P)-> rear + 1) % maxnum = (* P)-> front ){
Printf ("the queue is full/N ");
Return;
}

        (*p)->queue[(*p)->rear] = x;
        (*p)->rear = ((*p)->rear+1)%MAXNUM;
}

Void delqueue (queue ** P, datatype * X) // deletes the queue Header element and returns it using X

{
If (* P)-> front = (* P)-> rear ){
Printf ("the queue is empty/N ");
Return;
}

        *x = (*p)->queue[(*p)->front];
        (*p)->front = ((*p)->front+1)%MAXNUM;
}

Void firstqueue (queue ** P, datatype * X) // return the queue Header element

{
* X = (* P)-> queue [(* P)-> front];
}

Void prqueue (queue ** p) // print the queue Element

{
Int I;

        i = (*p)->front;
        while(i != (*p)->rear){
                printf("%c", (*p)->queue[i]);
                i = (i + 1) % MAXNUM;
        }
        printf("/n");
}

int main(int argc, char** argv)
{
        queue* p;
        int c, i;

        initqueue(&p);
        printf("input queue node value:/n");
/***
        while((c = getchar()) !='/n'){
                enqueue(&p, c);
        }
***/

        prqueue(&p);

For (I = 0; I <3; I ++ ){
Delqueue (& P, & C); // deletes a Header element.

}

        printf("/n");
        enqueue(&p, '*');
        enqueue(&p, '$');
        prqueue(&p);

        firstqueue(&p, &c);
        printf("first queue is %c/n", c);

        exit(0);
}

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.