Nep Ds Notes On Queue
Nep Ds Notes On Queue
Nep Ds Notes On Queue
A queue is linear data structure and collection of elements. A queue is another special
kind of list, where items are inserted at one end called the rear and deleted at the other
end called the front. The principle of queue is a “FIFO” or “First-in-first-out”.
Operations on QUEUE:
Queue contains two pointer variable FRONT and REAR where front pointer to front
element in the queue to dequeue and rear pointer to rear element in the queue to
enqueue.
Enqueue or insertion: which inserts an element at the end of the queue.
Dequeue or deletion: which deletes an element at the start of the queue.
Queue operations work as follows:
Two pointers called FRONT and REAR are used to keep track of the first and
last elements in the queue.
When initializing the queue, we set the value of FRONT and REAR to 0.
On enqueing an element, we increase the value of REAR index and place the
new element in the position pointed to by REAR.
On dequeueing an element, we return the value pointed to by FRONT and
increase the FRONT index.
Before enqueing, we check if queue is already full.
Before dequeuing, we check if queue is already empty.
When enqueing the first element, we set the value of FRONT to 1.
When dequeing the last element, we reset the values of FRONT and REAR to 0.
This procedure deletes the front element of the linked queue and stores it
in
ITEM
1. [Linked queue empty?] if (FRONT = NULL) then Write:
UNDERFLOW and Exit
6. Exit.
APPLICATIONS OF QUEUES:
Queues are widely used as waiting lists for a single shared resource like printer, disk,
CPU.
Queues are used to transfer data asynchronously (data not necessarily received at
same rate as sent) between two processes (IO buffers), e.g., pipes, file IO, sockets.
Queues are used as buffers on MP3 players and portable CD players, iPod playlist.
Queues are used in Playlist for jukebox to add songs to the end, play from the front
of the list.
Queues are used in operating system for handling interrupts. When programming a
real-time system that can be interrupted, for example, by a mouse click, it is necessary
to process the interrupts immediately, before proceeding with the current job. If the
interrupts have to be handled in the order of arrival, then a FIFO queue is the appropriate
data structure.
TYPES OF QUEUES
A queue data structure can be classified into the following types:
1. Circular Queue 2. Deque 3. Priority Queue 4. Multiple Queue
Circular Queue:
In linear queues, insertions can be done only at one end called the REAR and
deletions are always done from the other end called the FRONT.
If Queue is completely full then it will not be possible to Insert an Item to the Queue.
If two successive deletions are made, even though there is space available, the
overflow condition still exists Because Insertion is made through Rear End of the
Queue.
So, it is a major drawback of a linear queue.
To resolve this problem, two solutions are made.
First, shift the elements to the left so that the vacant space can be occupied and
utilized efficiently. But this can be very time-consuming, especially when the queue
is quite large.
The second option is to use a circular queue. In the circular queue, the first index
comes right after the last index as shown in figure below.
Example:
In this example, the front and rear values are '0' and '5' respectively.
On adding an element to the queue each time, the rear value increments by 1
until it reaches the upper limit of the queue (which is 7 in this case), and after
this the process restarts again.
In a similar fashion on deleting a queue element, the value of the front is
incremented by 1 till the value becomes equal to the upper limit of the queue,
after this the queue starts over again from 0.
When it reaches a point when the queue becomes full, the first element present
in the queue will become the rear most element, this happens only when the front
of the queue moves forward else it results in a queue overflow.
Algorithm to Insert an Item to the Circular queue:
The following algorithm is used to insert a new value in the circular queue.
1. If rear points to the end of the queue, then go to step 2. else go to step 3.
2. Let Rear value = "0'.
3. Increase the rear value by l
4. If front' and the 'rear pointers point at the same place in the queue and that place has
a not NULL value, then "queue overflow", go to step 6, else go to step 5.
5. The new value is inserted in the position the rear pointer points to.
6. Exit
Algorithm to Delete an Item to the Circular queue:
The following algorithm is used to deletes an item in the circular queue
1. If front = NULL, then there are no elements in the queue. So, an “underflow”
condition will be reported.
2. If the queue is not empty and front = rear, then after deleting the element at the front
the queue becomes empty and so front and rear are set to NULL.
3. If the queue is not empty and front = N, then after deleting the element at the front,
front is set to 0.
Priority Queue:
A priority queue is a data structure in which each element is assigned a priority. The
priority of the element will be used to determine the order in which the elements will
be processed.
The general rules of processing the elements of a priority queue are
An element with higher priority is processed before an element with a lower priority.
Two elements with the same priority are processed on a first-come-first-served
(FCFS) basis.
A priority queue can be thought of as a modified queue in which when an element has
to be removed from the queue, the one with the highest-priority is retrieved first.
Stacks Queues
Stacks are based on the LIFO
principle, i.e., the element Queues are based on the FIFO principle, i.e., the
inserted at the last, is the first element inserted at the first, is the first element to
element to come out of the list. come out of the list.
In stacks it has only one pointer to In queues. It has two pointers to access the list.
access the list, called the top, The front pointer always points to the first element
which always points to the last inserted in the list and is still present, and the rear
element present in the list. pointer always points to the last inserted element.
*****************************************************************