Nep Ds Notes On Queue

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Unit 3: 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.

Representation of Queue (or) Implementation of Queue:


The queue can be represented in two ways:
1. Queue using Array
2. Queue using Linked List

1.Queue using Array:


 Queue will be maintained by a linear array queue and two pointer variables.
 FRONT containing the location of the front element of the queue, REAR containing
the location of the rear element of the queue.
 The condition FRON=NULL will indicate that the queue is empty.
 Whenever an element is deleted from queue the value of front is increased by 1.
FRONT:= FRONT + 1;
 Similarly, whenever an element is added to queue, the value of REAR is increased
by 1; REAR:=REAR + 1;
 This means that after N insertions the rear element of the queue will occupy
QUEUE[N].
 Suppose want to insert an element ITEM in the queue it occupies the last part of
array.
That is REAR:=N. One way to do this is to simply move the entire queue to the
beginning of the array, changing FRONT and REAR accordingly.
 If FRONT=N and an element of QUEUE is deleted, then reset FRONT=1 instead of
increasing to N+1.
 Queue contains only one element i,e FRONT=REAR!=NULL. Suppose that the
element is deleted Front=NULL and REAR=NULL to indicate that queue is empty.

Algorithm for insertion of an item to the Queue:


QINSERT (QUEUE, N, FRONT, REAR, ITEM)
This procedure inserts an element ITEM into a queue.
1. [Queue already filled?]
If FRONT = 1 and REAR = N, or if FRONT = REAR + 1, then:
Write: OVERFLOW, and Return.
2. [Find new value of REAR.]
If FRONT := NULL, then: [Queue initially empty.]
Set FRONT= 1 and REAR := 1.
Else if REAR = N, then:
Set REAR = 1.
Else:
Set REAR = REAR + 1.
[End of If structure].
3. Set QUEUE[REAR]: = ITEM. [These inserts new element.]
4. Return.
Algorithm to delete an Item from the queue:
QDELETE (QUEUE, N, FRONT, REAR, ITEM)
This procedure deletes an element from a queue and assigns it to the
variable
ITEM.
1. [Queue already empty?] If FRONT: = NULL, then: Write:
UNDERFLOW, and Return.
2. Set ITEM = QUEUE[FRONT].
3. [Find new value of FRONT.]
If FRONT = REAR, then: [Queue has only one element to start.]
Set FRONT: = NULL and REAR: = NULL.
Else if FRONT = N, then:
Set FRONT: = 1.
Else:
Set FRONT= FRONT + 1.
(End of If structure.]
4. Return.
2. Queue using linked list:
 A linked queue is a queue implemented as a linked list with two pointers FRONT
and REAR pointing to nodes which is in FRONT and REAR of queue.
 The INFO fields of the list hold the elements of the queue and the LINK fields hold
pointers to the neighboring elements in the queue.
 In the case of insertion into linked queue, a node borrowed from the AVAIL list and
carrying the item to be inserted is added as the last node of the linked list
representing the queue.
 The REAR pointer is updated to point to the last node just added to the list.
 In case of deletion, the first node of the list is pointed to by FRONT is deleted and
the FRONT pointer is updated to point to the next node in the list.

Algorithm for Insertion to Queue represented as Linked list:


LINKQ_INSERT (INFO.LINK, FRONT, REAR, AVAILITEM)
This procedure inserts an ITEM into a linked queue
1. [Available space?] If AVAL = NULL, then Write OVERFLOW and
Exit
2. [Remove first node from AVAIL list]
Set NEW: = AVAIL and AVAIL: = LINK[AVAIL]
3. Set INFO|NEW]: = ITEM and LINK[NEW]=NULL [Copies ITEM
into new node
4. If (FRONT = NULL) then FRONT = REAR = NEW

[ If Q is empty then ITEM is the first element in the queue Q]


else set LINK[REAR]:= NEW and REAR = NEW [REAR points to the
new node appended to the end of the list]
5. Exit.

Algorithm for Deletion of Node in queue represented as Linked List:

LINKQ_DELETE (INFO. LINK, FRONT, REAR, AVAIL, ITEM)

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

2. Set TEMP = FRONT [If linked queue is nonempty, remember FRONT


in a temporary variable TEMP]

3. ITEM = INFO [TEMP]


4. FRONT = LINK [TEMP] [Reset FRONT to point to the next element
in the queue

5. LINK[TEMP] =AVAIL and AVAlL=TEMP [return the deleted node


TEMP to the AVAIL list]

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:

Schematic diagram of a circular queue with 6 elements.

 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.

Deque: [Double Ended Queue]:


 A deque is a list in which the elements can be inserted or deleted at either end. It is
also known as a head-tail linked list because elements can be added to or removed
from either the front (head) or the back (tail) end.
 However, no element can be added and deleted from the middle.
 In the computer’s memory, a deque is implemented using either a circular array or
a circular doubly linked list.
 In a deque, two pointers are maintained, LEFT and RIGHT, which point to either
end of the deque. The elements in a deque extend from the LEFT end to the RIGHT
end and since it is circular, Dequeue[N–1] is followed by Dequeue [0]. Consider the
deques shown in Figure.

There are two variants of a double-ended queue. They include


Input restricted deque: In this dequeue, insertions can be done only at one of the ends,
while deletions can be done from both ends.
Output restricted deque: In this dequeue, deletions can be done only at one of the
ends, while insertions can be done on both ends.

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.

Implementation of a Priority Queue:


There are two ways to implement a priority queue.

1. One-Way List Representation of a Priority Queue


One way to maintain a priority queue in memory is by means of a one-way list, as
follows:
 Each node in the list will contain three items of information: an information field
INFO, a priority number PRN and a link number LINK.
 A node X precedes a node Y in the list
a. when X has higher priority than Y or
b. when both have the same priority but X was added to the list before Y.
 This means that the order in the one-way list corresponds to the order of the priority
queue.

2. Array Representation of a Priority Queue


 Another way to maintain a priority queue in memory is to use a separate queue for
each level of priority (or for each priority number).
 Each such queue will appear in its own circular array and must have its own pair of
pointers, FRÒNT and REAR.
 if each queue is allocated the same amount of space. a two-dimensional array
QUEUE can be used instead of the linear arrays.
 Observe that FRONT[K] and REAR[K] contain, respectively, the front and rear
elements of row K of QUEUE, the row that maintains the queue of elements with
priority number K.
APPLICATIONS OF STACKS :
Reversing a list
Parentheses checker
Conversion of an infix expression into a postfix expression
Evaluation of a postfix expression
Conversion of an infix expression into a prefix expression
Evaluation of a prefix expression
Recursion
Tower of Hanoi

Differences between Stack and Queue

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.

Insertion and deletion in queues take place from


Insertion and deletion in stacks the opposite ends of the list. The insertion takes
take place only from one end of place at the rear of the list and the deletion takes
the list called the top. place from the front of the list.

Insert operation is called push


operation. Insert operation is called enqueue operation.

Delete operation is called pop


operation. Delete operation is called dequeue operation.
Stacks Queues

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.

Stack is used in solving problems Queue is used in solving problems having


works on recursion. sequential processing.

Can be considered as a vertical Can be considered as a horizontal collection


collection visual. visual.

*****************************************************************

You might also like