Implementation of QUEUE For more notes and topics visit: eITnotes.com
A Queue is one of the simplest Data Structures and can be implemented with different methods on a computer. Queue of tasks to be executed in a computer is analogous to the queue that we see in our daily life at various places. Example in daily life: Queue of peoples in a bank, students queue in school, a traveler's queue for tickets at railway station are few examples of QUEUE. It is necessary to wait in a queue for obtaining the service. In the same fashion, in a computer there may be a queue of tasks waiting for execution, few others for printing, and others for inputting data and instructions through the Keyboard. eITnotes.com
A Queue is a Linear Data Structure. It is an ordered, homogenous collection of elements in which elements are appended at one end called REAR and elements are deleted at other end called FRONT end. The meaning of front is face side and rear means back side. The first entry in a queue to which the service is offered is to the elements that is on front. After servicing, it is removed from the queue. The information is manipulated in the same sequence as it was collected. Queue follows that rule First-In-First-Out. eITnotes.com
A BABA CBACBA DCBADCBA DCBDCB rear front rear front rear front rear front rear front First In First Out eITnotes.com
Array Implementation A queue can be implemented with an array, as shown here. For example, this queue contains the integers 4 (at the front), 8 and 6 (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... An array of integers to implement a queue of integers 48 6 eITnotes.com
Array Implementation The easiest implementation also keeps track of the number of items in the queue and the index of the first element (at the front of the queue), the last element (at the rear). [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 0 last 2 eITnotes.com
A Dequeue Operation When an element leaves the queue, size is decremented, and first changes, too. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 2 first 1 last 2 eITnotes.com
An Enqueue Operation When an element enters the queue, size is incremented, and last changes, too. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 1 last 3 eITnotes.com
At the End of the Array There is special behaviour at the end of the array. For example, suppose we want to add a new element to this queue, where the last index is [5]: [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 216 size 3 first 3 last 5 eITnotes.com
At the End of the Array The new element goes at the front of the array (if that spot isn’t already used): [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] 216 size 4 first 3 last 0 4 eITnotes.com
Array Implementation Easy to implement But it has a limited capacity with a fixed array Or you must use a dynamic array for an unbounded capacity Special behavior is needed when the rear reaches the end of the array. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ] size 3 first 0 last 2 eITnotes.com
Linked List Implementation A queue can also be implemented with a linked list with both a head and a tail pointer null 13 head_ptr tail_ptr eITnotes.com
Linked List Implementation Which end do you think is the front of the queue? Why? null 13 head_ptr tail_ptr eITnotes.com
Linked List Implementation The head_ptr points to the front of the list. Because it is harder to remove items from the tail of the list null head_ptr 13 tail_ptr Front Rear The solution is to keep a second pointer, tail, which points to the last element of the list. Of course, this time efficiency comes at the cost of the additional space used to store the tail pointer eITnotes.com
THANK YOU THANK YOU eITnotes.com