/*Circular Queue -*/
#include<stdio.h>
#include<conio.h>
# define size 5
struct queue
{
int rear,front,Q[size];
}q;
void insert(int item)
{
if ((q.front == 0 && q.rear == size-1) || (q.front == q.rear+1))
{
printf("\nQueue Overflow\n");
return;
}
if (q.front == -1)
{
q.front = 0;
q.rear = 0;
}
else
{
if (q.rear == size - 1)
q.rear = 0;
else
q.rear = q.rear + 1;
}
q.Q[q.rear] = item ;
}
void del()
{
if (q.front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("\nDeleted element is %d\n",q.Q[q.front]);
if (q.front == q.rear)
{
q.front = -1;
q.rear = -1;
}
else
{
if (q.front == size - 1)
q.front = 0;
else
q.front = q.front + 1;
}
}
void display()
{
int i = q.front, j=q.rear;
if (q.front == -1)
{
printf("Queue is empty");
return;
}
printf("\nQueue is :\n");
if (i <= j)
{
while (i <= j)
{
printf("%d ",q.Q[i]);
i++;
}
}
else
{
while (i <= size - 1)
{
printf("%d ",q.Q[i]);
i++;
}
i = 0;
while (i <= j)
{
printf("%d ",q.Q[i]);
i++;
}
}
}
void main()
{
int choice,data;
q.front=-1;
q.rear=-1;
while(1)
{
printf("\n1.Insert\n2.Delete\n3.display\n4.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter data\n");
scanf("%d",&data);
insert(data);
break;
case 2:del();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("\nEnter proper choice\n");
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
# define size 5
struct queue
{
int rear,front,Q[size];
}q;
void insert(int item)
{
if ((q.front == 0 && q.rear == size-1) || (q.front == q.rear+1))
{
printf("\nQueue Overflow\n");
return;
}
if (q.front == -1)
{
q.front = 0;
q.rear = 0;
}
else
{
if (q.rear == size - 1)
q.rear = 0;
else
q.rear = q.rear + 1;
}
q.Q[q.rear] = item ;
}
void del()
{
if (q.front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("\nDeleted element is %d\n",q.Q[q.front]);
if (q.front == q.rear)
{
q.front = -1;
q.rear = -1;
}
else
{
if (q.front == size - 1)
q.front = 0;
else
q.front = q.front + 1;
}
}
void display()
{
int i = q.front, j=q.rear;
if (q.front == -1)
{
printf("Queue is empty");
return;
}
printf("\nQueue is :\n");
if (i <= j)
{
while (i <= j)
{
printf("%d ",q.Q[i]);
i++;
}
}
else
{
while (i <= size - 1)
{
printf("%d ",q.Q[i]);
i++;
}
i = 0;
while (i <= j)
{
printf("%d ",q.Q[i]);
i++;
}
}
}
void main()
{
int choice,data;
q.front=-1;
q.rear=-1;
while(1)
{
printf("\n1.Insert\n2.Delete\n3.display\n4.exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("\nEnter data\n");
scanf("%d",&data);
insert(data);
break;
case 2:del();
break;
case 3:display();
break;
case 4:exit(0);
break;
default:printf("\nEnter proper choice\n");
}
}
getch();
}
No comments:
Post a Comment