/* Simple Queue using array */
#include<stdio.h>
#include<conio.h>
#define size 5
struct Queue{
int rear,front,Q[size];
}q;
int qfull()
{
if(q.rear>=size-1)
{
return 1;
}
else
{
return 0;
}
}
int qempty()
{
if(q.front==-1 || q.front>q.rear)
{
return 1;
}
else
{
return 0;
}
}
void insert(int x)
{
if(q.front==-1)
{
q.front++;
}
q.rear++;
q.Q[q.rear]=x;
}
int del()
{
int i;
i=q.Q[q.front];
q.front++;
return i;
}
void display()
{
int i;
printf("\n");
for(i=q.front;i<=q.rear;i++)
{
printf("%d ",q.Q[i]);
}
printf("\n");
}
void main()
{
int choice,value,d;
q.front=-1;
q.rear=-1;
while(1)
{
printf("\n1.Insert\n2.delete\n3.display\n4.exit\nEnter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:if(qfull())
{
printf("\nQueue is full\n");
}
else
{
printf("\nEnter data\n");
scanf("%d",&value);
insert(value);
}
break;
case 2:if(qempty())
{
printf("\n\Queue is empty can't delete\n");
}
else
{
d=del();
printf("\n\deleted element is %d\n",d);
}
break;
case 3:if(qempty())
{
printf("\nAdd at least one item\n");
}
else
{
display();
}
break;
case 4:exit(0);
break;
default:printf("\nEnter proper choice\n");
}
}
}
#include<stdio.h>
#include<conio.h>
#define size 5
struct Queue{
int rear,front,Q[size];
}q;
int qfull()
{
if(q.rear>=size-1)
{
return 1;
}
else
{
return 0;
}
}
int qempty()
{
if(q.front==-1 || q.front>q.rear)
{
return 1;
}
else
{
return 0;
}
}
void insert(int x)
{
if(q.front==-1)
{
q.front++;
}
q.rear++;
q.Q[q.rear]=x;
}
int del()
{
int i;
i=q.Q[q.front];
q.front++;
return i;
}
void display()
{
int i;
printf("\n");
for(i=q.front;i<=q.rear;i++)
{
printf("%d ",q.Q[i]);
}
printf("\n");
}
void main()
{
int choice,value,d;
q.front=-1;
q.rear=-1;
while(1)
{
printf("\n1.Insert\n2.delete\n3.display\n4.exit\nEnter your choice:\n");
scanf("%d",&choice);
switch(choice)
{
case 1:if(qfull())
{
printf("\nQueue is full\n");
}
else
{
printf("\nEnter data\n");
scanf("%d",&value);
insert(value);
}
break;
case 2:if(qempty())
{
printf("\n\Queue is empty can't delete\n");
}
else
{
d=del();
printf("\n\deleted element is %d\n",d);
}
break;
case 3:if(qempty())
{
printf("\nAdd at least one item\n");
}
else
{
display();
}
break;
case 4:exit(0);
break;
default:printf("\nEnter proper choice\n");
}
}
}
No comments:
Post a Comment