Data Structures & Algorithms: Dated: 06-12-2010
Data Structures & Algorithms: Dated: 06-12-2010
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[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
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
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
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
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
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
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
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
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.
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
Deque defines a data structure in which item can be added or deleted at either the front or rear end.
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 X Y Z Rear
Deque
front = 2 rear = 4
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
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