Code Scrappers


Queue Using Linked List - C Program


                    #include<stdio.h>
                    #include<stdlib.h>

                    void enqueue();

                    void dequeue();

                    void display();

                    struct node
                    {
                        int data;

                        struct node* link;
                    };

                    struct node *rear, *front;

                    void main()
                    {
                        int i=0;

                        int choice, value;

                        front = NULL;

                        while(1)
                        {
                            printf("1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");

                            printf("Enter your choice: \n");

                            scanf("%d",&choice);

                            switch(choice)
                            {
                                case 1:enqueue();

                                       break;

                                case 2:dequeue();

                                       break;

                                case 3:display();

                                       break;

                                case 4:exit(0);

                                       break;

                            }

                        }
                    }

                    void enqueue()
                    {
                        struct node* temp;

                        int value;

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

                        printf("enter the value to be pushed.\n");

                        scanf("%d",&value);

                        temp->data = value;

                        if(rear == NULL)
                        {
                            rear = temp;

                            rear->link = NULL;

                            front = rear;
                        }
                        else
                        {
                            rear->link = temp;

                            rear = temp;

                            rear->link = NULL;
                        }

                    }
                    void dequeue()
                    {
                        struct node*temp = front;

                        if(temp == NULL)
                        {
                            printf("Queue is Empty\n");

                        }
                        else
                        {
                            front = front->link;

                            free(temp);
                        }
                    }
                    void display()
                    {
                        struct node*temp = front;

                        if(temp!=NULL)
                        {
                            printf("Elements are: \n");

                            while(temp!=NULL)
                            {
                                printf("%d\t",temp->data);

                                temp = temp->link;

                            }printf("\n");
                        }
                        else
                        {
                            printf("queue is empty\n");
                        }

                    }

                



Output

    1.Enqueue
    2.Dequeue
    3.Display
    4.Exit

    Enter your choice:
    1

    enter the value to be pushed.
    10

    1.Enqueue
    2.Dequeue
    3.Display
    4.Exit

    Enter your choice:
    1

    enter the value to be pushed.
    20

    1.Enqueue
    2.Dequeue
    3.Display
    4.Exit

    Enter your choice:
    3

    Elements are:
    10      20

    1.Enqueue
    2.Dequeue
    3.Display
    4.Exit

    Enter your choice:
    2

    1.Enqueue
    2.Dequeue
    3.Display
    4.Exit

    Enter your choice:
    3

    Elements are:
    20

    1.Enqueue
    2.Dequeue
    3.Display
    4.Exit

    Enter your choice:
    4