Fundamental Algorithm ch5
Fundamental Algorithm ch5
Fundamental Algorithm ch5
Queue
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).
2
Implementation of Queues
o Two common ways in which queues may be implemented are as follows:
ARRAYS
POINTERS (Linkedlist)
rear(back)
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
0 1 2 3 4 5 6
4 8 6
o The array implementations determines the queue size and the front and
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
o The following steps should be taken to enqueue (insert) data into a queue
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
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
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.
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
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
o Circular queue is used to reuse free spaces to store data elements if the
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 Insertion (enqueue)
queues.
11
Priority Queues…
Deletion (dequeue)
Deletion requires a search for the element of highest priority and deletes
Priority Queue:
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
15