Q UEUE
Q UEUE
Q UEUE
#include<stdio.h>
#define MAX 3
int queue[MAX];
int front = -1;
int rear = -1;
int item;
void enqueue();
void dequeue();
void display();
void main()
{
int ch = 0;
while(ch != 4)
{
printf("\n1. Enqueue\n2. Dequeue\n3. Display\n4. Exit\n");
printf("Enter the choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
case 4:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
}
}
void enqueue()
{
if(rear == MAX - 1)
{
printf("Overflow\n");
}
else
{
printf("Insert the element: ");
scanf("%d", &item);
void dequeue()
{
if(front == -1 && rear == -1)
{
printf("Underflow\n");
}
else
{
item = queue[front];
printf("Deleted %d\n", item);
if(front == rear)
{
front = -1;
rear = -1;
}
else
{
front = front + 1;
}
}
}
void display()
{
if(front == -1)
{
printf("Queue is empty\n");
}
else
{
printf("Queue elements are: ");
for(int i = front; i <= rear; i++)
{
printf("%d ", queue[i]);
}
printf("\n");
}
}
OUTPUT
ubuntu@ubuntu-H81M-S:~/AshwinHarikumar-10$ ./a.out
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Insert the element in queue: 12
Inserted 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Insert the element in queue: 14
Inserted 14
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 1
Insert the element in queue: 15
Inserted 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 2
Deleted 12
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 3
Queue elements are: 14 15
1. Enqueue
2. Dequeue
3. Display
4. Exit
Enter your choice: 4
Exiting...