Follow This Blog For more... 😊

What is Queue in Data Structure?| Simple Queue | Write a program to implement enqueue and dequeue in the simple queue. | Using C language | Data Structure |

🤔What is Queue in Data Structure?
👉A Queue is a linear data structure in which all the insertion(Enqueue) and deletion(Dequeue) of data or you can say its values are done at two different ends which are called rear and front respectively.

⇨ A queue has two pointers :
  1. Front pointer
  2. Rear pointer

⚙️Basic Operations in Queue :
Queue operations may involve initializing or defining the queue, utilizing it, and then completely erasing it from memory. Here we will try to understand the
basic operations associated with queues :

  • enqueue() - add (insert) an item to the queue. 
  • dequeue() - accessing the content while removing it.
Types of Queue :
1. Simple Queue (Linear Queue)
2. Circular Queue
3. Double Ended Queue
4. Priority Queue

We discuss each type in separate blogs.

⏺️Simple Queue (Linear Queue)
⇨ In a simple queue, insertion takes place at the rear (from the back end of the queue) and removal occurs at the front (from the front end of the queue).
⇨ It strictly follows the FIFO (First in First out) manner.

⏺️Drawbacks of Simple Queue
⇨ In a linear queue, the traversal through the queue is possible only once, for example: once an element is deleted, we cannot insert another element in its position. This disadvantage of a simple queue is overcome by a circular queue, thus saving memory.

👨‍💻Write a program to implement enqueue and dequeue in the simple queue.

CODE:

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

    int fp=-1,rp=-1;//fp means front-pointer & rp means rear-pointer.
    #define N 10
    int queue[N];

    void enqueue();
    void dequeue();
    void display();
    void operations_list();


    main()
    {
        int choice;
        operations_list();
        printf("Enter the choice:");
        scanf("%d",&choice);
        while(1)
        {
            switch(choice)
            {
                case 1:
                    {
                        enqueue();
                    }
                    break;
                case 2:
                    {
                        dequeue();
                    }
                    break;
                case 3:
                    {
                        display();
                    }
                    break;
                case 4:
                    {
                        exit(0);
                    }
                    break;
                default:
                {
                    printf("Enter choise between 1 to 4 !!!");
                }
            }
            operations_list();
            printf("Enter the choice:");
            scanf("%d",&choice);
        }
    }

    void operations_list()
    {
        printf("\nList_of_Operations:\n1.Enqueue\n2.Dequeue\n3.Display\n4.Exit\n");
    }

    void enqueue()
    {
        int data;
        if(rp>N-2)
        {
            printf("Queue is Overflow !!");
        }
        else
        {
            if(fp==-1)
            {
                fp=0;
            }
            rp++;
            printf("Enter the data:");
            scanf("%d",&data);
            queue[rp]=data;
        }
    }

    void dequeue()
    {
        if(fp==-1&&rp==-1||fp==rp+1)
        {
            printf("Queue is Underflow");
        }
        else
        {
            queue[fp]='\0';
            fp++;
        }
    }

    void display()
    {
        int i;
        for(i=fp;i<=rp;i++)
        {
            printf("Queue[%d]=%d\n",i,queue[i]);
        }
    }


OUTPUT:




Comments

Popular Posts