Professional Documents
Culture Documents
Queues C++
Queues C++
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
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) {}
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
x x
1
x 4
back
x
rear
0
x
front
front
5
9
8 6
7
buffer
Deletion of element from queue
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
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
queue
• extract elements in priority order
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.
• Remvroght
• Insrtleft
• Insrtright