You are on page 1of 37

Queues

Queue
 Stores a set of elements in a particular order
 Queue principle: FIRST IN FIRST OUT
 = FIFO
 It means: the first element inserted is the
first one to be removed
 Example

 The first one in line is the first one to be


served
What is a queue?
 Queues are linear data structures in which we add elements to one
end and remove them from the other end.

 The first item to be en-queued is the first to be de-queued. Queue


is therefore called a First In First Out (FIFO) structure.
 Queue operations:
 Enqueue /Insert

 Dequeue / Delete

 Empty
First In First Out

D rear
C rear C D rear
B rear B B C
rear front A B front
A A front A front
front
Queue Implementation (cont.)
 Implementation of Queue ADT
 use an one-dim array
 two variables
 front : one less than the position of the first
element
 rear : the position of the last element
 data member declaration in class
const int SIZE = 5;
class Queue
{
private:
int arr[SIZE];
int rear, front;
Implementation of Queue
 constructor definition
Queue(): rear(-1), front(0) {}

 member function IsFull()


bool IsFull()
{
if(rear >= SIZE-1)
return true;
else
return false;
}
Implementation of Queue
(cont.)
 member function IsEmpty()
bool IsEmpty()
{
if(front > rear)
return true;
else
return false;
}
 member function Add()
void insert(int val)
{
if(rear >= SIZE-1)
{
cout<<"Queue Full"<<endl;
exit(1);
}
arr[++rear] = val;
}
Implementation of Queue
(cont.)
 member function Delete()
int remove()
{
if(IsEmpty())
{
cout<<"Queue Empty"<<endl;
exit(1);
}
return arr[front++];
}
Queue Manipulation Issue
 It’s intuitive to use array for
implementing a queue. However, queue
manipulations (add and/or delete) will
require elements in the array to move.
In the worse case, the complexity is of
O(MaxSize).
Shifting Elements in Queue

front rear

front rear

front rear
Solution 1
When ever remove is called move all the elements in the start of
queue.

x = arr[0]
for (int i=0; i <rear; i++)
arr[i] = arr[i+1];
rear--;
Solution 2
These problems can be avoided by using
a circular array – an array in which the
location of the front and back index are
allowed to change and the contents to
wrap around when the last position in the
buffer is filled.
Circular Queue
Imagine that the two ends are joined together to form a circle
Queue implemented by a circular array
The Queue maintains two indices, front and rear, that initially are both SIZE
-1
1 2

0
3
rear
front 9

4
8

7 5
6
Insertion in queue

2 3

1
rear 4
back
0
x

5
front 9

8 6
7
Queue implemented using circular array

Assume that X denotes the indices in the buffer that hold an


object.
2 3

x x
1
x 4
back
x
rear
0
x
front
front
5
9

8 6
7
buffer
Deletion of element from queue

Assume that X denotes the indices in the buffer that hold an


object.
2 3

x x
1
front x 4
back
x rear

0
front

5
9

8 6
7
buffer
Implementation of Circular
Queue
 constructor definition
CirculeQueue(): rear(SIZE-1), front(SIZE-1) {}
Queue Operations
front 2 3

rear
X X
1
X 4
X
0 X
X
X 5
9
X X X
8 6
7
When array is full again front is equal to rear so we
can not differentiate whether array is full or empty
Queue Operations
front 2 3

X X
1
X 4
X
rear 0
X
X 5
9
X X X
8 6
7
So we sacrifice one element.
Implementation of Circular
Queue (cont.)
 member function Add()
Void Add(int x)
// add x to the circular queue
{
int k = (rear+1) % SIZE;
if (front = = k)
{
cout<<“Queue overflow”<<endl;
exit(1);
}
else
{
arr[k] = x;
rear = k;
}
}
Implementation of
Circular Queue (cont.)
void insert(int x)
{
if (rear == SIZE-1)
rear = 0;
else
rear++;
if (rear == front)
{
cout << “Queue Overflow”;
exit(1);
}
arr[rear] = x;
}
Implementation of Circular
Queue (cont.)
 member function Delete()
int Delete()
// remove front element from queue
{
if (front == rear)
{
cout<<“Queue overflow”<<endl;
System.exit(1);
}
front = (front+1)%SIZE;
int x = arr[front];
return x;
}
Implementation of
Circular Queue (cont.)
bool empty()
{
if (rear == q->front) return
true;
else
return false;
}//Calling is as empty(&q)
int remove()
{
if (empty(q))
{
cout <<“Queue overflow”;
exit(1) ;
}
if (front == SIZE-1)
front = 0;
else
front++;
return (arr[front]);
}
Priority Queues

Priority queue is a data structure in which


intrinsic ordering of elements determines the
result of basic operations.
Two kinds of priority queues:
• Ascending priority queue/Min priority Queue

• Descending priority queue/MAX priority

Queue
Min Priority Queue
• Collection of elements into which elements
are inserted arbitrarily and smallest
element is removed.
• Each element has a priority or key.
• Supports following operations:
 isEmpty
 insert an element into the priority queue
 get element with min priority
 remove element with min priority
Max Priority Queue
• Collection of elements into which elements
are inserted arbitrarily and largest element
is removed.
• Each element has a priority or key.
• Supports following operations:
 isEmpty
 insert an element into the priority queue
 get element with max priority
 remove element with max priority
Applications
Sorting
• use element key as priority

• put elements to be sorted into a priority

queue
• extract elements in priority order

 if a min priority queue is used, elements are


extracted in ascending order of priority
 if a max priority queue is used, elements are
extracted in descending order of priority (or
key)
Sorting Example
Sort five elements whose keys are 6, 8, 2, 4,
1 using a max priority queue.
 Put the five elements into a max priority
queue.
 Do five removemax() operations placing
removed elements into the sorted array from
right to left.
After Putting Into Max Priority Queue

8 4 6
1
Max
2 Priority
Queue

Sorted Array
After First Remove Max Operation

4 6
1
Max
2 Priority
Queue

Sorted Array
After Second Remove Max Operation

4
1
Max
2 Priority
Queue

6 8

Sorted Array
After Third Remove Max Operation

1
Max
2 Priority
Queue

4 6 8

Sorted Array
After Fourth Remove Max Operation

1
Max
Priority
Queue

2 4 6 8

Sorted Array
After Fifth Remove Max Operation

Max
Priority
Queue

1 2 4 6 8

Sorted Array
Queue Operations
Suppose n elements of a priority queue are maintained in positions 0 to
n-1 of an array of size maxpq then pqinsert(pq,x) is strightforward.

template <class KeyType>


void insert(KeyType x)
{
if (rear >= MaxSize - 1)
{
cout << “Queue Overflow”;
exit(1);
}
queue[rear] = x;
rear++;
} // elements are nor ordered.
For deletion there are two issues.
1. To locate the smallest element.
2. How to delete a middle element of the array.
Deletion Operation
Solutions
1. A special empty() indicator can be placed into
a deleted position. Insertion is done as given
before.
2. Deletion operation labels a position as empty
but insertion is done at the first empty
position.
3. Each deletion compact the array by shifting all
elements past the deleted element by one
position and decrementing pq.rear by 1.
4. Keep the array sorted.
DEQUEUE
A dequeue is an ordered set of elements from
which elements may be deleted at either end
and into which items may be inserted at either
end.
Basic operations:
• Remvleft

• Remvroght

• Insrtleft

• Insrtright

You might also like