0

C Program to perform different operations on queue.

So here we have a program to perform various operations  on Queue elements like

  • Insert
  • Delete
  • Peek

If you don’t know about queues visit this blog:

 

#include<stdio.h>

#include<conio.h>

#define max 10

int queue[max];

int front=-1,rear=-1;

/*Here all the header files are included and the max capacity of queue is set to 10 by using #define max 10. front and rear of queue are set to -1 and -1 which means queue is empty.*/

void insert(void)

{

int num;

printf(“\n enter the no. to be inserted: “);

scanf(“%d”,&num);

if(rear==max-1)

printf(“\n overflow”);

else if(front==-1 && rear==-1)

front=rear=0;

else

rear++;

queue[rear]=num;

}

/*here first if condition is checking weather the queue is full or not if it is then print overflow. Then if it is empty i.e front and rear both are -1. then it is incremented to index 0 and element is inserted in it. the queue will be filled till it becomes full.*/

int delete_element(void)                   

{

int val;

if(front==-1 || front>rear)               

{

printf(“\n underflow”);

return -1;

}

else

{

val=queue[front];

front++;

if(front>rear)

front=rear=-1;

return val;

}

}

/*Here first it is checked that if the queue is empty if yes then no deletion can take place. if queue has some elements then deletion take place from front by increasing the value of front so that the queue front of the queue becomes the new index and the deleted element is not part of queue. when the value of front become more than rear it means that all the elements are deleted so both front and rear are declared as -1.*/

int peek(void)                                      

{

if(front==-1 || front>rear)

{

printf(“\n queue is empty”);

return 1;

}

else

{

return queue[front];

}

}

void display()                                     

{

int i;

printf(“\n”);

if(front==-1 || front>rear)

printf(“\n queue is empty”);

else

{

for(i=front;i<=rear;i++)

printf(“\t %d”,queue[i]);

}

}

/* so here we have two functions peek to look at the first element in the queue which is done by just getting the value of queue on the index equal to front i.e. queue[front]. also condition should be checked if queue is empty.*/

/*second function is display here too first condition is checked of empty queue because if queue is empty how can we display elements. so after that the for loops start from the index front till the rear and elements with index front are printed with incrementing the front in every loop cycle.*/

int main()

{

int option,val;

clrscr();

printf(“\n ***************************”);

do

{

printf(“\n 1.insert an element”);

printf(“\n 2.delete an element”);

printf(“\n 3.peek”);

printf(“\n 4.display”);

printf(“\n 5.exit”);

printf(“\n enter your option”);

scanf(“%d”, &option);

switch(option)

{

case 1:insert();

break;

case 2:val=delete_element();

if(val!=-1)

printf(“\n the no. deleted is: %d”,val);

break;

case 3:val=peek();

printf(“\n  the first value in queue is: %d”,val);

break;

case 4:display();

break;

}

}while(option!=5);

getch();

return 0;  }

/* and at last we have main function with switch in it which calls the needed function according to choice*/

ks007

Leave a Reply

Your email address will not be published. Required fields are marked *