MASIGNASUKAv102
6510051498749449419

Queue Data-Structure Program in C Language - Learn With AVRK

Add Comments
Wednesday, September 1, 2021
Loading...

Performing insertion and deletion in queue data structure using C Language

#include<stdio.h>
#include<process.h>
void insert();
void delet();
void display();
int front = -1, rear = -1,maxsize;
int queue[100];
int main ()
{
    printf ("Enter the size of queue : ");
    scanf("%d",&maxsize);
    int choice;
    while(choice != 4)
    {
        printf("\n------------------\n");
        printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
        printf("\nEnter your choice : ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            insert();
            break;
            case 2:
            delet();
            break;
            case 3:
            display();
            break;
            case 4:
            exit(0);
            break;
            default:
            printf("\nEnter valid choice..\n");
        }
    }
}
void insert()
{
    int item; 
    printf("\nEnter the element : ");
    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 delet()
{
    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("\nprinting values .....\n");
        for(i=front;i<=rear;i++)
        {
            printf("\n%d\n",queue[i]);
        }
    }
}
You mast run the above code using Dev C++ compiler to get the exact output given below -

Output -

Enter the size of queue : 4

------------------

1.Insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice : 1
Enter the element : 11

Value inserted...
------------------

1.Insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice : 1
Enter the element : 22

Value inserted...
------------------

1.Insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice : 3

Printing values...
11
22

------------------

1.Insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice : 2

Value deleted...
------------------

1.Insert an element
2.Delete an element
3.Display the queue
4.Exit
Enter your choice : 4

--------------------------------
Process exited after 286.3 seconds with return value 0
Press any key to continue . . .
Also Read : C Program to Count Number of Digits in Given Number - Learn With AVRK
Learn With AVRK

Learn With AVRK