introduction to c programming ce00312-1 lecture 12 circular queue and priority queue data structures

25
Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Upload: dashawn-ironside

Post on 16-Dec-2015

235 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Introduction to C Programming

CE00312-1

Lecture 12

Circular Queue and Priority Queue Data Structures

Page 2: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Recap on Queues

Addition to a queue (enqueue)

 

addq (item , queue)begin     if rear = size then queuefull

    else begin

         q[rear] = item increment rear

    end end

Deletion from a queue (dequeue)

 deleteq (item , queue)

begin     if front = rear then queueempty     else begin

item = q[front]         front = front+1     end end

Page 3: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Queues

Front = 0

Rear = 0

4

3

1

2

0

4

3

1

2

0

4

3

1

2

0

4

3

1

2

0

Front = 0

Rear = 3

Front = 2

Rear = 3

Front = 2

Rear = 5

Array size = 5

d

c

e

a

b

c c

Page 4: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Circular Array

Allows array to wrap round to the front Array bounds no longer dictate empty or full How do I define empty /Full Underflow/Overflow If pointer to front catches up with rear on dequeuing

then underflow If result of enqueing means rear pointer = front then

overflow

Page 5: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

C Queue

Front = 2

Rear = 0

4

3

1

2

0

4

3

1

2

0

4

3

1

2

0

4

3

1

2

0

Front = 2

Rear = 1

Front = 4

Rear = 1

Front = 0

Rear = 2

d

c

e e

c

d

e

f f f

g

Page 6: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Circular Queue

Front = rear is used to define both empty and full

Sacrifice one element in the array by initialising size to size –1

If rear = front can’t add element Test for remove happened before front is

updated

Page 7: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Circular Queues

If rear++ == front Insertion would

cause overflow If rear = front

Removal would cause underflow

4

3

1

2

0

Front = 3

Rear = 2

c

d

b

a

Page 8: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Circular Queue FunctionsAddition to a queue (enqueue) addq (item , queue)

begin     if rear + 1 = front then queueoverflow     else begin

q[rear] = itemincrement rear

rear = rear mod (size –1)    end end

Deletion from a queue (dequeue) deleteq (item , queue)

begin     if front = rear then queueunderflow     else

begin item = q[front]

        front = front+1 front = front mod (size –1)

     end end

Page 9: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Priority Queue Stacks and queues are linear structures Very efficient in terms of insertion and deletion Not so efficient for locating specific data We have to do several operations of load and unload to

access specific data Priority is a means of storing data such that unloading

produces most relevant data to an operation E.g. most important process running in job scheduler Uses ‘heap sort’ which always puts highest priority at head of

queue Not the same as a conventional ordinal sort

Page 10: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Priority Priority is defined as the largest or highest

ranking Stack deletes newest Queue deletes oldest Priority queue deletes highest priority Newest item inserted to retain integrity of

priority Employs heap sort

Page 11: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3

2 6

2 9

1 2 2 4

3 3

4 5

Page 12: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 4 4

2 6

2 9

1 2 2 4

3 3

4 5

Add 44 to heap

Page 13: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 2 6

4 4

2 9

1 2 2 4

3 3

4 5

Page 14: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 2 6

2 9

4 4

1 2 2 4

3 3

4 5

Page 15: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 2 6

2 9

4 4

4 7

1 2 2 4

3 3

4 5

Now add 47

Page 16: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 2 6

2 9

4 4

1 2

4 7 2 4

3 3

4 5

Page 17: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 2 6

2 9

4 4

1 2

2 4

4 7

End result

Page 18: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap

Attempts to maintain complete tree Balanced Fills from left to right on each level No more than one level between leaves Root always contains highest priority value Deletion always is from root Heap reorganised on deletion How?

Page 19: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 2 6

2 9

4 4

1 2

3 3 2 4

4 5

E m p ty

Root removed

Page 20: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 2 6

2 9

4 4

3 3 2 4

4 5

1 2

Page 21: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 2 6

2 9

4 4

3 3 2 4

1 2

4 5

Page 22: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Heap sort

1 0 7

2 2

1 3 2 6

2 9

4 4

1 2 2 4

3 3

4 5

Page 23: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Array Implementation

45 44 33 22 29

1 2 3 4 5 6 7 8 9

12 24

Where leaf nodes are 2n and 2n+1

Or

root is n div 2 using integer division

Page 24: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Array Implementation

45 44 33 22 29

1 2 3 4 5 6 7 8 9

12 24 21 24

21 is in position 8 – 8/2 = 4 22 is in pos 4 – no swap

24 is in position 9 – 9/2 = 4 22 is in pos 4 – swap

Page 25: Introduction to C Programming CE00312-1 Lecture 12 Circular Queue and Priority Queue Data Structures

Recap Circular queues more efficient than standard

queue Linked list implementation of queue obviates

need for circular queue. Dynamic. Priority Queue always yields highest priority for

deletion Implements heap sort Maintains complete tree structure