Queue is a data structure , similar to
Stack , but here in queue we have openings at both the ends , one for insertion and other for deletion.
Some of the common operations of queue are :
- Adding an item to queue(enqueue)
- Removing an item from a queue(dequeue)
- Overflow Condition(Queue full)
- Underflow Condition(Queue empty)
- Getting the front element(peek)
|
|
Overflow situation occur when the queue is full and then we are trying to insert/add another item to the queue .
Underflow situation occur when the queue is empty and the we are trying to remove/delete an item from the queue.
The principle of queue is First In First Out (FIFO).
Download code
PROGRAM :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int rear = -1,front = -1,item,MAX ,choice ,i;
printf("Enter Size of Queue : \t ");
scanf("%d" , &MAX);
int QueueArray[MAX];
while (1)
{
printf("\nQueue Operations :");
printf("\n1. Insert/Add");
printf("\n2. Remove/Delete");
printf("\n3. Peek/Front item");
printf("\n4. Display Queue");
printf("\n5. Exit");
printf("\nEnter your choice :\t");
scanf("%d" , &choice);
switch (choice)
{
case 1:
if (rear == MAX - 1)
{
printf("Queue Overflow \n");
}
else
{
if (front == -1)
{
front = 0;
}
printf("Inset the element in queue : \t");
scanf("%d" , &item);
rear = rear + 1;
QueueArray[rear] = item;
}
break;
case 2:
if (front == -1 || front > rear)
{
printf("Queue Underflow \n");
break;
}
else
{
printf("Element deleted from queue is %d \n" , QueueArray[front] );
front = front + 1;
}
break;
case 3:
if (front == -1 || front > rear)
{
printf("Queue Underflow \n");
break;
}
else
{
printf("The front element is : %d \t" , QueueArray[front]);
}
break;
case 4:
if (front == -1 || front > rear)
{
printf("Queue Underflow \n");
break;
}
else
{
printf("The current elements in queuue are :");
for (i = front; i <= rear; i++)
{
printf("%d " ,QueueArray[i]);
}
}
break;
case 5:
return 0;
break;
default:
printf("Wrong Entry \n ");
break;
}
}
return 0;
}
Download codeOUTPUT :
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQNLiMkM7Su6FDhFd2ANPfQY0H1VaK0tKfkaiv_sA_nbKlkNGA8hpUI8ndd6uTY3o8U5fHYrXFm8vS26B5TQ06jq4C7SPt2NBloMSf1if6-FvHA8rd6aoSkyd1L-RkOZG2e4TmvOaGuNU/s640/qc.PNG) |
C - Queue Operations |
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSRa02QpUeybFjhxsMYKKXLP6UMMVxQcgMcYeqXiT77ANDdPa-EtcPPDZCev-bsOOPSOQbtPCUZe0PbaeqkCa6noUit5e8ULjqtBW6vyaDtIBqJQJoM7jLqQOmvOl8lMPFCtGTtN2TxyE/s1600/qovrflw.PNG) |
C- Queue Overflow |
![](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_XjPJmlNGX-Ag_yw8Hj69fC2-EIx97hXfMfBC72UHU-jL70NmUfPZR5Hug07PTlOQ-W7N5AktPkcFKIeQBxfn_dijpEKSIJq90QTuR5RpeAanL0raxv5WgKGuADroDWXSH91OFnsk4uk/s1600/qunderFlow.PNG) |
C - Queue Underflow |
Download code