You are on page 1of 14

IT T33- Data Structures

UNIT II
Queues: Queue abstract data type - Array implementation – circular queue - linked list
implementation of queues – priority queues – double ended queues – multiple stacks and queues -
application

Objective:
 To introduce and understand the concept of queue and its operations
 To understand the application of queues
Outcome:
 Able to apply the queue data structures in programming

2.1 Queue- Abstract Data type


A Queue is an ordered collection of items from which items may be deleted at one end
called the front of the queue and into which items may be inserted at the other end called rear
of the queue. Queue is called as First –in-First-Out (FIFO).The primary operation that can be
done on a queue are:

 Enqueue (Insert)- adds a new element in the queue, this process is carried out by
incrementing the rear end and adding the new element in the rear position.
 Dequeue(Delete)- delete an element from the queue, this process is carried out by
incrementing the front end and deleting the element a front end position.

Types of queue
1. Linear Queue
2. Circular Queue
3. Double ended queue
4. Priority Queue

2.1.1 Linear queue


Linear queue is an ordered collection of items from which items may be deleted at one
end called the front of the queue and into which items may be inserted at the other end called
rear of the queue. Queue is called as First –in-First-Out (FIFO).

2.1.2 Circular Queue


Circular Queue is the another form of linear queue in which last position is connected to the
first position of the queue. Circular queue also has two ends:-
 Front
 Rear
The front end is where we delete element and the rear end is where we insert element. The
next position of the rear is front, then the queue is said to be fully occupied.

SMVEC- Department of Information Technology 1


IT T33- Data Structures
2.1.3 Double Ended Queue
In double ended queue, insertion and deletion are made at both rear and front end of the queue.
They are two variations of deque.
 Input restricted Dequeue -Insertion can be done only at one of the dequeue,while deletion
can be done on both ends
 Output restricted dequeue - deletion can be done only at one of the dequeue,while
insertion can be done on both ends

Types of deque
1. Linear deque
2. Circular deque

Linear Deque
It is similar to the linear queue except the following conditions:
 If the front pointer is in the first position (zeroth position), we can’t insert data at front end.
 If the rear is in the last position (queuesize-1) we can’t insert data at rear end.

Circular Deque
Circular deque is similar to circular queue except the following conditions:
 Insertion and deletion are made at front and rear end of the queue.Irrespective of the
position of the front and rear we can insert and delete

2.2 Static implementation (Representation) of linear queue using Array

Representation of linear queue


1. Array representation of queue [Static implementation of queue]
2. Linked list representation of queue [Dynamic implementation of queue]

Array representation of queue


One of the simplest ways of representing a queue is by means of single dimensional array.

Basic operations
1. Enqueue
2. Dequeue
3. Display

 Initially the front end and the rear end are at the same position (-1).
 The rear pointer is in the last position (size-1) then queue is said to be full, if front pointer is
(-1) then the queue is empty.
 When you insert elements, rear pointer moves one by one until the last position is
reached.
 When we delete elements, the front pointer moves one by one until the rear pointer is
reached.

SMVEC- Department of Information Technology 2


IT T33- Data Structures

2.2.1 Enqueue
Algorithm Enqueue(queue,rear,front,queuesize)
{
if rear=queuesize-1
{ write "queue is in overflow condition. Thus enqueue operation is not possible.";
return;
}
if rear=-1 and front=-1
{ rear=front=0;
}
else
{ rear=rear+1;
}
read rollno;
queue[rear]=rollno;
}

2.2.3 Dequeue
Algorithm Dequeue(queue,rear,front,queuesize)
{
if rear=queuesize-1
{
write "queue is in overflow condition. Thus enqueue opeation is not possble.";
return;
}
if rear=front
{ rear=front=-1;
}
else
{ front=front+1;
}
}

2.2.4 Display
Algorithm display(queue,front,rear)
{
if front=-1&rear=-1&rear
{ write "Queue is in underflow condition. Display operation is not possible.";
return;
}
for i=front to rear
{ write queue[i];
}
return;
}

SMVEC- Department of Information Technology 3


IT T33- Data Structures
2.3 Dynamic implementation(Representation) of Linear queue using Linked List
Each element is represented as node. The node is represented in two fields.
 Data
 Link Field
Since, the node contains collection of two different data, it is defined as structure.
struct node
{
int data;
struct node link;
}front=NULL; rear=NULL;
2.3.1 Enqueue
Algorithm enqueue(struct node *front, struct node *rear)
{
struct node *newnode;
newnode=new node;
read rollno;
newnode->data=rollno;
newnode->link=NULL;
if front=NULL and rear=NULL
{
front=newnode;
rear=newnode;
}
else
{
rear->link=newnode;
rear=newnode;
}
return;
}

2.3.2 Dequeue
Algorithm dequeue(struct node *front, struct node *rear)
{
struct node *temp;
if front=NULL and rear=NULL
{
write "Queue is in underflow condition. Dequeue operaation is not possible.";
return;
}
if front=rear
{
front=rear=NULL;
}
else
{
front=front->link;
}
delete temp;
return;
}

SMVEC- Department of Information Technology 4


IT T33- Data Structures

2.3.3 Display
Algorithm display(struct node *front, struct node *rear)
{
struct node *temp;
if front=NULL and rear=NULL
{
write "Queue is in underflow condition. Dequeue operaation is not possible.";
return;
}
temp=front;
while(temp!=front)
{
write temp->data;
temp=temp->link;
}
return;
}

2.4 Circular Queue


Circular queue also has two ends:-
 Front
 Rear

Circular Queue
 When a new item is inserted at the rear, the pointer to rear moves upwards.
 When an item is deleted from the queue the front arrow moves downwards.
 After a few insert and delete operations the rear might reach the end of the queue and no
 More items can be inserted although the items from the front of the queue have been
deleted and there is space in the queue.

Concept of Circular Queue:


There is a formula which has to be applied for setting the front and rear pointers, for a circular
queue.

Circular Queue Empty: Front=Rear=0.


Basic operations
 Enqueue
 Dequeue
 Display

SMVEC- Department of Information Technology 5


IT T33- Data Structures
2.4.1 Enqueue
Algorithm enqueue(queue,front,rear,queuesize)
{
if (rear+1)%queuesize=front
{
write "Queue is in overflow condition. Enqueue operation is not possible.";
return;
}
read rollno;
if front=-1 and rear=-1
{
front=rear=0;
}
else
{
rear=(rear+1)%queuesize;
}
queue[rear]=rollno;
return;
}
2.4.2 Dequeue
Algorithm dequeue(queue, front, rear, queuesize)
{
if front=-1 and rear=-1
{
write "Queue is in underflow condition. Dequeue operation is not possible.";
return;
}
write "The element to be deleted",queue[front];
if front=rear
{ front=rear=-1;
}
else
{ front=(front+1)%queuesize;
}
return;
}
2.4.3 Display
Algorithm display(queue, front, rear, queuesize)
{
if front=-1 and rear=-1
{
write "Queue is in underflow condition. Dequeue operation is not possible."
return;
}
i=front;
while(i!=rear)
{
write queue[i];
i=(i+1)%queuesize;
}
write queue[rear];
return;
}

SMVEC- Department of Information Technology 6


IT T33- Data Structures
2.5 Priority Queue (or) Application of Queue
Application of queue
 Direct applications
o Access to shared resources (e.g., printer)
o Multiprogramming
 Indirect applications
o Priority queue
o Auxiliary data structure for algorithms
o Component of other data structures
Priority queue
Priority queue is a data structure having collection of elements which are associated with
specific order. They are of two types:
 Ascending Priority Queue- It is a collection of elements in which the insertion of
element can be any order, but only the smallest element can be removed.
 Descending Priority Queue- It is a collection of elements in which the insertion of
element can be any order, but only the largest element can be removed.

 The priority queue elements are arranged in any order and out of which only the smallest or
largest element allowed to be deleted each time.
 The implementation of priority can be done by using array and linked list. The data structure
heap is used to implement priority queue efficiently.

Basic Operations
 Enqueue
 Dequeue

2.5.1 Enqueue
Algorithm enqueue(queue,front,rear,queuesize)
{
if rear=queuesize-1;
{
write "Queue is in overflow condition. Enqueue is not possible.";
return;
}
read rollno;
if front=-1 and rear=-1
{
front=rear=0;
}
else
{
i=rear;
while(i>=0)and (rollno<queue[i])
{
queue[i+1]=queue[i];
i=i-1;
}
queue[i+1]=rollno;
rear=rear+1;
return;
}
}
SMVEC- Department of Information Technology 7
IT T33- Data Structures

2.5.2 Dequeue
Algorithm dequeue(queue,front,rear)
{
if front=-1 and rear=-1
{
write "Queue is in underflow condition. Dequeue operation is not possible.";
return;
}
write "Element to be deleted", queue[front];
if front=rear
{
front=rear=-1;
}
else
{
front=front+1;
}
return;
}

2.6 Double Ended Queue (DEQUE)


In double ended queue, insertion and deletion are made at both rear and front end of the
queue.

They are two variations of deque.


 Input restricted Dequeue -Insertion can be done only at one of the dequeue,while deletion
can be done on both ends
 Output restricted Dequeue - deletion can be done only at one of the dequeue,while
insertion can be done on both ends

Types of deque
 Linear dequeue
 Circular dequeue
Linear Deque
It is similar to the linear queue except the following conditions:
 If the front pointer is in the first position (zeroth position), we can’t insert data at front end.
 If the rear is in the last position (queuesize-1) we can’t insert data at rear end.

Circular Deque
Circular deque is similar to circular queue except the following conditions:
 Insertion and deletion are made at front and rear end of the queue.
 Irrespective of the position of the front and rear we can insert and delete he data.

SMVEC- Department of Information Technology 8


IT T33- Data Structures
2.6.1 Enqueue – Front
Algorithm enqueue (queue, front,rear,queuesize)
{
if front=0
{
write "Queue is in overflow condition. Enqueue is not possible.";
return;
}
if front=-1 and rear=-1
{
front=rear=queuesize-1;
}
else
{
front=front-1;
}
read rollno;
queue[front]=rollno;
return;
}
2.6.2 Enqueue – rear
Algorithm enqueue (queue, front,rear,queuesize)
{
if front=0
{ write "Queue is in overflow condition. Enqueue is not possible.";
return;
}
if front=-1 and rear=-1
{
front=rear=0;
}
else
{
rear=rear+1;
}
read rollno;
queue[rear]=rollno;
return;
}
2.6.3 Dequeue – front
Algorithm dequeue(queue,front,rear)
{
if rear=-1 and front=-1
{ write "Queue is in underflow condition. Dequeue is not possible.";
return;
}
write "Element to be deleted",queue[front];
if front=rear
{
front=rear=-1;
}
else
{ front=front+1;
}
return; }

SMVEC- Department of Information Technology 9


IT T33- Data Structures
2.6.4 Dequeue – rear
Algorithm dequeue(queue,front,rear)
{
if rear=-1 and front=-1
{
write "Queue is in underflow condition. Dequeue is not possible.";
return;
}
write "Element to be deleted",queue[front];
if front=rear
{
front=rear=-1;
}
else
{
rear=rear-1;
}
return;
}
2.7 Multiple Stacks and Queues in detail
2.7.1 Multiple Stacks:
When a stack is created using single array, we cannot able to store large amount of data,
thus this problem is rectified using more than one stack in the same array of sufficient array. This
technique is called as Multiple Stack.
When an array of STACK[n] is used to represent two stacks, say Stack A and Stack B.
Then the value of n is such that the combined size of both the Stack[A] and Stack[B] will never
exceed n. Stack[A] will grow from left to right, whereas Stack[B] will grow in opposite direction ie)
right to left.
 Here only one single one-dimensional array (Q) is used to store multiple stacks.
 B (i) denotes one position less than the position in Q for bottommost element of stacks i.
 T (i) denotes the top most element of stack i.
 m denotes the maximum size of the array being used.
 n denotes the number of stacks.
We also assume that equal segments of array will be used for each stack.
Initially let B (i) = T (i) = [m/n]*(i-1) where 1<= i <= n.
Again we can have push or pop operations, which can be performed on each of these stacks .

SMVEC- Department of Information Technology 10


IT T33- Data Structures
Algorithm for Push operation
Push (i, x)
{
If (((i<n) && (T(i)==B(i+1)) || ((i>=n) && (T(i) ==m)));
{
Then call STACK_FULL ;
}
Else
{
T(i) = T(i) + 1;
Q[T(i)] = x ;
}
}

Algorithm for Pop Operation


Pop (i,x)
{
if ( T(i) = = B(i) )
{

Then print that the stack is empty.


}
Else
{
x = Q[T(i)];

T[i] = T[i] – 1;
}
}

2.7.2 Multiple Queues:


 Problems with the queues represented with arrays are:
 We have to allocate large space to avoid the overflow.
 Large space will be wasted due to less size of queue.
 There exists a trade-off between the number of overflows and the space.
 One possibility to reduce this tradeoff is to represent more than one queue in the same
array of sufficient size

 Free elements could be anywhere in the Queue such as before the front, after the rear, and
between front and rear in the Queue
 Queue 1’s and Queue 2 ‘s size could be change if it is necessary. When the Queue 1 is full and
the Queue 2 has free space; the Queue 1 can increase the size to use that free space from the
Queue 2. Same way for the Queue 2

SMVEC- Department of Information Technology 11


IT T33- Data Structures

Insertion and deletion of Multiple Queue

 Inserts data to the Queue 1,


Q1Rear = (Q1Front + Q1count) % Q1Size

 Inserts data to the Queue 2,


Q2Rear = (Q2Front + Q2count) % Q2Size + Q1Size

 Deletes data from the Queue 1,


Q1Front = (Q1Front + 1) % Q1Size

 Deletes data from the Queue 2,


Q2Front = (Q2Front + 1) % Q2Size + Q1Size

SMVEC- Department of Information Technology 12


IT T33- Data Structures
Two Marks
1. Define a queue
Queue is an ordered collection of elements in which insertions are restricted to one end
called the rear end and deletions are restricted to other end called the front end. Queues are also
referred as First-In-First-Out (FIFO) Lists.

2. Define a priority queue (April 2015)(November 2015)(November 2013)


Priority queue is a collection of elements, each containing a key referred as the priority for
that element. Elements can be inserted in any order (i.e., of alternating priority), but are arranged
in order of their priority value in the queue. The elements are deleted from the queue in the order
of their priority (i.e., the elements with the highest priority is deleted first). The elements with the
same priority are given equal importance and processed accordingly.

3. State the difference between queues and linked lists


The difference between queues and linked lists is that insertions and deletions may occur
anywhere in the linked list, but in queues insertions can be made only in the rear end and deletions
can be made only in the front end.

4. Define a Deque or Double ended queue (April 2015)(May 2014)(November 2013)


Deque (Double-Ended Queue) is another form of a queue in which insertions and deletions
are made at both the front and rear ends of the queue.
There are two variations of a deque, namely, input restricted deque and output restricted
deque. The input restricted deque allows insertion at one end (it can be either front or rear) only.
The output restricted deque allows deletion at one end (it can be either front or rear) only.

5. What are the types of queues?


• Linear Queues
• Circular Queues
• Double-Ended-Queue

6. What is linear queue?


The queue has two ends, the front end and the rear end. The rear end is where we insert
elements and front end is where we delete elements. We can traverse in a linear queue in
only one direction ie) from front to rear.

7. What is Circular queue?(November 2015)


Another form of linear queue in which the last position is connected to the first position of
the list. The circular queue is similar to linear queue has two ends, the front end and the rear end.
The rear end is where we insert elements and front end is where we delete elements. We can
traverse in a circular queue in only one direction ie) from front to rear.

9. List the applications of queues

 Serving requests of a single shared resource like (printer, disk, CPU), between two
processes
 Job Scheduling
 Used in multitasking operating System
 Categorizing of data
 Simulation and Modeling

SMVEC- Department of Information Technology 13


IT T33- Data Structures
10. What are the applications of priority queues?

• The selection problem


• Event simulation

11. List out the difference between Linked stack and Linked Queue.
Stack Queue

Stack is a collection of items Queue is an ordered collection of items


Items are inserted & deleted at one end called Items are deleted at one end called ‘front’ end
‘Top of the stack’. of the queue.
Items are inserted at other end called ‘rear’ of
the queue.
It allows access to only one data item: the last The first item inserted is the first to be
item inserted. removed (FIFO).
This structure of accessing is known as Last in This structure of accessing is known as First
First out structure (LIFO) in First out structure (FIFO)

Assignment questions
1. Efficiently implement a queue class using a circular array. You may use a vector (rather
than a primitive array) as the underlying array structure.
2. Design a data representation sequentially mapping n queues into an array M
[m]. Represent each queue as a circular queue within M. Write functions Add, Delete, and
QueueFull for this representation.
3. Write routines to implement two stacks using only one array. Your stack routines should not
declare an overflow unless every slot in the array is used.
4. Obtain a data representation mapping a stack s and a queue q into a single array M [n].
Write algorithms to add and delete elements from these two data objects. What can you
say about the suitability of your data representation?
5. Write a routine for implementing the priority queue?

SMVEC- Department of Information Technology 14

You might also like