The document discusses different types of queues including simple queue, circular queue, priority queue, and deque. A simple queue follows FIFO principle but has limitations as items can only be inserted once. A circular queue overcomes this by allowing the rear and front pointers to wrap around. A priority queue inserts or deletes items based on priority. Deque allows insertions and deletions on both ends.
2. Assignment
Submitted by:
Toseef Hassan (BSF1600847)
Muhammad Ishfaq (BSF1604280)
Muhammad Shahzad (BSF1600951)
Muhammad Umar (BSF1600824)
Class: BS Information Technology (Eve)-A
Semester: IV Semester
Submitted to:
Mr. Khalid Mehmood
(Head Of Department)
Information Technology
Division of Science & Technology
5. What is a Queue?
A queue is a linear list which obeys FIFO (First In First
Out) principle to insert & delete values.
An ordered collection of homogeneous elements.
It’s a non-primitive linear data structure.
8. The Queue Operations:
Insertion or Enqueue:
Placing an item in a queue is called “insertion or
enqueue”, which is done at the end of the queue called
“rear” or “tail”.
Deletion or Dequeue:
Removing an item from a queue is called “deletion or
dequeue”, which is done at the other end of the queue
called “front” or “head”.
9. Insertion or Enqueue:
Algorithm:
If (rear = N-1 )
print (“*Queue is Full*”);
Else
rear = rear + 1; //rear=0
Q[rear] = item
The Queue Operations:
45Q
Rear
0 1 2 3
Where N is the length of array (4 in this
case)
Rear= -1 (First time only)
Front= -1
Item 45
15. Deletion or Dequeue:
Algorithm:
If (front =rear) // -1 = 3
print (“Queue is empty”);
Else
Front = front + 1;
item = Q [front];
Return item;
The Queue Operations:
45 32 23 07Q
Rear
0 1 2 3
N= 4
Front= -1
Front
16. Deletion or Dequeue:
Algorithm:
If (front =rear) // 0 = 3
print (“Queue is empty”);
Else
Front = front + 1;
item = Q [front];
Return item;
The Queue Operations:
32 23 07Q
Rear
0 1 2 3
N= 4
Front= 0
Front
17. Deletion or Dequeue:
Algorithm:
If (front =rear) // 1 = 3
print (“Queue is empty”);
Else
Front = front + 1;
item = Q [front];
Return item;
The Queue Operations:
23 07Q
Rear
0 1 2 3
N= 4
Front= 1
Front
18. Deletion or Dequeue:
Algorithm:
If (front =rear) // 2 = 3
print (“Queue is empty”);
Else
Front = front + 1;
item = Q [front];
Return item;
The Queue Operations:
07Q
Rear
0 1 2 3
N= 4
Front= 2
Front
19. Deletion or Dequeue:
Algorithm:
If (front =rear) // 3 = 3
print (“Queue is empty”);
Else
Front = front + 1;
item = Q [front];
Return item;
The Queue Operations:
Q
Rear
0 1 2 3
N= 4
Front= 3
Front
20. We have seen that all the values of queue are deleted
one by one, hence it can accommodate four more
values being empty.
But still, we can’t insert new values because;
Rear=3 and “Queue is full” condition invokes.
This is the major fault of simple queue that we can
only insert values once in a queue because there is no
mechanism to bring “rear” back to beginning (0).
Disadvantages of Simple Queue:
21. Circular Queue:
Circular queues were introduced to overcome the
limitations of simple queue.
As the name indicates a circular queue is not linear in
structure but instead it is circular.
Front and Rear variables display a circular movement
(clock wise) over the queue data structure.
As the Rear or Front variable reaches the end of the
queue, it is send back to beginning of the queue
using the mod function.
22. A B DCCirc_Q
Front Rear
(a) Initial circular queue
DCCirc_Q
Front Rear
(b) Circular queue after two
deletions
E DCCirc_Q
FrontRear
(c) Circ_Q after insertion of e
E F DCCirc_Q
FrontRear
(d) Circ_Q after insertion of
f
Workingofacircularqueue
23. Insertion:
Algorithm:
Insert (front, rear, n, item)
{ int p=rear;
rear= (rear+1)%n; // rear=1
If (front==rear)
{ Print “*Queue is full*”
rear=p;}
Q [rear] = item;
}
45Q
Rear
0 1 2 3
Where N is the length of array (4
in this case)
Rear= 0 (First time only)
Front= 0
Item 45
Front
24. Insertion:
Algorithm:
Insert (front, rear, n, item)
{ int p=rear;
rear= (rear+1)%n; // rear=2
If (front==rear)
{ Print “*Queue is full*”
rear=p;}
Q [rear] = item;
}
45 32Q
Rear
0 1 2 3
Item 32
Front
25. Insertion:
Algorithm:
Insert (front, rear, n, item)
{ int p=rear;
rear= (rear+1)%n; // rear=3
If (front==rear)
{ Print “*Queue is full*”
rear=p;}
Q [rear] = item;
}
45 32 23Q
Rear
0 1 2 3
Item 23
Front
26. Insertion:
Algorithm:
Insert (front, rear, n, item)
{ int p=rear; // p=3
rear= (rear+1)%n; // rear=0
If (front==rear)
{ Print “*Queue is full*”
rear=p;}
Q [rear] = item;
}
45 32 23Q
Rear
0 1 2 3
Item 23
Front
27. Deletion:
Algorithm:
Delete (front, rear, n, item)
{ If (front==rear)
Print “*Queue is empty*”;
front = (front+1) % n; // front=1
Item = Q [front];
}
45 32 23Q
Rear
0 1 2 3
Front
28. Deletion:
Algorithm:
Delete (front, rear, n, item)
{ If (front==rear)
Print “*Queue is empty*”;
front = (front+1) % n; // front=2
Item = Q [front];
}
32 23Q
Rear
0 1 2 3
Front
29. Deletion:
Algorithm:
Delete (front, rear, n, item)
{ If (front==rear)
Print “*Queue is empty*”;
front = (front+1) % n; // front=3
Item = Q [front];
}
23Q
Rear
0 1 2 3
Front
30. Deletion:
Algorithm:
Delete (front, rear, n, item)
{ If (front==rear)
Print “*Queue is empty*”;
front = (front+1) % n; // front=3
Item = Q [front];
}
Q
Rear
0 1 2 3
Front
31. Let’s try Insertion again
Insert (front, rear, n, item)
{ int p=rear;
rear= (rear+1)%n; // rear=0
If (front==rear)
{ Print “*Queue is full*”
rear=p;}
Q [rear] = item;
}
70Q
Rear Front
Item 70
0 1 2 3
Rear= 3
Front= 3
32. Let’s try Insertion again
Insert (front, rear, n, item)
{ int p=rear;
rear= (rear+1)%n; // rear=1
If (front==rear)
{ Print “*Queue is full*”
rear=p;}
Q [rear] = item;
}
70 89Q
Rear Front
Item 89
0 1 2 3
Rear= 0
Front= 3
33. Let’s try Insertion again
Insert (front, rear, n, item)
{ int p=rear;
rear= (rear+1)%n; // rear=2
If (front==rear)
{ Print “*Queue is full*”
rear=p;}
Q [rear] = item;
}
70 89 27Q
Rear Front
Item 27
0 1 2 3
Rear= 1
Front= 3
34. Let’s try Insertion again
Insert (front, rear, n, item)
{ int p=rear;
rear= (rear+1)%n; // rear=3
If (front==rear)
{ Print “*Queue is full*”
rear=p;}
Q [rear] = item;
}
70 89 27Q
Rear Front
0 1 2 3
Rear= 2
Front= 3
35. Priority Queue:
A priority queue is a queue in which insertion or deletion of
items from any position in the queue are done based on some
property (such as priority of task).
37. Deques:
A deque (double ended queue) is a linear list in which all insertions and
deletions are made at the end of the list. A dequeue is pronounced as
‘deck’ or ‘de queue.’
A deque is more general than a stack or queue and is a sort of FLIDLO
(First In Last In First Out Last Out).
A deque has two variants; input restricted queue and output restricted
queue.
An input restricted dequeue is one where insertions are allowed at one
end only while deletions are allowed at both ends.
An output restricted dequeue allows insertions at both ends of the queue
but permits deletions only at one end.