Professional Documents
Culture Documents
Week#09
Queue and Its Implementation
Contact Info:
Room No: 112
Email: umme.laila@iobm.edu.pk
1
Introduction to Queue
• Queues are linear data structures in which we add elements
to one end (REAR) and remove them from the other end
(FRONT).
• Queue Operations:
• Enqueue: Insert an element at the REAR of the queue.
• Dequeue: Delete an element at the FRONT of the queue.
2
Real-life Application of Queue
3
Application of Queue in computer Science
•Queue is used in
Waiting lists for a single shared resource like CPU,
Disk, and Printer.
4
Example
REAR
FRONT
5
Queue Representation
• Queues may be represented as one-way list or linear arrays
QUEUE.
• Two pointer variables are used
• FRONT – containing the location of front element of the queue.
• REAR – containing the location of the rear element of the queue
•After N insertions, the rear element of the queue will occupy QUEUE [N]
6
Array Representation of Queue
7
Array Representation of Queue
•Suppose the queue is at the end and we need to insert an ITEM at N then
we consider that the QUEUE is
QUEUE IS CIRCULAR, that is, that QUEUE [1] comes after QUEUE
[N] in the array.
•We insert ITEM into the QUEUE by assigning ITEM to QUEUE [1] in the
array. Instead of increasing REAR to N+1 we reset REAR=1 and then
assign
9
Example 6.11
10
Example 6.11 (Contd.)
11
Algorithms for Queue Operations
12
Algorithms for Queue Operations
13
DEQUE
14
DEQUE
• There are two variations of a deque
15
DEQUE
Various operations of Dequeu are:-
16
DEQUE
Algorithm to Insert / enqueue from rear
19
DEQUE
Algorithm to Delete / dequeue from rear
20
DEQUE
21
APPLICATION OF DEQUEUE
22
Priority Queues
• A priority queue is a collection of elements where the elements
are stored according to their priority levels.
23
Applications of Priority Queues
• Priority queues are used for implementing job scheduling by the
operating system, where jobs with higher priorities are to be
processed first.
24