Queue
Queue
Queue
CODE:
#include <stdio.h>
#define MAX 50
void insert();
void delet();
void display();
int queue_array[MAX];
int r = - 1;
int f = - 1;
int main()
{
int choice;
while (1)
{
printf("1.Insert element to queue \n");
printf("2.Delete element from queue \n");
printf("3.Display all elements of queue \n");
printf("4.Quit \n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delet();
break;
case 3:
display();
break;
case 4:
break;
default:
printf("Wrong choice \n");
}
}
}
void insert()
{
int add_item;
if (r == MAX - 1)
printf("Queue Overflow \n");
else
{
if (f == - 1)
f = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
r = r + 1;
queue_array[r] = add_item;
}
}
void delet()
{
if (f == - 1 || f > r)
{
printf("Queue Underflow \n");
return ;
}
else
{
printf("Element deleted from queue is : %d\n", queue_array[f]);
f = f + 1;
}
}
void display()
{
int i;
if (f == - 1)
printf("Queue is empty \n");
else
{
printf("Queue is : \n");
for (i = f; i <= r; i++)
printf("%d ", queue_array[i]);
printf("\n");
}
}
Circular Queue
ALGORITHM:
Algorithm to enqueue an element in a circular queue:
Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]
TRAVERSE:
Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]
Step 4: EXIT\.
CODE:
#include <stdio.h>
# define max 6
int queue[max];
int f=-1;
int r=-1;
int dequeue()
{
if((f==-1) && (r==-1))
{
printf("\nQueue is underflow..");
}
else if(f==r)
{
printf("\nThe dequeued element is %d", queue[f]);
f=-1;
r=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[f]);
f=(f+1)%max;
}
}
void display()
{
int i=f;
if(f==-1 && r==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=r)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
}
int main()
{
int choice=1,x;
switch(choice)
{
case 2: dequeue();
break;
case 3: display();
case 4: exit(1);
default:
printf("Wrong choice \n");
}
return 0;
}
OUTPUT: