A List Implementation That Links Data

Slides:



Advertisements
Similar presentations
The Efficiency of Algorithms Chapter 4 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Advertisements

An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Queue, Deque, and Priority Queue Implementations Chapter 24 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.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Sorted Lists and Their Implementations Chapter 12 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Queue, Deque, and Priority Queue Implementations Chapter 11 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Tree Implementations Chapter 24 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graph Implementations Chapter 29 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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Searching Chapter 18 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
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.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
A Heap Implementation Chapter 26 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
A Bag Implementation that Links Data Chapter 3 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Linked Lists Chapter 5 (continued)
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 Linked Lists.
Bag Implementations that Use Arrays
An Introduction to Sorting
A Bag Implementation that Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queue, Deque, and Priority Queue Implementations
A Bag Implementation that Links Data
Queue, Deque, and Priority Queue Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Linked Lists: Implementation of Queue & Deque
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
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations Chapter 9.
List Implementations that Use Arrays
A List Implementation That Links Data
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack Implementations
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Queues CSC212.
A Heap Implementation Chapter 26 Adapted from Pearson Education, Inc.
Linked Lists Chapter 5 (continued)
The Efficiency of Algorithms
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Bag Implementations that Use Arrays
A Binary Search Tree Implementation
Linked Lists Chapter 5 (continued)
Copyright ©2012 by Pearson Education, Inc. All rights reserved
List Implementations that Use Arrays
Stack Implementations
© 2016 Pearson Education, Ltd. All rights reserved.
A List Implementation that Links Data
© 2016 Pearson Education, Ltd. All rights reserved.
Queue, Deque, and Priority Queue Implementations
Presentation transcript:

A List Implementation That Links Data Chapter 14 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Operations on a Chain of Linked Nodes Adding a Node at Various Positions Removing a Node from Various Positions The Private Method getNodeAt … Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Beginning the Implementation The Data Fields and Constructor Adding to the End of the List Adding at a Given Position Within the List The Methods isEmpty and toArray Testing the Core Methods … Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Contents Continuing the Implementation A Refined Implementation The Tail Reference The Efficiency of Using a Chain to Implement the ADT List Java Class Library: The Class LinkedList Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Objectives Describe linked organization of data Implement add methods of the ADT list by using linked chain of nodes Test partially complete implementation of a class Copyright ©2012 by Pearson Education, Inc. All rights reserved

Operations on a Chain of Linked Nodes Adding a Node at Various Positions Case 1: Chain is empty Case 2: Adding node at chain’s beginning Case 3: Adding node between adjacent nodes Case 4: Adding node to chain’s end Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Case 1 Figure 14-1 (a) An empty chain and a new node; (b) after adding the new node to a chain that was empty Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Case 2 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 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Case 3 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 Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Case 4 FIGURE 14-4 A chain of nodes (a) prior to adding a node at the end; (b) after locating its last node; (c) after adding a node at the end Copyright ©2012 by Pearson Education, Inc. All rights reserved

Removing a Node from Various Positions Case 1: Removing first node FIGURE 14-5 A chain of nodes (a) just prior to removing the first node; (b) just after removing the first node Copyright ©2012 by Pearson Education, Inc. All rights reserved

Removing a Node from Various Positions Case 2: Removing node other than first FIGURE 14-6 A chain of nodes (a) just prior to removing an interior node; (b) just after removing an interior node Copyright ©2012 by Pearson Education, Inc. All rights reserved

The Private Method getNodeAt Returns reference to node at specified position Copyright ©2012 by Pearson Education, Inc. All rights reserved

Beginning the Implementation Design Decision: The structure of the chain of linked nodes Add statements at beginning Use code from Cases 1 and 2 Add at end Must invoke getNodeAt Could maintain both head and tail references (deferred until later) Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 14-7 A linked chain with (a) a head reference; (b) both a head reference and a tail reference Copyright ©2012 by Pearson Education, Inc. All rights reserved

Implementation View whole class, Listing 14-1 Note Methods for add Method isEmpty Method toArray Consider initial test program, Listing 14-2 Output of listing 14-2 Note: Code listing files must be in same folder as PowerPoint files for links to work Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Implementation Finishing the implementation Method remove Method replace Method contains Refining the implementation Add tail reference Avoids traversal of entire chain when add is called Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 14-8 A linked chain with both a head reference and a tail reference Copyright ©2012 by Pearson Education, Inc. All rights reserved

Implement with Tail Reference Must alter method clear lastNode = null; Adding to end of list For empty list, head and tail reference new node For non-empty list, use lastNode.setNextNode(newNode); lastNode = newNode; View changes, Listing 14-A Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved FIGURE 14-9 Adding a node to the end of a nonempty chain that has a tail reference Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Figure 14-11 The time efficiencies of the ADT list operations for three implementations, expressed in Big Oh notation Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved Design Decisions Efficiency of execution vs. implementation time Issues include: Access time Add, remove, search Memory usage Overhead for pointers Wasted memory for arrays Copyright ©2012 by Pearson Education, Inc. All rights reserved

Java Class Library: The Class LinkedList ListInterface similar to what we have defined Has more methods May use different name for method Class LinkedList Implements List, Queue, Deque Copyright ©2012 by Pearson Education, Inc. All rights reserved

Copyright ©2012 by Pearson Education, Inc. All rights reserved End Chapter 14 Copyright ©2012 by Pearson Education, Inc. All rights reserved