A List Implementation that Links Data

Slides:



Advertisements
Similar presentations
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Advertisements

A Bag Implementation that Links Data Chapter 3 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A List Implementation That Links Data Chapter 6 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X Announcements.
Iterators Chapter 8 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Queue, Deque, and Priority Queue Implementations Chapter 24 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Stack Implementations Chapter 22 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Inheritance and Lists Chapter 14 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
List Implementations That Link Data Chapter 6. 2 Chapter Contents Linked Data Forming a Chains The Class Node A Linked Implementation Adding to End of.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Bag Implementations that Use Arrays Chapter 2 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Searching Chapter 18 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Binary Search Tree Implementation Chapter 27 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Data Structures and Abstractions with Java, 4e Frank Carrano
An Introduction to Sorting Chapter 8 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with.
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Lists Chapter 4 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Bag Implementations that Use Arrays Chapter 2 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Stack Implementations Chapter 6 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Stacks Chapter 5 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
A Binary Search Tree Implementation Chapter 25 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Objectives Upon completion you will be able to: Explain the design, use, and operation of a linear.
Queues and Priority Queue Implementations Chapter 14 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
The Efficiency of Algorithms Chapter 9 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Chapter 7 Stacks © 2006 Pearson Addison-Wesley. All rights reserved 7A-1.
Link-Based Implementations
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Bag Implementations that Use Arrays
An Introduction to Sorting
A Bag Implementation that Links Data
Chapter 16 Tree Implementations
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Chapter 12 Sorted Lists and their Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Hashing as a Dictionary Implementation
Queue and Priority Queue Implementations
Chapter 14: Queue and Priority Queue Implementations
List Implementations Chapter 9.
Slides by Steve Armstrong LeTourneau University Longview, TX
Dynamic Data Structures and Generics
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Implementations
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queues cont. Chapter 8 © 2011 Pearson Addison-Wesley. All rights reserved.
Bag Implementations that Use Arrays
Queues and Priority Queue Implementations
Chapter 7 © 2011 Pearson Addison-Wesley. All rights reserved.
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Uses An Array
© 2016 Pearson Education, Ltd. All rights reserved.
© 2016 Pearson Education, Ltd. All rights reserved.
Queues, Deques, and Priority Queues
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

A List Implementation that Links Data Chapter 14 Data Structures and Abstractions with Java, 4e, Global Edition Frank Carrano © 2016 Pearson Education, Ltd.  All rights reserved.

Advantages of Linked Implementation Uses memory only as needed When entry removed, unneeded memory returned to system Avoids moving data when adding or removing entries © 2016 Pearson Education, Ltd.  All rights reserved.

Adding a Node at Various Positions Possible cases: Chain is empty Adding node at chain’s beginning Adding node between adjacent nodes Adding node to chain’s end © 2016 Pearson Education, Ltd.  All rights reserved.

Adding a Node This pseudocode establishes a new node for the given data © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Adding a Node FIGURE 14-1 (a) An empty chain and a new node; (b) after adding the new node to a chain that was empty © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Adding a Node This pseudocode describes the steps needed to add a node to the beginning of a chain. © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Adding a Node FIGURE 14-2 A chain of nodes (a) just prior to adding a node at the beginning; (b) just after adding a node at the beginning © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Adding a Node Pseudocode to add a node to a chain between two existing, consecutive nodes © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Adding a Node FIGURE 14-3 A chain of nodes (a) just prior to adding a node between two adjacent nodes; (b) just after adding a node between two adjacent nodes © 2016 Pearson Education, Ltd.  All rights reserved.

Adding a Node Steps to add a node at the end of a chain. © 2016 Pearson Education, Ltd.  All rights reserved.

Adding a Node FIGURE 14-4 A chain of nodes (a) prior to adding a node at the end © 2016 Pearson Education, Ltd.  All rights reserved.

Adding a Node FIGURE 14-4 A chain of nodes (b) after locating its last node; © 2016 Pearson Education, Ltd.  All rights reserved.

Adding a Node FIGURE 14-4 A chain of nodes (c) after adding a node at the end © 2016 Pearson Education, Ltd.  All rights reserved.

Removing a Node from Various Positions Possible cases Removing the first node Removing a node other than first one © 2016 Pearson Education, Ltd.  All rights reserved.

Removing a Node Steps for removing the first node. © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Removing a Node FIGURE 14-5 A chain of nodes (a) just prior to removing the first node; (b) just after removing the first node © 2016 Pearson Education, Ltd.  All rights reserved.

Removing a Node Removing a node other than the first one. © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Removing a Node FIGURE 14-6 A chain of nodes (a) just prior to removing an interior node; (b) just after removing an interior node © 2016 Pearson Education, Ltd.  All rights reserved.

Removing a Node Operations on a chain depended on the method getNodeAt © 2016 Pearson Education, Ltd.  All rights reserved.

Design Decision A Link to Last Node FIGURE 14-7 A linked chain with (a) a head reference; (b) both a head reference and a tail reference © 2016 Pearson Education, Ltd.  All rights reserved.

Data Fields and Constructor LISTING 14-1 An outline of the class LList © 2016 Pearson Education, Ltd.  All rights reserved.

Data Fields and Constructor LISTING 14-1 An outline of the class LList © 2016 Pearson Education, Ltd.  All rights reserved.

Adding to the End of the List The method add assumes method getNodeAt © 2016 Pearson Education, Ltd.  All rights reserved.

Adding at a Given Position Java method. © 2016 Pearson Education, Ltd.  All rights reserved.

Method isEmpty Note use of assert statement. © 2016 Pearson Education, Ltd.  All rights reserved.

Method toArray Traverses chain, loads an array. © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Testing Core Methods LISTING 14-2 A main method that tests part of the implementation of the ADT list © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. Testing Core Methods LISTING 14-2 A main method that tests part of the implementation of the ADT list © 2016 Pearson Education, Ltd.  All rights reserved.

Continuing the Implementation The remove method returns the entry that it deletes from the list © 2016 Pearson Education, Ltd.  All rights reserved.

Continuing the Implementation Replacing a list entry requires us to replace the data portion of a node with other data. © 2016 Pearson Education, Ltd.  All rights reserved.

Continuing the Implementation Retrieving a list entry is straightforward. © 2016 Pearson Education, Ltd.  All rights reserved.

Continuing the Implementation Checking to see if an entry is in the list, the method contains. © 2016 Pearson Education, Ltd.  All rights reserved.

A Refined Implementation FIGURE 14-8 A linked chain with both a head reference and a tail reference © 2016 Pearson Education, Ltd.  All rights reserved.

A Refined Implementation FIGURE 14-9 Adding a node to the end of a nonempty chain that has a tail reference © 2016 Pearson Education, Ltd.  All rights reserved.

A Refined Implementation Revision of the first add method © 2016 Pearson Education, Ltd.  All rights reserved.

A Refined Implementation Implementation of the method that adds by position. © 2016 Pearson Education, Ltd.  All rights reserved.

A Refined Implementation Implementation of the method that adds by position. © 2016 Pearson Education, Ltd.  All rights reserved.

A Refined Implementation FIGURE 14-10 Removing the last node from a chain that has both head and tail references when the chain contains (a) one node © 2016 Pearson Education, Ltd.  All rights reserved.

A Refined Implementation FIGURE 14-10 Removing the last node from a chain that has both head and tail references when the chain contains (b) more than one node © 2016 Pearson Education, Ltd.  All rights reserved.

A Refined Implementation Implementation of the remove operation: © 2016 Pearson Education, Ltd.  All rights reserved.

A Refined Implementation Implementation of the remove operation: © 2016 Pearson Education, Ltd.  All rights reserved.

Efficiency of Using a Chain FIGURE 14-11 The time efficiencies of the ADT list operations for three implementations, expressed in Big Oh notation © 2016 Pearson Education, Ltd.  All rights reserved.

Java Class Library: The Class LinkedList Implements the interface List LinkedList defines more methods than are in the interface List You can use the class LinkedList as implementation of ADT queue deque or list. © 2016 Pearson Education, Ltd.  All rights reserved.

© 2016 Pearson Education, Ltd. All rights reserved. End Chapter 14 © 2016 Pearson Education, Ltd.  All rights reserved.