c program to implement queue operations using array
#include<stdio.h>
//#include<conio.h>
#define MAX 10
int queue[MAX];
int front =-1,rear=-1;
void insert(void);
int delete_element(void);
int peep(void);
void display(void);
main()
{
int option,val;
do{
printf("\n *******MAIN MENU**********");
printf("\n 1.Insert an element");
printf("\n 2.Delete an element");
printf("\n 3.peep");
printf("\n 4.display");
printf("\n 5.Exit");
printf("\n********************************");
printf("\n Enter your option\n");
scanf("%d",&option);
switch(option)
{
case 1:
insert();
break;
case 2:
val=delete_element();
printf("\n The number that was deleted is :%d",val);
break;
case 3:
val=peep();
printf("\n The first value in the queue is:%d",val);
break;
case 4:
display();
break;
}
}while(option!=5);
//getch();
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue \n");
scanf("%d",&num);
if(rear==MAX-1)
printf("\n Overflow");
else
if(front==-1 && rear==-1)
front=rear=0;
else
rear++;
queue[rear]=num;
}
int delete_element()
{
int val;
if (front==-1 || front>rear)
{
printf("\n Underflow \n");
return -1;
}
else
{
val=queue[front];
front++;
return val;
}
}
int peep()
{
return queue[front];
}
void display()
{
int i;
printf("\n");
for(i=front;i<=rear;i++)
printf("\t %d",queue[i]);
}
//#include<conio.h>
#define MAX 10
int queue[MAX];
int front =-1,rear=-1;
void insert(void);
int delete_element(void);
int peep(void);
void display(void);
main()
{
int option,val;
do{
printf("\n *******MAIN MENU**********");
printf("\n 1.Insert an element");
printf("\n 2.Delete an element");
printf("\n 3.peep");
printf("\n 4.display");
printf("\n 5.Exit");
printf("\n********************************");
printf("\n Enter your option\n");
scanf("%d",&option);
switch(option)
{
case 1:
insert();
break;
case 2:
val=delete_element();
printf("\n The number that was deleted is :%d",val);
break;
case 3:
val=peep();
printf("\n The first value in the queue is:%d",val);
break;
case 4:
display();
break;
}
}while(option!=5);
//getch();
return 0;
}
void insert()
{
int num;
printf("\n Enter the number to be inserted in the queue \n");
scanf("%d",&num);
if(rear==MAX-1)
printf("\n Overflow");
else
if(front==-1 && rear==-1)
front=rear=0;
else
rear++;
queue[rear]=num;
}
int delete_element()
{
int val;
if (front==-1 || front>rear)
{
printf("\n Underflow \n");
return -1;
}
else
{
val=queue[front];
front++;
return val;
}
}
int peep()
{
return queue[front];
}
void display()
{
int i;
printf("\n");
for(i=front;i<=rear;i++)
printf("\t %d",queue[i]);
}
Comments
Post a Comment