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 element2.Delete an element3.Display the queue4.ExitEnter your choice : 1Enter the element : 11
Value inserted...------------------
1.Insert an element2.Delete an element3.Display the queue4.ExitEnter your choice : 1Enter the element : 22
Value inserted...------------------
1.Insert an element2.Delete an element3.Display the queue4.ExitEnter your choice : 3
Printing values...1122
------------------
1.Insert an element2.Delete an element3.Display the queue4.ExitEnter your choice : 2
Value deleted...------------------
1.Insert an element2.Delete an element3.Display the queue4.ExitEnter your choice : 4
--------------------------------Process exited after 286.3 seconds with return value 0Press any key to continue . . .
Also Read : C Program to Count Number of Digits in Given Number - Learn With AVRK