Queue
Queue
Queue
National Institute of Science & Technology
• Queue is an abstract data structure, somewhat similar to Stacks,Unlike stacks, a queue is open at both
NIST UNIVERSITY
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.
• The most fundamental operations in the queue ADT include: enqueue(), dequeue(), peek(), isFull(),
isEmpty(). These are all built-in operations to carry out data manipulation and to check the status of the
queue.
• Queue uses two pointers − front and rear. The front pointer accesses the data from the front end
(helping in enqueueing) while the rear pointer accesses data from the rear end (helping in dequeuing).
1. Enqueue:
NIST UNIVERSITY
Algorithm:
1. START Step 1: IF REAR=MAX-1
PRINT “ Overflow”
2. Check if the queue is full.
GOTO Step 4
3. If the queue is full, produce overflow error and exit.
[END OF IF]
4. If the queue is not full, increment rear pointer to
Step 2: IF FRONT= -1 and REAR= -1
point the next empty space.
SET FRONT = REAR = 0
5. Add data element to the queue location, where the ELSE
rear is pointing.
SET REAR= REAR+1
6. return success. [END OF IF]
7. END Step 3: SET QUEUE[REAR]= VALUE
Step 4: END
1. Dequeue:
NIST UNIVERSITY
Algorithm:
1. START
Step 1: IF FRONT= -1 or FRONT > REAR
2. Check if the queue is empty.
PRINT “ Underflow”
3. If the queue is empty, produce underflow error and
ELSE
exit.
SET VALUE = QUEUE[FRONT]
4. If the queue is not empty, access the data where
SET FRONT = FRONT + 1
front is pointing.
[ END OF IF]
5. Increment front pointer to point to the next available
Step 2: END
data element.
6. Return success.
7. END
3. PEEK
NIST UNIVERSITY
Algorithm:
1. START
2. Return the element at the front of the queue
3. END Step 1: IF front == -1
Print "Queue is Empty“
Else
Return Queue[front]
Step 2: END
4. isFull
NIST UNIVERSITY
Algorithm:
1. START
2. If the count of queue elements equals the queue size,
return true Step 1: If rear == MAX – 1
3. Otherwise, return false Return True // Queue is full
4. END Else
Return False // Queue is not full
Step 2: END OF IF
5. isEmpty
NIST UNIVERSITY
Algorithm:
1. START
2. If the count of queue elements equals zero, return true
3. Otherwise, return false Step 1: If front == -1
4. END Return True // Queue is empty
Else
Return False // Queue is not empty
Step 2: END OF IF
#include <stdio.h>
NIST UNIVERSITY
{ if (isEmpty()) {
if (isEmpty()) printf("Queue is empty.\n");
{ return;
printf("Queue is empty. Cannot dequeue.\n"); }
return -1; // Return a sentinel value printf("Queue elements: ");
} for (int i = q.front; i <= q.rear; i++) {
int data = q.arr[q.front++]; printf("%d ", q.arr[i]);
if (q.front > q.rear) }
{ printf("\n");
q.front = q.rear = -1; }
}
return data;
}
{ Inserted: 10
enqueue(10); Inserted: 20
enqueue(20); Inserted: 30
enqueue(30); Inserted: 40
enqueue(40); Inserted: 50
enqueue(50); Queue is full. Cannot insert data.
enqueue(60); Queue elements: 10 20 30 40 50
display(); Removed: 10
printf("Removed: %d\n", dequeue()); Removed: 20
printf("Removed: %d\n", dequeue()); Queue elements: 30 40 50
display();
return 0;
}
2. Circular Queue
3. Double Ended Queue (or Deque)
1. Linear Queue
In Linear Queue, an insertion takes place from one end while the deletion occurs from another end. The
end at which the insertion takes place is known as the rear end, and the end at which the deletion takes
place is known as front end. It strictly follows the FIFO rule.
The major drawback of using a linear Queue is that insertion is done only from the rear end. If the first
NIST UNIVERSITY
three elements are deleted from the Queue, we cannot insert more elements even though the space is
available in a Linear Queue. In this case, the linear Queue shows the overflow condition as the rear is
pointing to the last element of the Queue.
2. Circular Queue
In Circular Queue, all the nodes are represented as circular. It is similar to the linear Queue except that the
last element of the queue is connected to the first element. It is also known as Ring Buffer, as all the ends
are connected to another end.
The drawback that occurs in a linear queue is overcome by using the circular queue. If the empty space
NIST UNIVERSITY
is available in a circular queue, the new element can be added in an empty space by simply
incrementing the value of rear. The main advantage of using the circular queue is better memory
utilization.
1. Enqueue:
NIST UNIVERSITY
Step 1: IF FRONT = -1
1. Dequeue:
NIST UNIVERSITY
# define max 6 }
int queue[max]; // array declaration else {
int front=-1; rear=(rear+1)%max; // rear is incremented
int rear=-1; queue[rear]=element; // assigning a value to the
void enqueue(int element) queue at the rear position.
{ }
if(front==-1 && rear==-1) }
{ int dequeue()
front=0; {
rear=0; if((front==-1) && (rear==-1)) // condition to check
queue[rear]=element; queue is empty
} {
else if((rear+1)%max==front) printf("\nQueue is underflow..");
{ }
else if(front==rear)
NIST UNIVERSITY
The deque stands for Double Ended Queue. Deque is a linear data structure where the insertion
and deletion operations are performed from both ends. We can say that deque is a generalized
version of the queue.
Though the insertion and deletion in a deque can be performed on both ends, it does not follow
the FIFO rule. The representation of a deque is given as follows -
Types of deque
NIST UNIVERSITY
In output restricted queue, deletion operation can be performed at only one end, while insertion
can be performed from both ends.
Check empty
This operation is performed to check whether the deque is empty or not. If front = -1, it
means that the deque is empty.
Check full
This operation is performed to check whether the deque is full or not. If front = rear + 1, or
front = 0 and rear = n - 1 it means that the deque is full.
The time complexity of all of the above operations of the deque is O(1), i.e., constant.
Enter choice: 2
case 6: printf("Exiting...\n");
NIST UNIVERSITY
Enter value: 20
return 0;
Inserted 20 at rear.
default:
Enter choice: 1
printf("Invalid choice! Try again.\n");
Enter value: 5
} } }
Inserted 5 at front.
Output:
Enter choice: 5
1. Insert at Front
Deque elements: 5 10 20
2. Insert at Rear
Enter choice: 3
3. Delete from Front
Deleted 5 from front.
4. Delete from Rear
Enter choice: 4
5. Display
Deleted 20 from rear.
6. Exit
Enter choice: 5
Enter choice: 2
Deque elements: 10
Enter value: 10
Enter choice: 6
Inserted 10 at rear.