Data Structures & Algorithms
Dated: 06-12-2010
Today Topics
What are Queues?
Representation of Queues
Operations on Queues
QInsert
QDelete
What are Queues?
Queue can be defined as:
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 the rear of the queue).
A queue is a waiting line – seen in daily life
A line of people waiting for a bank teller
A line of cars at a toll both
queue
Features:
A list structure with two access points called the
front and rear.
All insertions (enqueue) occur at the rear and
deletions (dequeue) occur at the front.
Varying length (dynamic).
Homogeneous components
Has a First-In, First-Out characteristic (FIFO)
application
Application of Queue
Purpose of queue is to provide some form
of buffering. Queues are used for:
Process Management: In timesharing
system, programs are added to a queue
and executed one after the other.
Buffer between the fast computer and a
slow printer.
In queues, scheme used is FIFO or LILO
implementation of Queues
There are two ways of implementing a queue:
Array (Static)
Link List (Dynamic)
A queue can be implemented with an array and
two integers.
The first integer front used for deletion item
from queue and rear used for insert item in
queue
Array Implementation
How to implement queue
Decide how many data member are needed to
implement the queue.
We need at least four types of variable.
An array to store the element
Variables queuefront to keep track of the first element
Queuerear to keep track of the last elelment.
Maxsize variable
How to use rear and front to access the queue
element.????
How they indicate that queue is full or empty.
Bounds of Queue
If FRONT: = NULL “empty”
If REARE := NUL “empty”
If REARE := N “overflow, full”
If FRONT=REARE != NULL then the
queue contain only one element.
If FRONT = REARE???????? Very
interesting situation.
Representation of Queues
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
34 12 53 61 9 23 -8 15 24 42
front rear
Queues Working
Currently Queue Status
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
front = 0 rear = 0
Queues Working
Currently Queue Status
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
32
front
rear QInsert(32)
front = 1 rear = 1
Queues Working
Currently Queue Status
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
32 44
front rear
QInsert(44)
front = 1 rear = 2
Queues Working
Currently Queue Status
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
32 44 65
front rear
QInsert(65)
front = 1 rear = 3
Queues Working
Currently Queue Status
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
32 44 65 25
front rear
QInsert(25)
front = 1 rear = 4
Queues Working
Currently Queue Status
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
32 44 65 25 53
front rear
QInsert(53)
front = 1 rear = 5
Queues Working
Currently Queue Status
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
44 65 25 53
front rear
QDelete()
front = 2 rear = 5
Queues Working
Currently Queue Status
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
65 25 53
front rear
QDelete()
front = 3 rear = 5
Think about it
What happen when
Rear= 10
Front = 9
????????? And you want to insert.
Two solution::::::
Check the front, if there is room then slides
all the element to fist position, but well
when queue is small.
Use circular array.
In case of circular queue how to
set rear point
Rear= (rear + 1) % maxsize; how???????
If Rear < maxsize -1, then
Rear + 1 <= mzxsize -1 and so
(Rear +1) % maxsize= Rear + 1.
If Rear == maxsize -1 (that is rear point to the
last position) then
Rear + 1 == maxsize and so
(Rear + 1) % maxsize==0. in this case Rear is set to 0.
And same can be done for Front adjustment.
Operations on Queue
Generally, Queue is implemented with only
two principle operations
InitializeQueue queue to an empty state
destroyQuere revome all the elements
isEmpthyQueue
isFullQueue
Front
rear
Insertion: adds an item to a queue
Deletion: delete an item from the queue
Algorithms of Insert Operation
QINSERT(QUEUE, N, FRONT, REAR, ITEM)
This algorithm insert an element ITEM into a queue.
Setp1. [Queue already filled]
if FRONT := 1 and REAR:= N,
write “overflow” and return
Step 2. [Find new value of REAR]
if FRONT := NULL [Queue initially empty]
Set FRONT:= 1 and REAR:= 1
Else if REAR:=N than
Set REAR:=1
Else
Set REAR:=REAR+1
[End of if structure]
Step 3. Set QUEUE[REAR]:=ITEM [this insert new item ]
Step 4. exit
Algorithms of Insert Operation
QInsert (X)
Algorithm for Insert element into the Queue
1. Start
2. if rear >= Max then
Print “Queue Overflow!”
else
rear = rear + 1
Q[rear] = X
if front = 0 then
front = 1
end if
end if
End
Algorithms of DELETE Operation
QDELETE(QUEUE, N, FRONT, REAR, ITEM)
This algorithm delete an element ITEM into a queue.
Setp1. [Queue already empty]
if FRONT := NULL write “UNDERFLOW”
return
Step 2. set ITEM:= QUEUE[FORNT]
Step 3. [Find new value of FRONT]
if FRONT := REAR than [Queue has only one element]
Set FRONT:= NULL and REAR:= NULL
Else if FRONT:=N than
Set FRONT;=1
Else
Set FRONT:=FRONT+1
[End of if structure]
Step 5. exit
Algorithms of Delete Operation
QDelete ()
Algorithm for delete element into the Queue
1. Start
2. if front = 0 then
Print “Queue Underflow!”
else
E = Q[front]
if front = rear then
front = rear = 0
else
front = front + 1
end if
end if
3. End
Circular Queue
When elements are deleted from the front, their spaces
cannot be used to store new elements.
To solve this problem, circular queue is used
Q[8] Q[1]
Q[7] Q[2]
Q[6] Q[3]
Q[5] Q[4]
Algorithms of CQInsert Operation
CQInsert (X)
Algorithm for Insert element into the Circular Queue
1. Start
2. if front = 1 and rear = Max then
Print “Queue Overflow!”
else if rear + 1 = front then
Print “Queue Overflow!”
else
if front = 0 and rear=0 then
front = rear = 1
else if rear = Max then
rear = 1
else
rear = rear + 1
end if
Q[rear] = X
end if
End
Algorithms of CQDelete Operation
CQDelete ()
Algorithm for delete element into the Circular Queue
1. Start
2. if front = 0 then
Print “Queue Underflow!”
else
E = Q[rear]
if front = rear then
front = rear = 0
else if front = Max then
front = 1
else
front = front + 1
end if
end if
End
DeQue
Word deque is a short form of double-ended queue.
Deque defines a data structure in which item can be added or deleted at either the front or rear end.
But no changes can be made elsewhere in the list.
Deque is a generalization of both a stack and a queue.
DeQue
There are two variations of deques. These are:
Input – Restricted Deque
It allows insertions only at one end but allows
deletions at both ends.
Output – Restricted Deque
It allows deletions only at one end but allows
insertions at both end
Representation of Deque
delete delete
insert insert
a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9] a[10]
34 12 53 61 9 23 -8 15 24 42
front Deque rear
Implementation of Deque
When an item is inserted at the Front of
DEQ, then front is decreased by 1.
When an item is inserted at the Rear of
DEQ, then rear is increased by 1.
When an item is deleted at the Front of
DEQ, then front is increased by 1.
When an item is deleted at the Rear of
DEQ, then rear is decreased by 1.
Implementation of Deque
In the following figure, the DEQ has some
items stored in it
Front X Y Z Rear
Deque
front = 2 rear = 4
The first element at the front of the deque
is empty and two elements at the rear are
also empty.
Implementation of Deque
If another item XX is added at the rear,
then the DEQ and values of front and rear
will be :
Front X Y Z XX Rear
Deque
front = 2 rear = 5
Implementation of Deque
If two items are deleted at the front and
one item is deleted at the rear, then the
DEQ and values of front and rear will be:
Front Z Rear
Deque
front = 4 rear = 4
Algorithms of DeQInsert Operation
DeQInsert (X, Side)
Algorithm for Insert element into the Deque
1. Start
2. if front = 0 and rear = 0 then
front = rear = 1
DQ[front] = X
Return
end if
[specify front or rear side to insert value]
3. [insert value at front of the queue] if Side = 1 then
if front > 1 then
front = front – 1
DQ[front] = X
else
Print “No space at front of the Deque!”
Algorithms of DeQInsert Operation
end if
else
if rear < Max then
rear = rear + 1
DQ[rear] = X
else
Print “No space at rear of the Deque!”
end if
end if
4. End
Algorithms of DeQDelete Operation
DeQDelete (Side)
Algorithm for delete element into the Deque
1. Start
2. If front = 0 and rear = 0 then
Print “DeQueue Underflow!”
Return
end if
3. if front = rear then
E= DQ[front]
front = rear = 0
Return
end if
Algorithms of DeQDelete Operation
4. if Side = 1 then
if front = Max then
E = DQ[front]
front = 0
else
E = DQ[front]
front = front + 1
end if
else
E = DQ[rear]
rear = rear - 1
end if
5. End
Priority Queues
Priority queue is a collection of elements where the
elements are stored according to their priority levels.
The order in which the elements should get added or
removed is decided by the priority of the element.
Following rules are applied to maintain a priority queue:
The element with a higher priority is processed before
any element of lower priority.
If there are elements with the same priority, then the
element added first in the queue would get processed.
Example for Priority Queues
Priority queues are used for implementing job
scheduling by the operating system where jobs
with higher priorities are to be processed first.
Another application of priority queues is
simulation systems where priority corresponds
to event times.
Representation of Priority Queues
Priority queues can be represented in the
several ways.
Best way to represent it is to use a separate
queue for each level of priority.
Each such queue is represented in circular
fashion and has its own front and rear.
Representation of Priority Queues
Usually, an array of arrays, i.e. a two-dimensional array
is used to represent.
1 X Y
2 A B C
3 M N O P
4
Next Lecture
Pointer Review
Linked List
Representation of Link List
Operations of Linked List
Circular Linked List
Double Linked List (Two-Way List)
Representation of Double Linked List
Operations of Double Linked List