QUEUE Lab Program(KSWU)
SOURCE CODE :
#include<stdio.h>
#include<time.h>
#define max 50
void insert();
void delete();
void display();
int queue_array[max];
int rear=-1;
int front=-1;
void main()
{
int choice;
clock_t start,end;
double cpu_time_used;
clrscr();
start = clock();
while(1)
{
printf("1. insert \n");
printf("2. delete \n");
printf("3.display \n");
printf("4.exit\n");
printf("enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("wrong choice \n");
}
end = clock();
cpu_time_used=((double)(end-start))/CLOCKS_PER_SEC;
printf("the time taken for execution is %f seconds",cpu_time_used);
getch();
}
}
void insert()
{
int add_item;
if(rear==max-1)
printf("queue is overflow \n");
else
{
if (front==-1)
front=0;
printf("insert the element in queue");
scanf("%d",&add_item);
rear=rear+1;
queue_array[rear]=add_item;
}
}
void delete()
{
if (front==-1||front>rear)
{
printf("queue underflow \n");
return;
}
else
{
printf("element deleted from queue is %d \n",queue_array[front]);
front=front+1;
}
}
void display()
{
int i;
if(front==-1)
printf("queue is empty\n");
else
{
printf("queue is \n");
for (i=front;i<=rear;i++)
printf("%d",queue_array[i]);
printf("\n");
}
}
No comments:
Post a Comment