#include<stdio.h>
#include<conio.h>
#define MAX 20 //defining size of queue
void insert(int); //function declaration for insertion
int del(); //function declaration for deletion
void display(); //function declaration for displaying queue
int queue[MAX]; //queue array
int front=-1,rear=-1; /*initialy setting both parameters to -1 position i.e out of the array*/
main()
{
int choice,num; //variables that we require in the entire program
while(1) //for repeatingly asking the user whether to continue the program?
{
printf("Enter your choice\n");
printf("1. INSERT \n");
printf("2. DELETE \n");
printf("3. DISPLAY \n");
printf("4. EXIT \n");
scanf("%d",&choice); //getting the choice of the user
switch(choice) //using switch statement to select the working of the entered(desired) choice
{
case 1: //If user wishes to insert element in the queue
printf("Enter number to be inserted in the queue: ");
scanf("%d",&num); //getting the element to be inserted from the user
insert(num); //calling insert function
break; //jumping out of switch statement
case 2://If user wishes to delete element from the queue
num=delete(); //calling delete function and saving the result in an int variable(num)
break;
case 3://If user wishes to display the queue
display(); //calling display function
printf("\n");
break;
case 4: //If user wishes to exit(stop continuing the program)
exit(1); //end of while loop
default ://If user enters a number other than 1 to 4
printf("INVALID CHOICE \n");
}
}
}
//FUNCTION DEFINATION
void insert (int x) //value of number to be inserted(num) gets copied in 'x' when function is called
{
if(rear==MAX-1) //rear is at the last position in queue array
{
printf("Queue is full (OVERFLOW)\n");
return; //returning control back to calling function
}
if (front==-1)//only if we wish to insert 1st element in the queue
{
front=0;//front is set to 1st position i.e queue[0] and here the first insertion will take place
}
rear=rear+1; //insertion take place at REAR
queue[rear]=x;//element to be inserted is placed at the rear position of queue
}//for eg: for 1st insertion: front is made 0 and rear is also made 0(-1+1)
//and so the insertion will take place at queue[0]
//for next insertion rear will be 1(0+1) and front will remain 0 as insertion occurs at REAR
int delete () //for deleting element from queue
{
int x;
if(front==-1 || front==rear+1) //front==-1 means front is yet not incremented or set to 0 for first insertion
//front==rear+1 : if for eg front=0,rear=2
//for first deletion front=1,rear=2;for second deletion front=2,rear=2;now if front=3(rear+1),there will be no element in the queue
{
printf("Queue is empty (UNDERFLOW) \n");
}
x=queue[front];//saving value of element at FRONT(to be deleted) in a variable
front=front+1;//deletion occurs at FRONT,and so we increment it for each deletion
printf("%d has been deleted \n",x);
return x;
}
void display() //for displaying queue
{
int i;
if(front==-1 || front==rear+1) //checking if queue is empty
{
printf("Queue is EMPTY \n");
}
//if queue is not empty:
for(i=front;i<=rear;i++)//start printing elements of queue from FRONT to REAR
{
printf("%d ",queue[i]);//displaying QUEUE
}
}
#include<conio.h>
#define MAX 20 //defining size of queue
void insert(int); //function declaration for insertion
int del(); //function declaration for deletion
void display(); //function declaration for displaying queue
int queue[MAX]; //queue array
int front=-1,rear=-1; /*initialy setting both parameters to -1 position i.e out of the array*/
main()
{
int choice,num; //variables that we require in the entire program
while(1) //for repeatingly asking the user whether to continue the program?
{
printf("Enter your choice\n");
printf("1. INSERT \n");
printf("2. DELETE \n");
printf("3. DISPLAY \n");
printf("4. EXIT \n");
scanf("%d",&choice); //getting the choice of the user
switch(choice) //using switch statement to select the working of the entered(desired) choice
{
case 1: //If user wishes to insert element in the queue
printf("Enter number to be inserted in the queue: ");
scanf("%d",&num); //getting the element to be inserted from the user
insert(num); //calling insert function
break; //jumping out of switch statement
case 2://If user wishes to delete element from the queue
num=delete(); //calling delete function and saving the result in an int variable(num)
break;
case 3://If user wishes to display the queue
display(); //calling display function
printf("\n");
break;
case 4: //If user wishes to exit(stop continuing the program)
exit(1); //end of while loop
default ://If user enters a number other than 1 to 4
printf("INVALID CHOICE \n");
}
}
}
//FUNCTION DEFINATION
void insert (int x) //value of number to be inserted(num) gets copied in 'x' when function is called
{
if(rear==MAX-1) //rear is at the last position in queue array
{
printf("Queue is full (OVERFLOW)\n");
return; //returning control back to calling function
}
if (front==-1)//only if we wish to insert 1st element in the queue
{
front=0;//front is set to 1st position i.e queue[0] and here the first insertion will take place
}
rear=rear+1; //insertion take place at REAR
queue[rear]=x;//element to be inserted is placed at the rear position of queue
}//for eg: for 1st insertion: front is made 0 and rear is also made 0(-1+1)
//and so the insertion will take place at queue[0]
//for next insertion rear will be 1(0+1) and front will remain 0 as insertion occurs at REAR
int delete () //for deleting element from queue
{
int x;
if(front==-1 || front==rear+1) //front==-1 means front is yet not incremented or set to 0 for first insertion
//front==rear+1 : if for eg front=0,rear=2
//for first deletion front=1,rear=2;for second deletion front=2,rear=2;now if front=3(rear+1),there will be no element in the queue
{
printf("Queue is empty (UNDERFLOW) \n");
}
x=queue[front];//saving value of element at FRONT(to be deleted) in a variable
front=front+1;//deletion occurs at FRONT,and so we increment it for each deletion
printf("%d has been deleted \n",x);
return x;
}
void display() //for displaying queue
{
int i;
if(front==-1 || front==rear+1) //checking if queue is empty
{
printf("Queue is EMPTY \n");
}
//if queue is not empty:
for(i=front;i<=rear;i++)//start printing elements of queue from FRONT to REAR
{
printf("%d ",queue[i]);//displaying QUEUE
}
}