You are on page 1of 15

Chapter Five

Queue

Compiled by: Nigusu Y.(nigusu02@gmail.com

1
Queues
o A queue is a data structure that stores a set of elements in particular order

and permits addition of elements from end and removal of elements from
front.

o Elements are removed in the same order in which they are stored (FIFO).

o Its a data structure that has access to its data at the front and rear(back).

o Its operated on FIFO (First In First Out) or LILO (Last In Last Out)

structure. ( FIRST-COME-FIRST-SERVE).

o Real-world example can be seen as queues at ticket windows & bus-stops.

2
Implementation of Queues
o Two common ways in which queues may be implemented are as follows:

 ARRAYS

 POINTERS (Linkedlist)

o Queue has the following restricted constraints.

 Elements are stored by the order of insertions from front to back.

 Items can only be added to the back of the queue

 Only the front element is accessed or removed from the queue.

o Errors will be created if the conditions are not satisfied:

 Overflow:- trying to add new item in full stack/queue

 Underflow:- trying to remove from empty stack/queue


3
Operations of queue
o Uses two pointers to keep tack of items of data in the front (start) and

rear(back)

o Queue has two basic operations:

 enqueue - inserting data at the rear/back

 dequeue - removing data from the front

 Peek - List (display) the elements of the queue.

o Suppose a queue Q has maximum size 4, say 4 elements pushed and 1

elements popped.
Rear
Front
24 12 34
4
Array implementations of Queue
o For example the following queue contains the integers 4 at the front and

8 and 6 at the rear.

0 1 2 3 4 5 6
4 8 6

o 4, 8, 6…array elements of integers to implement queues of integers and

we don’t care about the other parts of the array.

o The array implementations determines the queue size and the front and

rear elements stored in the array.

o Its keeps the items o the queue and index of the first and last elements.

5
Enqueue Operations
o As queue maintains two data pointers, front and rear, its operations

are comparatively more difficult to implement than Queue.

o Enqueue operations: Size increased and rear changed

o The following steps should be taken to enqueue (insert) data into a queue

Step 1 - Check if queue is full.


Step 2 - If queue is full, produce overflow error and exit.
Step 3 - If queue is not full, increment rear pointer to point next empty
space.
Step 4 - Add data element to the queue location, where rear is pointing.
Step 5 - return success.
6
o Enqueue operations: Size increased and rear changed

o Add integer value 7 at array index number a[4] in the array name a.

0 1 2 3 4 5 6
a
4 8 6 7
 Now the front is 4 stored on index number a[0] and the rear shifts from 6 to

7 which is from a[3] to a[4]

a 0 1 2 3 4 5 6
4 8 6 7 10

 Still the front is 4 stored on index number a[0] and the rear shifts from

element 7 to 10 which is from index number a[4] to a[5]

7
Dequeue Operation
o Accessing data from queue is a process of two tasks access the data
where front is pointing and remove the data after access.
o Dequeue operations: size decrement and front change.

o The following steps are taken to perform dequeue operation -


Step 1 - Check if queue is empty.
Step 2 - If queue is empty, produce underflow error and exit.
Step 3 - If queue is not empty, access data where front is pointing.
Step 3 - Increment front pointer to point next available data element.
Step 5 - return success

8
o In dequeue operation the first element is dequeued from the list if the list is

not empty.

o Example: The element 4 is removed and the front element is 8 and the rear

is 7.
0 1 2 3 4 5 6
8 6 7
 The position of array front shifts from [0] to [1] and the rare still located on

position [4] and data element 4 is removed.

0 1 2 3 4 5 6
6 7
 The position of array front shifts from [1] to [4] and the rare still located on

position [4] and data element 8 is removed.


9
Circular array implementations
o There is a special behaviour at the end of the array.

o Circular queue is used to reuse free spaces to store data elements if the

size of queue spot is full in the queuing process

0 1 2 3 4 5 6
7 10 2 9
o If we insert new elements ,the new data goes to the front of array to use

vacant space. For example if want to add element 12 to the queue above,
we can use the free space..

o Here the front is 7 and the rear is 12.


0 1 2 3 4 5 6
10 12 7 10 2 9
Priority Queues

o Priority queue is a queue where each data has an association key

that is provided at the time of insertion.

o Elements are dequeue according to their priority (associated key)

and current position.

o Insertion (enqueue)

 The insertion in priority queues is the same as in non-priority

queues.
11

Priority Queues…
 Deletion (dequeue)

 Deletion requires a search for the element of highest priority and deletes

the element with highest priority.

 The following methods can be used for deletion/removal from a given

Priority Queue:

o An empty indicator replaces deleted elements.

o After each deletion elements can be moved up in the array

decrementing the rear.

o The array in the queue can be maintained as an ordered circular array

12
o Example: consider the following queue a persons of where females
have priority than males (gender is the key too give priority)
Kebede Abel Melat Eyasu Helen Amare
M M F M F M

o Dequeue (): Deletes Melat

Kebede Abel Eyasu Helen Amare


M M M F M

o Dequeue (): Deletes Helen

Kebede Abel Eyasu Amare


M M M M
13
Application of queues
o Print server- maintains a queue of print jobs
o Disk Driver- maintains a queue of disk input/output requests
o Simulation of waiting line- maintains a queue of persons
o Telephone calls in a busy environment –maintains a queue of
telephone calls
o Task scheduler in multiprocessing system- maintains priority queues
of processes
o Time sharing computer system- where many users share the system
simultaneously.
o Railway reservation counter- is also an example of queue where the
people collect their tickets on FIFO or FCFS based.
14
Chapter End!!!

15

You might also like