Queues
Queue Overview
Queue ADT
Basic operations of queue
Implementations of queues using
insert, delete etc.
array
linked list
Circular queue
Priority queue
Queue
Stores a set of elements in a particular
order
Stack 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
rear B
front A
C
rear B
fron A
t
D
rear C
B
front A
rear
D
C
front B
rear
front
Queue ADT
abstract typedef<<eltyper> QUEUE (eltype);
abstract empty(q)
QUEUE(eltype) q;
postcondition empty== len(q)==0;
abstract eltype remove(q)
QUEUE (eltype) q;
precondition: empty(q) == FALSE
postcondition remove == first(q`);
q== sub(q`,1, len(q`) -1;
Queue ADT
Abstract insert(q, elt);
QUEUE (eltype) q;
eltype elt;
postcondition q == q + <elt> ;
Implementation of Queues
Any list implementation could be used to
implement a queues
Arrays and Circular Arrays(static: the size of
queue is given initially)
Linked lists (dynamic: never become full)
We will explore implementations based on
array and linked list
Lets see how to use an array to
implement a queue first
Array Implementation
A queue in C++ may be declared as a
structure containing two objects
An array to hold elements of the stacks
Integer to indicate position of stack top
#define SIZE 100
struct queue
{
int items[SIZE];
int front, rear;
};
Variable of type queue can be declared in main() as
queue q;
[Link] =-1; [Link] =0
Queue
void
insert(queue *q, int
Operations
x)
{
if ( q->rear==SIZE-1)
{
cout <<Queue overflow;
exit(1) ;
}
else
q->items[++q->rear)]=x;
}
Call of function will be like insert(&q, 2);
Queue
intOperations
remove(queue *q)
{
if (empty())
{
cout <<Queue underflow;
exit(1);
}
return(q->items[q->front++]);
}
int empty(queue *q)
{
if (q->rear < q->front)
return(TRUE)
else
return (FALSE);
}
No of elements in queue = [Link] [Link] + 1
Queue Array
Store queue as elements in array
Problem
Queue contents move
As result, can not add to back of queue,
even though queue is not full
Solution 1
When ever remove is called move all the elements in the
start of queue.
x = [Link][0]
for (int i=0; i <[Link]; i++)
[Link][i] = [Link][i+1];
[Link]--;
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 implemented by a circular
Queue
array
The Queue maintains two indices, front and rear, that initially are
both SIZE -1
1
0
3
rear
front
4
8
7
5
6
Insertion in queue
1
rear
back
4
0
front
x
5
9
8
6
7
Queue implemented using circular array
Assume that X denotes the indices in the buffer that hold
an object.
2
x
1
back
front
front
4
rear
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
x
1
back
front
rear
5
9
8
6
7
buffer
Circular Array
Implementation
#define SIZE 100
struct queue
{
int items[SIZE];
int front, rear;
}q;
[Link] = [Link] = SIZE -1;
Queue
Operations
int empty(queue *q)
{
if (q->rear == q->front)
return(TRUE)
else
return (FALSE);
}//Calling is as empty(&q)
int remove(queue *q)
{
if (empty(q))
{
cout <<Queue overflow;
exit(1) ;
}
if (q->front == SIZE-1)
q->front = 0;
else
q->front++;
return (q->items[q->front]);
}
Queue
Operations
2
front
rear
X
9
X
8
X
7
X
6
When array is full again front is equal to rear
so we can not differentiate whether array is
full or empty
Queue
Operations
2
front
rear
X
9
X
8
X
7
So we sacrifice one element.
Queue
Operations
void insert(queue *q, int x)
{
if (q->rear == SIZE-1)
q->rear = 0;
else
q->rear++;
if (pq->rear == pq->front)
{
cout << Queue Overflow;
exit(1);
}
q->items[q->rear] = x;
}
Priority Queues
Priority queue is a data structure in which
intrinsic ordering of elements 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
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 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
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
1
Sorted Array
Max
Priority
Queue
After First Remove Max
Operation
4
1
Max
Priority
Queue
Sorted Array
After Second Remove Max
Operation
4
1
Max
Priority
Queue
Sorted Array
After Third Remove Max
Operation
1
Max
Priority
Queue
Sorted Array
After Fourth Remove Max
Operation
Max
Priority
Queue
Sorted Array
After Fifth Remove Max
Operation
Max
Priority
Queue
1
Sorted Array
Queue
Operations
Suppose n elements of a priority queue are maintained in
positions 0 to n-1 of an array maxpq then pqinsert(pq,x) is
strightforward.
void insert(queue *pq, int x)
{
if (q->rear >= maxpq-1)
{
cout << Queue Overflow;
exit(1);
}
pq->items[pq->rear] = x;
pq->rear++;
} // elements are nor ordered.
For deletion there are two issues.
[Link] locate the smallest element.
[Link] 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.
[Link] operation labels a position as
empty but insertion is done at the first
empty position.
[Link] deletion compact the array by
shifting all elements past the deleted
element by one position and
decrementing [Link] by 1.
[Link] the array sorted.