Queues
Lecture 5
Queues
• Unlike a stack, a queue is a structure in which both ends are used: one
for adding new elements and one for removing them
• A queue is an ordered collection of items from which items may be
deleted at one end (called the front of the queue) and into which items
may be inserted at the other end (called the rear of the queue)
– I.e., it is simply a waiting line that grows by adding elements to its
end and shrinks by taking elements from its front
– Thus, the last element has to wait until all elements preceding it on
the queue are removed
– It is data structure which implements a First-In, First-Out (FIFO)
behaviour i.e., elements are removed in the same order as they
were entered
• Basically data enters the queue at one end
and exits at the other
Examples
• Ticketing counter
• Printer SPOOL
• CPU Scheduling (at times)
• …
First one in
line is the first
to serve
Queue Attributes
• Size: Represents the total number of elements in a queue
• Front: Points to the first element of queue
– Used for removal
• Rear: Points to the last element of queue
– Used for insertion
A B C
Rear Front Rear
Front
B C
Queue Operations
• Primarily two operations performed on both ends
– Enqueue: Add an item at the rear of queue
– Dequeue: Remove an item from the front of queue
Queue Operations
• Enqueue Concerns
– What if when the queue is full?
• Dequeue Concerns
– What if the queue is empty
Solution:
• Before any enqueue, check if the queue is already full or
not
• Before dequeue, check if queue is not already empty
Queue Operations
Enqueue(element) // Add an element at the end of the queue
Dequeue() // Remove an element from the front of the queue
isEmpty() // Check to see if queue is empty
isFull() // Check to see if queue is full
FirstElement() // Returns first element in queue without
removing it
Clear() // Clear the queue
Choice of implementation
• Any implementation of a queue requires:
– Storage for the data as well as markers (“pointers”) for the
front and for the back of the queue
Two possible choices:
• Array based queue: Maximum queue size is known
ahead of time
• Linked List based queue: Maximum queue size
unknown
Array based queue implementation
• An array-based implementation would need structures
like
– An array to store the elements of the queue, items
– An index to track the front queue element, Front
– An index to track the position following last queue element, Rear
• Additions to the queue would result in incrementing
Rear while deletions from the queue would result in
incrementing Front
Array based queue implementation
#define length 5
Struct q // queue
{
int items[length];
int front, rear;
}
insert(q, x) // enqueue
q.items[++q.rear]=x;
X=remove(q) // dequeue
x=q.items[++q.front];
Array based queue implementation
The queue is empty when q.rear < q.front
The number of elements in the queue at any time is equal to the value of
q.rear – q.front +1
Problem: Run out of space
Now there are 3 elements the queue but there is room for 5
To insert a new value F, q.rear must be increased by 1 such that
q.items[5] is set to the value F but q.items is an array of only 5
elements so this insertion cannot be made
One possible solution: Shift items
• Shifting the elements downward with each
deletion
– Check if array is not empty
– Simply dequeue from the first location of array, say
array[0] i.e., the zeroth index
– After dequeue shift all the elements of the array from
array[index] to array[index-1]
Dequeue operation
x = q.items[0];
for ( i = 0 ; i < q.rear; i++ )
q.items[i] = q.items[i+1]
q.rear--;
• Method is costly as we have to move all the
elements of the array
• Do we need Front Index in this case?
– No, because we are always dequeue(ing) from the first index
Static Implementation of Queue
16
Static Implementation of Queue
17
18
Lab Work
• Write a program to implement Dequeue
operation of a Queue
19
Lab Work
Take a single string as input. Using this input string, you have
to create multiple queues in which each queue will comprise
of a separate word that appears in input string. At the end,
you will again concatenate all queues to a single queue.
• Example: String = “Data Structure and Algo”
Q1 = D → a → t → a
Q2 = S → t → r → u → c → t → u → r → e
Q3 = a → n → d
Q4 = A → l → g → o
• At the end concatenate all queues and display them
• Q1 → Q2 → Q3 → Q4
20
Lab Work
Write a program to demonstrate a printer
queue in which jobs for printing are added on
the basis of priority. Each printing job should
have name (e.g. A, B, C, D …..) and priority value
(e.g. 1=High, 2=Medium, 3=Low). If two jobs
have same priority then FIFO rule should be
applied.
21