Queue implemented as a linked list

#include<stdio.h>

#define max 4

struct node

{

        int data;

        struct node *next;

};

void push(struct node **,int);

void pop(struct node **);

void display(struct node *);

main()

{

        struct node *head;

        head = NULL;

        push(&head,10);

        push(&head,20);

        push(&head,30);

        push(&head,40);

        push(&head,50);

        printf("the elements in queue are\n");

        display(head);

        printf("\n");

        pop(&head);

        printf("after deleting the elements are\n");

        display(head);

}

void push(struct node **q,int num)

{

        struct node *temp,*r;

        static int n=1;

        temp = *q;

        if(temp == NULL)

        {

                temp = (struct node*)malloc(sizeof(struct node));

                temp->data = num;

                temp->next = NULL;

                *q = temp;

        }

        else

        {

                if(n<max)

                {

                        while(temp->next!=NULL)

                        {

                                temp = temp->next;

                                n++;

                        }

                        r = (struct node*)malloc(sizeof(struct node));

                        r->data = num;

                        r->next = NULL;

                        temp->next = r;

                }

                else

                {

                        printf("Queue is full\n");

                }

        }

}

void pop(struct node **q)

{

        struct node *temp,*old;

        temp = *q;

        if(temp == NULL)

        {

                printf("Queue is empty\n");

        }

        else

        {

        *q = temp->next;

        free(temp);

        }

}

void display(struct node *q)

{

        while(q!=NULL)

        {

                printf("%d  ",q->data);

                q = q->next;

        }

}

-----------------------------------------------------------------------------------------------------------------

op :

Queue is full

the elements in queue are

10  20  30  40

after deleting the elements are

20  30  40

Author

Written by Admin

Aliquam molestie ligula vitae nunc lobortis dictum varius tellus porttitor. Suspendisse vehicula diam a ligula malesuada a pellentesque turpis facilisis. Vestibulum a urna elit. Nulla bibendum dolor suscipit tortor euismod eu laoreet odio facilisis.

0 comments: