Code Scrappers


Queue Using Array C Program


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

                    #define SIZE 10

                    void enqueue();

                    void dequeue();

                    void display();

                    int queue[SIZE], front = 0, rear = 0, i;

                    void main()

                    {
                        int choice;

                        printf("queue implemetation\n");

                        while(1)

                        {
                            printf("1. enqueue\n 2. dequeue\n 3. display\n 4. 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;

                                default: printf("invalid input\n");
                                break;


                            }
                        }
                    }

                    void enqueue()

                    {
                        int value;

                        if(rear >= SIZE)

                        {
                            printf("queue is full\n");

                        }

                        else

                        {
                            printf("enter the data\n");

                            scanf("%d",&value);

                            queue[rear] = value;

                            rear++;

                        }
                    }

                    void dequeue()

                    {
                        if(rear == front)

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

                        }

                        else

                        {
                            for(i = 0;i <= rear - 1;i++)

                            queue[i] = queue[i+1];

                            rear--;

                        }
                    }

                    void display()

                    {
                        if(rear == front)

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

                        }

                        else

                        {
                            for(i = front; i < rear; i++)

                            {
                                printf("%d\n",queue[i]);

                            }
                        }

                    }

                



Output

          queue implemetation

           1. enqueue
           2. dequeue
           3. display
           4. exit

          enter your choice
          1

          enter the data
          10

           1. enqueue
           2. dequeue
           3. display
           4. exit

          enter your choice
          1

          enter the data
          20

           1. enqueue
           2. dequeue
           3. display
           4. exit

          enter your choice
          1

          enter the data
          30

           1. enqueue
           2. dequeue
           3. display
           4. exit

           enter your choice
           1

           enter the data
           40

            1. enqueue
            2. dequeue
            3. display
            4. exit

           enter your choice
           3

           10
           20
           30
           40

            1. enqueue
            2. dequeue
            3. display
            4. exit

           enter your choice
           2

            1. enqueue
            2. dequeue
            3. display
            4. exit

           enter your choice
           2

           1. enqueue
           2. dequeue
           3. display
           4. exit

          enter your choice
          3
          30
          40

           1. enqueue
           2. dequeue
           3. display
           4. exit

          enter your choice
          4