Queue
Queue
Queue
Definition of Queue
⚫ Queue is an abstract data structure, somewhat similar
to Stacks. Unlike stacks, a queue is open at both its
ends. One end is always used to insert data (enqueue)
and the other is used to remove data (dequeue).
⚫ Queue follows First-In-First-Out methodology, i.e.,
the data item stored first will be accessed first.
Basic operations in Queue
Two basic operation:
⚫ enqueue() − add (store) an item to the queue.
⚫ dequeue() − remove (access) an item from the queue.
⚫ This function helps to see the data at the front of the queue.
The algorithm of peek() function is as follows −
⚫ Algorithm
begin procedure peek
return queue[front]
end procedure
▪ Implementation of peek() function in C programming
language −
Example
int peek()
{
return queue[front];
}
isfull()
}
deQueue
int dequeue()
Operation
{
if((front==-1) && (rear==-1)) // condition to check queue is empty
{
printf("\nQueue is underflow..");
}
else if(front==rear)
{
printf("\nThe dequeued element is %d", queue[front]);
front=-1;
rear=-1;
}
else
{
printf("\nThe dequeued element is %d", queue[front]);
front=(front+1)%max;
}
}
Displaying Elements of Queue
void display()
{
int i=front;
if(front==-1 && rear==-1)
{
printf("\n Queue is empty..");
}
else
{
printf("\nElements in a Queue are :");
while(i<=rear)
{
printf("%d,", queue[i]);
i=(i+1)%max;
}
}
Time Complexity
⚫ Time complexity is the amount of time taken by an
algorithm to run, as a function of the length of the
input .
⚫ Time complexity of enQueue(), deQueue() operation is
O(1) as there is no loop in any of the operation.
Applications of Circular Queue