0% found this document useful (0 votes)
31 views

Queue - Note

Uploaded by

sayanpal854
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Queue - Note

Uploaded by

sayanpal854
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

2.What is a Queue Data Structure?

Queue is also an abstract data type or a linear data structure, just like stack
data structure, in which the first element is inserted from one end called
the REAR(also called tail), and the removal of existing element takes place
from the other end called as FRONT(also called head).
This makes queue as FIFO(First in First Out) data structure, which means
that element inserted first will be removed first.
Which is exactly how queue system works in real world. If you go to a ticket
counter to buy movie tickets, and are first in the queue, then you will be the
first one to get the tickets. Right? Same is the case with Queue data
structure. Data inserted first, will leave the queue first.
The process to add an element into queue is called Enqueue/insert and the
process of removal of an element from queue is called Dequeueue/delete.

2.1Basic features of Queue


1. Like stack, queue is also an ordered list of elements of similar data types.
2. Queue is a FIFO( First in First Out ) structure.
3. Once a new element is inserted into the Queue, all the elements inserted
before the new element in the queue must be removed, to remove the
new element.

2.2Applications of Queue
Queue, as the name suggests is used whenever we need to manage any
group of objects in an order in which the first one coming in, also gets out
first while the others wait for their turn, like in the following scenarios:

1. Serving requests on a single shared resource, like a printer, CPU task


scheduling etc.
2. In real life scenario, Call Center phone systems uses Queues to hold
people calling them in an order, until a service representative is free.
3. Handling of interrupts in real-time systems. The interrupts are handled in
the same order as they arrive i.e First come first served.
4. When a resource is shared among multiple consumers.
5. When data is transferred asynchronously (data not necessarily received at
same rate as sent) between two processes.
6. Load Balancing

Algorithm of enqueue:
Step 1: IF REAR = MAX-1 Write OVERFLOW
Goto step 4
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: EXIT
Algorithm of dequeue:
Step 1: IF FRONT = -1 OR FRONT > REAR
Write UNDERFLOW
ELSE
SET FRONT = FRONT + 1
[END OF IF]
Step 2: EXIT

Algorithm of Count:
Step 1: write rear-front+1
Step2: Exit

Algorithm of peek :
Step1 : write queue[front]
Step 2: Exit

Algorithm of isfull:
Step1: IF REAR = MAX-1 return True else return false
Step2: Exit

Algorithm of isempty:
Step 1: If FRONT = -1 OR FRONT > REAR return True else return false
Step2: Exit

Algorithm Display:
Step 1: IF FRONT = -1 OR FRONT > REAR
Write UNDERFLOW
Step 2: Set i=front
Step 3:while i<=rear
Step3.1: Write queue[i]
Step 3.2: set i=i+1

2.4Complexity Analysis of Queue Operations


Just like Stack, in case of a Queue too, we know exactly, on which position
new element will be added and from where an element will be removed,
hence both these operations requires a single step.

 Enqueue/insertion: O(1)
 Dequeueue/deletion: O(1)
 Display:O(n)
 Search:O(n)
 Size: O(1)

3.What is a Circular Queue?


Before we start to learn about Circular queue, we should first understand,
why we need a circular queue, when we already have linear queue data
structure.
In a Linear queue, once the queue is completely full, it's not possible to
insert more elements. Even if we dequeueue the queue to remove some of
the elements, until the queue is reset, no new elements can be inserted. You
must be wondering why?

When we dequeueue any element to remove it from the queue, we are


actually moving the front of the queue forward, thereby reducing the overall
size of the queue. And we cannot insert new elements, because
the rear pointer is still at the end of the queue.
The only way is to reset the linear queue, for a fresh start.

Circular Queue is also a linear data structure, which follows the principle
of FIFO(First In First Out), but instead of ending the queue at the last
position, it again starts from the first position after the last, hence making
the queue behave like a circular data structure.

3.2Basic features of Circular Queue

1. In case of a circular queue, head pointer will always point to the front of
the queue, and tailpointer will always point to the end of the queue.
2. Initially, the head and the tail pointers will be pointing to the same
location, this would mean that the queue is empty.

3. New data is always added to the location pointed by the tail pointer, and
once the data is added, tail pointer is incremented to point to the next
available location.
4. In a circular queue, data is not actually removed from the queue. Only
the head pointer is incremented by one position when dequeueue is
executed. As the queue data is only the data between head and tail,
hence the data left outside is not a part of the queue anymore, hence
removed.

5. The head and the tail pointer will get reinitialised to 0 every time they
reach the end of the queue.
6. Also, the head and the tail pointers can cross each other. In other
words, head pointer can be greater than the tail. Sounds odd? This will
happen when we dequeueue the queue a couple of times and
the tail pointer gets reinitialised upon reaching the end of the queue.

Overflow condition
If the MAX_SIZE is the size of the array used in the implementation of
circular queue. Then the overflow condition is
front=(rear+1)%MAX_SIZE
or
front=rear+1

3.3Application of Circular Queue


Below we have some common real-world examples where circular queues
are used:
1. Computer controlled Traffic Signal System uses circular queue.
2. CPU scheduling and Memory management.

3.4Complexity Analysis of circular Queue Operations


Just like Stack, in case of a circular Queue too, we know exactly, on which
position new element will be added and from where an element will be
removed, hence both these operations requires a single step.
• Enqueue/insertion: O(1)
• Dequeueue/deletion: O(1)
• Display:O(n)
• Search:O(n)
• Size: O(1)

Algorithm of Insertion :

Step 1: IF FRONT =(REAR+1)%max


Write OVERFLOW
IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT !=0
SET REAR =0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL

Step 4: EXIT

Algorithm to delete an element from the circular queue

Step 1: IF FRONT = -1 and rear=-1


Write " UNDERFLOW "
Goto Step 4
[END of IF]

Step 2: SET VAL = QUEUE[FRONT]

Step 3: IF FRONT = REAR


SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]

Step 4: EXIT

Display function of Circular Queue

void display() {

int i;

if (isEmpty())

printf(" \n Empty Queue\n");

else {

printf("\n Front %d ", front);

printf("\n Items ");

for (i = front; i != rear; i = (i + 1) % SIZE) {

printf("%d ", items[i]);

printf("%d ", items[i]);

printf("\n Rear %d \n", rear);

4.1Implement Queue using Stacks


A Queue is defined by its property of FIFO, which means First in First Out,
i.e the element which is added first is taken out first. This behaviour defines
a queue, whereas data is actually stored in an array or a list in the
background.
What we mean here is that no matter how and where the data is getting
stored, if the first element added is the first element being removed and we
have implementation of the functions enqueue()and dequeueue() to enable
this behavior, we can say that we have implemented a Queue data
structure.
In our previous tutorial, we used a simple array to store the data elements,
but in this tutorial we will be using Stack data structure for storing the
data.
While implementing a queue data structure using stacks, we will have to
consider the natural behavior of stack too, which is First in Last Out.
For performing enqueue we require only one stack as we can
directly push data onto the stack, but to perform dequeueue we will
require two Stacks, because we need to follow queue's FIFO property and if
we directly pop any data element out of Stack, it will follow LIFO
approach(Last in First Out).
Take two stacks S1 and S2
Algorithm of enqueue or insertion operation:
Enqueue(S1,x)
{
Push(S1,x);
}
Algorithm od dequeueue or deletion operation:
Dequeueue(S1,S2)
{
if( S2 is empty)
{
if( S1 is empty)
{
print(queue is empty)
exit
}
else
{
while(S1 is not empty )
{
x=pop(S1)
push(S2,x)
}
}
}
return pop(S2)
}

5.1 Implement Stack using Queue


Stack is LIFO (last in - first out) data structure, in which elements are
added and removed from the same end, called top. In general stack is
implemented using array or linked list, but in the current article we will
review a different approach for implementing stack using queues. In contrast
queue is FIFO (first in - first out) data structure, in which elements are
added only from the one side - rear and removed from the other - front. In
order to implement stack using queues, we need to maintain two
queues q1 and q2. Also we will keep top stack element in a constant
memory.

Push algorithm Stack


push(q1,q2,x)
{ 5 6 7 8 2
if (q1 is empty) 0 1 2 3 4T 5 6
{
enqueue(q2,x) Queue 1
}
else 0 1 2 3 4 5 6
{
enqueue(q1,x) Queue 2
}
}
0 1 2 3 4 5 6
Algorithm of Pop operation.
pop(q1,q2)
{
if (q1 is empty)
{
if (q2 is empty)
{
print (stack is under flow)
exit
}
else
{
while(q2 is not contained one element)
{
x=dequeueue(q2)
enqueue(q1,x)
}
return dequeueue(q2)
}
else
{
while(q1 is not contained one element)
{
x=dequeueue(q1)
enqueue(q2,x)
}
return dequeueue(q1)
}
}

6.1Dequeueues(Double ended Queue)


A dequeueue 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 dequeue is implemented using either a circular array
or a circular doubly linked list. In a dequeue, two pointers are maintained,
LEFT and RIGHT, which point to either end of the dequeue. The elements in
a dequeue extend from the LEFT end to the RIGHT end and since it is
circular, Dequeueue[N–1] is followed by Dequeueue[0].
There are two variants of a double-ended queue. They include
 Input restricted dequeue In this dequeueue,insertions can be done
only at one of the ends,while deletions can be done from both ends.
 Output restricted dequeue In this dequeueue, deletions can be done
only at one of the ends,while insertions can be done on both ends.

The applications of dequeue


a) A-Steal job scheduling algorithm
b) Can be used as both stack and queue
c) To find the maximum of all sub arrays of size k

Input restricted dequeue:


Algorithm of insert_right():

Step 1: IF FRONT =(REAR+1)%max


Write OVERFLOW
IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT !=0
SET REAR =0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: EXIT
Algorithm delfromleft()

Step 1: IF FRONT = -1 and rear=-1


Write " UNDERFLOW "
Goto Step 4
[END of IF]

Step 2: SET VAL = QUEUE[FRONT]

Step 3: IF FRONT = REAR


SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]

Step 4: EXIT

Algorithm del from right

Step 1: IF FRONT = -1 and rear=-1


Write " UNDERFLOW "
Goto Step 4
[END of IF]

Step 2: SET VAL = QUEUE[FRONT]

Step 3: IF FRONT = REAR


SET FRONT = REAR = -1
ELSE
IF REAR = 0
SET REAR = Max-1
ELSE
SET REAR = REAR - 1
[END of IF]
[END OF IF]

Step 4: EXIT

Output restricted dequeue:


Algorithm of insert_right():

Step 1: IF FRONT =(REAR+1)%max


Write OVERFLOW
IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT !=0
SET REAR =0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: EXIT
Algorithm of insert_left():

Step 1: IF FRONT =(REAR+1)%max


Write OVERFLOW
IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR != MAX - 1 and FRONT =0
SET FRONT =max-1
ELSE
SET FRONT = FRONT- 1
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: EXIT

Algorithm delfromleft()

Step 1: IF FRONT = -1 and rear=-1


Write " UNDERFLOW "
Goto Step 4
[END of IF]

Step 2: SET VAL = QUEUE[FRONT]

Step 3: IF FRONT = REAR


SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]

6.2Priority Queues
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. The priority of the element can be set based on
various factors. Priority queues are widely used in operating systems to
execute the highest priority process first.
Array Representation of a Priority Queue
When arrays are used to implement a priority queue, then a separate queue
for each priority number is maintained. Each of these queues will be
implemented using circular arrays or circular queues. Every individual queue
will have its own FRONT and REAR pointers. We use a two-dimensional array
for this purpose where each queue will be allocated the same amount of
space. Look at the two-dimensional representation of a priority queue given
below.
Given the front and rear values of each queue, the two-dimensional matrix
can be formed as shown in Figure .

FRONT[K] and REAR[K] contain the front and rear values of row K, where K
is the priority number. Note that here we are assuming that the row and
column indices start from 1, not 0. Obviously, while programming, we will
not take such assumptions.
Insertion To insert a new element with priority K in the priority queue, add
the element at the rear end of row K, where K is the row number as well as
the priority number of that element. For example, if we have to insert an
element R with priority number 3, then the priority queue will be given as
shown in Figure

Deletion To delete an element, we find the first nonempty queue and then
process the front element of the first non-empty queue. In our priority
queue, the first non-empty queue is the one with priority number 1 and the
front element is A, so A will be deleted and processed first. In technical
terms, find the element with the smallest K, such that FRONT[K] != NULL.

0 1 0 1 2 3 4

0 0
1 1
2 2
3 3
4 4
5 5
6 6

You might also like