Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
1
Chapter 20 Queues and Priority Queues CS165 Colorado State - - PowerPoint PPT Presentation
Chapter 20 Queues and Priority Queues CS165 Colorado State University Original slides by Daniel Liang Modified slides by Wim Bohm, Sudipto Ghosh Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 1
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
1
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
2
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
3
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
4
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
. 70 . 55 . Back Front . 60 Item Next
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Last Node
1.
2.
3.
New node
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Node firstNode = lastNode.next; if (firstNode == lastnode) { lastNode = null;} else{lastNode.next = firstNode.next;} return firstNode.item;
CS200 -Queues
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
CS200 -Queues
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
When either front or back advances past MAX_QUEUE-1, it wraps around (to 0: using % MAX_QUEUE)
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
CS200 -Queues
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
When the queue is EMPTY, front is one slot ahead of back.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
When the queue is FULL, front is one slot ahead of back. Problem? Maintain size: 0:empty max_queue: full Solution?
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
! Initializing
front = 0 back = MAX_QUEUE-1 count = 0
! Adding
back = (back+1) % MAX_QUEUE; items[back] = newItem; ++count;
! Deleting / dequeueing
removeItem = items[front]; front = (front +1) % MAX_QUEUE;
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
21
Run
PriorityQueueDemo
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
22