Lec 7queue Final
Lec 7queue Final
By
Fatima Ubaid
Queue
• A queue is a linear list of elements in which deletions can take place only at one
end called the front, and insertions can take place only at the end called rear.
• Queue is a first in first out types of data structure(FIFO), the terms ‘Front’ and
‘Rear’ are used in describing a linear list only when it is implemented as a queue.
Types of Queue Data Structure
•Simple Queue
•Circular Queue
•Priority Queue
•Doubly Ended Queue
•Input Restricted Deque
•Output Restricted Deque
Circular Queue
• Types of dequeue :
• Input-restricted dequeue : In input-restricted dequeue, element can be
added at only one end but we can delete the element from both ends.
• Output-restricted dequeue : An output-restricted dequeue is a dequeue
where deletions take place at only one end but allows insertion at both
ends.
Priority Queue
• Mostly each of our queues will be maintained by a linear array QUEUE and two
pointer variables: FRONT containing the location of the Front element of the
queue and REAR, containing the location of the rear element of the queue.
0 1 2 3 4 5 6 7
• Whenever an element is added to the queue, the value of REAR is
• increased by 1
REAR = REAR + 1
0 1 2 3 4 5 6 7
The condition FRONT = null, will indicate that the queue is empty. Whenever an
element is deleted from the queue, the value of FRONT is increased by 1
Front = Front + 1
0 1 2 3 4 5 6 7
Total Elements in Queue
• This means that after N insertion the rear element of the queue will
occupy QUEUE[N] or queue will occupy the last part of the array. This
may occur even though the queue itself may not contain many elements.
class Queue {
int* arr;
int qfront;
int rear;
int size;
public:
Queue() {
size = 10;
arr = new int[size];
qfront = 0;
rear = 0;
}
Insertion / Enqueue
else
{ int ans = arr[qfront];
int dequeue() {
arr[qfront] = -1;
if(qfront == rear)
qfront++;
{
if(qfront==rear){
return -1;
qfront = 0;
Cout << “ Queue is
rear = 0;
Empty”;
}
}
return ans;
}
}
Is Empty
bool isEmpty() {
if(qfront == rear) {
return true;
}
else
{
return false;
}
}