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 rights reserved.
Queues and Priority Queues Queue is a first-in/first-out data structure. ! Elements are added to the end of the queue. ! Elements are removed from the beginning of the queue. Priority queues assign priorities to elements. ! The element with the highest priority is removed first. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 2 rights reserved.
The Queue Interface Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 3 rights reserved.
Using LinkedList for Queue Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 4 rights reserved.
5 Reference-Based Implementation 1 A linked list with two external references – A reference to the front – A reference to the back At which end do we enqueue / dequeue? 60 . 70 . 55 Next Item add / offer at BACK remove at FRONT What if we did it . . the other way? Back Front Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
6 Reference-Based Implementation 2 A circular linked list with one external reference – lastNode references the back of the queue – lastNode.getNext() references the front 60 . 70 . 55 . Last Node: node reference Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
7 Adding an item into a nonempty queue newNode.next = lastNode.next; 1. lastNode.next = newNode; 2. lastNode = newNode; 3. 60 . 70 . 55 85 . . Last Node New node Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
8 Adding an item to an empty queue ! Insert a new item into the empty queue 60 . . Last Node Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Add item to queue public void add (Object newItem){ Node newNode = new Node(newItem); A. Empty queue if (isEmpty()){ newNode.next = newNode; } else { B. items in queue newNode.next = lastNode.next; lastNode.next = newNode; } lastNode = newNode; } 9 Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
10 Removing an item from queue public Object remove() throws QueueException{ if ( !isEmpty ()){ Node firstNode = lastNode.next ; Why? if (firstNode == lastNode) { lastNode = null; } else{ lastNode.next = firstNode.next ; } return firstNode.item ; } else { exception handling.. } } Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Removing an Item What happens to this node? 60 . 70 . 80 . . . First Node Last Node Node firstNode = lastNode.next ; if (firstNode == lastnode) { lastNode = null; } else{ lastNode.next = firstNode.next ;} return firstNode.item ; 11 CS200 -Queues Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
12 Naïve Array-Based Implementation Drift wastes space How do we initialize front and back? (Hint: what does a queue with a single element look like? what does an empty queue look like? ) Problem: Drift Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Circular implementation of a 13 queue solves drift FRONT MAX_QUEUE-1 0 a 6 1 e i 5 2 o 4 3 BACK Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
14 Solving Drift: ! First Delete FRONT MAX_QUEUE-1 0 a 6 1 e i 5 2 o 4 3 BACK Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All CS200 -Queues rights reserved.
Solving Drift: ! Second Delete 0 MAX_QUEUE-1 FRONT 6 1 e i 5 2 o 4 3 BACK Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
16 Solving Drift ! add u When either front or back advances past MAX_QUEUE-1, it wraps around (to 0: MAX_QUEUE-1 0 using % MAX_QUEUE) 6 1 FRONT i 5 2 u o BACK BACK 4 3 Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
17 Queue with Single Item ! back and front are pointing at the same slot. 0 MAX_QUEUE-1 6 1 5 2 u BACK 4 3 FRONT Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All CS200 -Queues rights reserved.
18 Empty Queue: remove Single Item 0 MAX_QUEUE-1 Remove last item. 6 1 – front passed back . When the queue is EMPTY, front is one slot ahead of 5 2 back . u T N O R F BACK 4 3 F R O N T Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
19 Insert the last item back catches up to front when the queue BACK becomes full . FRONT 0 MAX_QUEUE-1 Problem? n a 6 1 Solution? f e BACK Maintain size: b i 5 2 0:empty o u max_queue: full When the queue is FULL, front is one slot ahead of 4 3 back . Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
20 Wrapping the values for front and back ! 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; --count; Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
The PriorityQueue Class Run PriorityQueueDemo Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 21 rights reserved.
Implementation of Priority Queue ! Naïve: ArrayList or Linked List – Keeping the elements ordered – This will make add costly ! Better implementation: Heap (later) Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All 22 rights reserved.
Recommend
More recommend