Introduction to C Programming CE003121 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Introduction to C Programming CE003121

Description:

if front = rear then queueempty. else begin. item = q[front] front = front 1. end. end ... If pointer to front catches up with rear on dequeuing then underflow ... – PowerPoint PPT presentation

Number of Views:68
Avg rating:3.0/5.0
Slides: 26
Provided by: rho2
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C Programming CE003121


1
Introduction to C ProgrammingCE00312-1
  • Lecture 12
  • Circular Queue and Priority Queue Data Structures

2
Recap on Queues
  • Addition to a queue (enqueue)
  •  
  • addq (item , queue)begin     if rear size
    then queuefull     else begin
  •          qrear item increment rear
  •     end end
  • Deletion from a queue (dequeue)
  •  
  • deleteq (item , queue) begin     if front
    rear then queueempty     else begin
  • item qfront         front front1
        end end

3
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
4
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

5
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
6
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 cant add element
  • Test for remove happened before front is updated

7
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
8
Circular Queue Functions
  • Addition to a queue (enqueue)
  •  
  • addq (item , queue)begin     if rear 1
    front then queueoverflow     else begin
  • qrear item
  • increment 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 qfront        front front1
    front front mod (size 1)
  •      end end

9
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

10
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

11
Heap sort
12
Heap sort
Add 44 to heap
13
Heap sort
14
Heap sort
15
Heap sort
Now add 47
16
Heap sort
17
Heap sort
End result
18
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?

19
Heap sort
Root removed
20
Heap sort
21
Heap sort
22
Heap sort
23
Array Implementation
Where leaf nodes are 2n and 2n1 Or root is n
div 2 using integer division
24
Array Implementation
25
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
Write a Comment
User Comments (0)
About PowerShow.com