Queue Implementation using Array in C | codeitwise

Here we are going to write a program for queue implementation using array in c.

Queue follows FIFO rule i.e., First In First Out rule as the element that is inserted first is the first to be removed.

In queue, elements are added at one end called the Rear and are removed from another end called the Front.


queue representation



Queue Operations - 

We can perform two operations on queue i.e., insertion and deletion.

Insertion is done at Rear end and Deletion is done from Front end only.

A simple example of queue in real life is waiting in a line to buy a movie ticket.

Now lets write c program to implement queue using array i.e., a program to perform insert and delete operation on a queue.


Queue implementation using Array in C - 


#include <stdio.h>

#define max 10

int front = -1, rear=-1;
int queue[max];

void insert();
void delete();
void display();

int main()
{
    int ch;
    do
    {
        printf("----------Main Menu----------\n");
        printf("1. Insert Element in queue\n");
        printf("2. Delete Element in queue\n");
        printf("3. Display Queue\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &ch);
        switch (ch)
        {
        case 1:
            insert();
            break;
        
        case 2:
            delete();
            break;

        case 3:
            display();
            break;
        
        case 4:
            printf("Exited!");
            break;
            
        default:
            printf("Invalid Input!!!!!\n");
            break;
        }
    } while (ch != 4);

    return 0;
}

void insert()
{
    int n;
    printf("Enter number to be added: ");
    scanf("%d", &n);
    if(rear==max-1)
    {
        printf("Queue Overflow!!!!!\n");
    }
    else if (front==-1 && rear==-1){
        front=0;
        rear=0;
        queue[rear]=n;
    }
    else{
        rear++;
        queue[rear]=n;
    }
}

void delete()
{
    if(front==-1 || front>rear)
    {
        printf("Queue Undeflow!!!!!\n");
    }
    else{
        printf("Elememt %d deleted!\n",queue[front]);
        front++;
        if(front>rear)
        {
            front=-1;
            rear=-1;
        }
    }
}

void display()
{
    if(front==-1 || front>rear)
    {
        printf("Empty Queue!!!!!\n");
    }
    else{
        for(int i=front;i<=rear;i++)
        {
            printf("%d ",queue[i]);
        }
        printf("\n");
    }
}


Output - 


----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 2      
Queue Undeflow!!!!!
----------Main Menu----------
1. Insert Element in queue   
2. Delete Element in queue   
3. Display Queue
4. Exit
Enter your choice: 3
Empty Queue!!!!!
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 2
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 5
Invalid Input!!!!!
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 5
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 9
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 5
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 8
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 7
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 8
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 6
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 3
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 7
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 1
Enter number to be added: 5
Queue Overflow!!!!!
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 3
2 5 9 5 8 7 8 6 3 7 
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 2
Elememt 2 deleted!
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 3
5 9 5 8 7 8 6 3 7 
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 2
Elememt 5 deleted!
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 3
9 5 8 7 8 6 3 7
----------Main Menu----------
1. Insert Element in queue
2. Delete Element in queue
3. Display Queue
4. Exit
Enter your choice: 4
Exited!



Explanation of Implementation of Queue Using Array Program in C - 


Here front & rear variables are used to keep track of the queue. Variable max is used to store the maximum number of elements that the queue can hold. We have defined the value of max as 10 which can be altered if desired (e.g #define max 5).

front = -1 and rear = -1 indicates that queue is empty and rear = max-1 indicates that queue is full (remember indexing starts from 0).

We have declared front, rear and queue[max] outside main() function that means these are declared as global variables. 

In functions insert() and delete() we will do insertion of elements in queue and deletion of elements from queue respectively. 

We know that program starts executing from main() function so inside that we are creating a menu that will be displayed to the user asking him/her to do operations like insert, delete and display according to his/her choice. Then we are asking the user to input his/her choice, storing it in ch variable and passing the value of ch to switch statement and accordingly the functions will get executed.

Now, let us understand what exactly is happening inside each functions.


Insert Operation in Queue -

In insert() function, we are first checking if rear = max-1, as that means queue is full and no more insertion can be done. So if that is the case we are displaying a message stating Queue Overflow!. 

And if the condition is false then we are checking another condition if front = -1 and rear = -1 which means queue is empty. So if that is the case then we are assigning 0 to both front and rear and are storing the new element at 0th index.

And if this condition is also false then we are increasing the value of rear and are storing the new element at position given by queue[rear].


Delete Operation in Queue - 

In delete() function, we are first checking if front = -1 or front > rear, as that means queue is empty and no more element is there to be removed. So if that is the case we are displaying a message stating Queue Underflow!.

And if the condition is false then we are printing the value given by queue[front] stored in queue (as in queue elements are removed from front) which indeed is indicating deleted value (say front index is 0 then queue[0] = 4 is deleted) and are increasing the value of front. That means if before deleting front index was 0 then after deleting it has become 1.

One more thing we are doing here is that by going on deleting elements if front has become greater than rear then we are resetting front and rear values to -1 i.e., front = -1 and rear = -1.


In display() function, we are first checking if front = -1 or front > rear, as that means queue is empty. So if that is the case we are displaying a message stating Empty Queue!.

And if the condition is false, we are displaying the queue at current state.


Post a Comment

If you have any doubts, please let me know

Previous Post Next Post