loader image

C Program to implement queue operations using array

Aim: Implement queue operations using array in C

#include<stdio.h>   
#include<stdlib.h>  
void insert();  
void dequeue();  
void display();  
int front = -1, rear = -1 ,maxsize;  
int queue[100];  
int main ()  
{  
    int choice;   
    printf("\n Enter the size of QUEUE : ");
    scanf("%d",&maxsize);
    printf("\n QUEUE OPERATIONS USING ARRAY");
    printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit");  
    while(choice != 4)   
    {     
        printf("\nEnter your choice : ");  
        scanf("%d",&choice);  
        switch(choice)  
        {  
            case 1:  
            insert();  
            break;  
            case 2:  
            dequeue();  
            break;  
            case 3:  
            display();  
            break;  
            case 4:  
            exit(0);  
            break;  
            default:   
            printf("\nEnter valid choice??\n");  
        }  
    }
    return 0;
}  
void insert()  
{  
    int item;  
    printf("\nEnter the element\n");  
    scanf("\n%d",&item);      
    if(rear == maxsize-1)  
    {  
        printf("\nOVERFLOW\n");  
        return;  
    }  
    if(front == -1 && rear == -1)  
    {  
        front = 0;  
        rear = 0;  
    }  
    else   
    {  
        rear = rear+1;  
    }  
    queue[rear] = item;  
    printf("\nValue inserted ");  
      
}  
void dequeue()  
{  
    int item;   
    if (front == -1 || front > rear)  
    {  
        printf("\nUNDERFLOW\n");  
        return;  
              
    }  
    else  
    {  
        item = queue[front];  
        if(front == rear)  
        {  
            front = -1;  
            rear = -1 ;  
        }  
        else   
        {  
            front = front + 1;  
        }  
        printf("\nvalue deleted ");  
    }  
      
      
}  
      
void display()  
{  
    int i;  
    if(rear == -1)  
    {  
        printf("\nEmpty queue\n");  
    }  
    else  
    {   printf("\n Elements in the queue are\n");  
        for(i=front;i<=rear;i++)  
        {  
            printf("\n%d",queue[i]);  
        }     
    }  
}
Output
 Enter the size of QUEUE : 5

 QUEUE OPERATIONS USING ARRAY
1.insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice : 1

Enter the element
6

Value inserted
Enter your choice : 1

Enter the element
7

Value inserted
Enter your choice : 1

Enter the element
8

Value inserted
Enter your choice : 1

Enter the element
9

Value inserted
Enter your choice : 3

 Elements in the queue are

6
7
8
9
Enter your choice : 2

value deleted
Enter your choice : 3

 Elements in the queue are

7
8
9
Enter your choice : 2

value deleted
Enter your choice : 3

 Elements in the queue are

8
9
Enter your choice : 4
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Scroll to Top