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 :
|
C - Queue Operations |
|
C- Queue Overflow |
|
C - Queue Underflow |
Download code