introduction to c programming ce00312 1 n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Introduction to C Programming CE00312-1 PowerPoint Presentation
Download Presentation
Introduction to C Programming CE00312-1

Loading in 2 Seconds...

play fullscreen
1 / 25

Introduction to C Programming CE00312-1 - PowerPoint PPT Presentation


  • 77 Views
  • Uploaded on

Introduction to C Programming CE00312-1. Lecture 12 Circular Queue and Priority Queue Data Structures. 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)

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Introduction to C Programming CE00312-1' - beryl


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
introduction to c programming ce00312 1

Introduction to C ProgrammingCE00312-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

endend

Deletion from a queue (dequeue)

deleteq (item , queue) beginif front = rear then queueempty else begin

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

Recap on Queues
queues

4

4

4

4

3

3

3

3

2

2

2

2

1

1

1

1

0

0

0

0

Queues

e

d

c

c

c

b

a

Front = 2

Rear = 5

Array size = 5

Front = 0

Rear = 0

Front = 0

Rear = 3

Front = 2

Rear = 3

circular array
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
c queue

4

4

4

4

3

3

3

3

2

2

2

2

1

1

1

1

0

0

0

0

C Queue

e

e

e

d

d

c

c

g

f

f

f

Front = 2

Rear = 0

Front = 2

Rear = 1

Front = 4

Rear = 1

Front = 0

Rear = 2

circular queue
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
circular queues
Circular Queues

4

  • If rear++ == front
    • Insertion would cause overflow
  • If rear = front
    • Removal would cause underflow

c

3

d

2

1

b

a

0

Front = 3

Rear = 2

circular queue functions
Addition to a queue (enqueue)

addq (item , queue)begin if rear + 1 = front then queueoverflow else begin

q[rear] = item

increment rear rear = rear mod (size –1)endend

Deletion from a queue (dequeue)

deleteq (item , queue) beginif front = rear then queueunderflow else

begin

item = q[front]        front = front+1 front = front mod (size –1)

endend

Circular Queue Functions
priority queue
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
priority
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
heap sort1
Heap sort

Add 44 to heap

heap sort4
Heap sort

Now add 47

heap sort6
Heap sort

End result

slide18
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?
heap sort7
Heap sort

Root removed

array implementation

45

44

33

22

29

12

24

1 2 3 4 5 6 7 8 9

Array Implementation

Where leaf nodes are 2n and 2n+1

Or

root is n div 2 using integer division

array implementation1

45

44

33

22

29

12

24

21

24

1 2 3 4 5 6 7 8 9

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

Array Implementation
recap
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