Queue deque and priority queue implementations
Download
1 / 40

Queue, Deque, and Priority Queue Implementations - PowerPoint PPT Presentation


  • 361 Views
  • Updated On :

Queue, Deque, and Priority Queue Implementations. Chapter 24. Slides by Steve Armstrong LeTourneau University Longview, TX ã 2007, Prentice Hall. Chapter Contents. A Linked List Implementation of a Queue An Array-Based Implementation of a Queue A Circular Array

Related searches for Queue, Deque, and Priority Queue Implementations

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 'Queue, Deque, and Priority Queue Implementations' - arnon


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
Queue deque and priority queue implementations l.jpg

Queue, Deque, and Priority Queue Implementations

Chapter 24

Slides by Steve ArmstrongLeTourneau UniversityLongview, TX

ã 2007,Prentice Hall


Chapter contents l.jpg
Chapter Contents

  • ALinked List Implementation of a Queue

  • An Array-Based Implementation of a Queue

    • A Circular Array

    • A Circular Array with One Unused Location

  • A Vector-Based Implementation of a Queue

  • Circular Linked Implementations of a Queue

    • A Two-Part Circular Linked Chain

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Chapter contents3 l.jpg
Chapter Contents

  • Java Class Library: The Class AbstractQueue

  • A Doubly Linked Implementation of a Queue

  • Possible Implementations of a Priority Queue

  • Java Class Library: The Class PriorityQueue

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A linked implementation of a queue 1 l.jpg
A Linked Implementation of a Queue 1

  • Use chain of linked nodes for the queue

    • Two ends at opposite ends of chain

    • Accessing last node inefficient

    • Could keep a reference to the tail of the chain

  • Place front of queue at beginning of chain

  • Place back of queue at end of chain

    • With references to both

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A linked implementation of a queue l.jpg
A Linked Implementation of a Queue

Fig. 24-1 A chain of linked nodes that implements a queue.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A linked implementation of a queue6 l.jpg
A Linked Implementation of a Queue

Fig. 24-2 (a) Before adding a new node to an empty chain; (b) after adding to it.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A linked implementation of a queue 3 l.jpg
A Linked Implementation of a Queue 3

Fig. 24-3 (a) Before adding a new node to the end of a chain; (b) after adding it.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A linked implementation of a queue 4 l.jpg
A Linked Implementation of a Queue 4

Fig. 24-4 (a) A queue of more than one entry; (b) after removing the queue's front.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A linked implementation of a queue9 l.jpg
A Linked Implementation of a Queue

Fig. 24-5 (a) A queue of one entry; (b) after removing the queue's front.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A linked implementation of a queue10 l.jpg
A Linked Implementation of a Queue

  • View source code of linked implementation of the ADT queue

  • Note methods

    • enqueue

    • getFront

    • dequeue

    • isEmpty

    • clear

    • private class Node

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Array based implementation of a queue 7 l.jpg
Array-Based Implementation of a Queue 7

  • Let queue[0] be the front

    • frontIndex, backIndex are indices of front and back

  • If we insist queue[0] is front

    • Must shift entries when we remove the front

  • Instead move frontIndex

    • Problem then is array can become full

    • But now beginning of array could be empty and available for use

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Array based implementation of a queue l.jpg
Array-Based Implementation of a Queue

Fig. 24-6 An array that represents a queue without shifting its entries: (a) initially; (b) after removing the front twice;

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Array based implementation of a queue13 l.jpg
Array-Based Implementation of a Queue

Fig. 24-6 An array that represents a queue without shifting its entries: (c) after several more additions & removals; (d) after two additions that wrap around to the beginning of the array

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A circular array l.jpg
A Circular Array

  • When queue reaches end of array

    • Add subsequent entries to beginning

  • Array behaves as though it were circular

    • First location follows last one

  • Use modulo arithmetic on indicesbackIndex = (backIndex + 1) % queue.length

  • Note: with circular arrayfrontIndex == backIndex + 1both when queue is empty and when full

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A circular array15 l.jpg
A Circular Array

Fig. 24-7 A circular array that represents a queue: (a) when full; (b) after removing 2 entries; (c) after removing 3 more entries;

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A circular array16 l.jpg
A Circular Array

Fig. 24-7 A circular array that represents a queue: (d) after removing all but one entry; (e) after removing remaining entry.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A circular array with one unused location l.jpg
A Circular Array with One Unused Location

Fig. 24-8 A seven-location circular array that contains at most six entries of a queue … continued →

Allows us to distinguish between empty and full queue

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A circular array with one unused location18 l.jpg
A Circular Array with One Unused Location

Fig. 24-8 (ctd.) A seven-location circular array that contains at most six entries of a queue.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A circular array with one unused location19 l.jpg
A Circular Array with One Unused Location

  • Note source code of class of an array-based implementation of the ADT queue

  • Note methods

    • enqueue

    • getFront

    • dequeue

    • doubleArray // a private method

    • isEmpty

    • isArrayFull

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Array based implementation of a queue20 l.jpg
Array-Based Implementation of a Queue

Fig. 24-9 An array-base queue: (a) initially; (b) after removing its front by incrementing frontIndex;

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Array based implementation of a queue21 l.jpg
Array-Based Implementation of a Queue

Fig. 24-9 An array-base queue: (c) after removing its front by setting queue[frontIndex] to null, then incrementing frontIndex.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Array based implementation of a queue22 l.jpg
Array-Based Implementation of a Queue

Fig. 24-10 Doubling the size of an array-based queue

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Vector based implementation of a queue l.jpg
Vector-Based Implementation of a Queue

  • Maintain front of queue at beginning of vector

  • Use addElement method to add entry at back

    • Vector expands as necessary

  • When remove front element, remaining elements move so new front is at beginning of vector

    • Indexes at front and back not needed

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Vector based implementation of a queue24 l.jpg
Vector-Based Implementation of a Queue

Fig. 24-11 A vector that represents a queue.

Click to view source code of vector-based implementation of ADT queue

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Circular linked implementations of a queue l.jpg
Circular Linked Implementations of a Queue

  • Last node references first node

    • Now we have a single reference to last node

    • And still locate first node quickly

  • No node contains a null

  • When a class uses circular linked chain for queue

    • Only one data item in the class

    • The reference to the chain's last node

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Circular linked implementations of a queue26 l.jpg
Circular Linked Implementations of a Queue

Fig. 24-12 A circular linked chain with an external reference to its last node that (a) has more than one node; (b) has one node; (c) is empty.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A two part linked chain l.jpg
A Two-Part Linked Chain

  • Linked nodes that form the queue followed by linked nodes available for use in the queue

    • queueNode references front of queue node

    • freeNode references first available node following end of queue

  • In essence we have two chains

    • One for the queue

    • One for available nodes

    • All joined in a circle

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A two part linked chain28 l.jpg
A Two-Part Linked Chain

Fig. 24-13 A two-part circular linked chain that represents both a queue and the nodes available to the queue.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A two part linked chain29 l.jpg
A Two-Part Linked Chain

Fig. 24-14 A two-part circular linked chain that represents a queue: (a) when it is empty; (b) after adding one entry; (c) after adding three more entries.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A two part linked chain30 l.jpg
A Two-Part Linked Chain

Fig. 24-14 A two-part circular linked chain that represents a queue: (d) after removing the front; (e) after adding one more entry

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A two part linked chain31 l.jpg
A Two-Part Linked Chain

Fig. 24-15 A chain that requires a new node for an addition to a queue: (a) before the addition; (b) after the addition.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A two part linked chain32 l.jpg
A Two-Part Linked Chain

Fig. 24-16 A chain with a node available for an addition to a queue: (a) before the addition; (b) after the addition.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A two part linked chain33 l.jpg
A Two-Part Linked Chain

  • View source code of a two-part circular linked implementation of the ADT queue

  • Note the node implements

  • Note methods

    • enqueue

    • getFront

    • dequeue

    • isEmpty

    • isChainFull

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A doubly linked implementation of a deque l.jpg
A Doubly Linked Implementation of a Deque

  • Chain with head reference enables reference of first and then the rest of the nodes

  • Tail reference allows reference of last node but not next-to-last

  • We need nodes that can reference both

    • Previous node

    • Next node

  • Thus the doubly linked chain

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Java class library the class abstractqueue l.jpg
Java Class Library: The Class AbstractQueue

  • Methods provided by this class

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A doubly linked implementation of a deque36 l.jpg
A Doubly Linked Implementation of a Deque

Fig. 24-17 A doubly linked chain with head and tail references

Click to view source code of linked implementation of ADT deque

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A doubly linked implementation of a deque37 l.jpg
A Doubly Linked Implementation of a Deque

Fig. 24-18 Adding to the back of a non empty deque: (a) after the new node is allocated; (b) after the addition is complete.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


A doubly linked implementation of a deque38 l.jpg
A Doubly Linked Implementation of a Deque

Fig. 24-19 (a) a deque containing at least two entries; (b) after removing first node and obtaining reference to the deque's first entry.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Possible implementations of a priority queue l.jpg
Possible Implementations of a Priority Queue

Fig. 24-20 Two possible implementations of a priority queue using (a) an array; (b) a chain of linked nodes.

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X


Java class library the class priorityqueue l.jpg
Java Class Library: The Class PriorityQueue

  • Extends java.util.AbstractQueue

  • Implements methods in java.util.Queue

    • They adhere to specs of priority queue

  • View methods specified

Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X