Presentation is loading. Please wait.

Presentation is loading. Please wait.

Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.

Similar presentations


Presentation on theme: "Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."— Presentation transcript:

1 Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall

2 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Chapter Contents Removing an Item from a Linked Chain Completing the Linked Implementation of the ADT List  The Method remove  The Method replace  The Method getEntry  The Method contains  The Remaining Methods

3 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Chapter Contents A Class Node That Has Set and Get Methods Tail References  A Revised Implementation of the List The Pros and Cons of Using a Chain to Implement the ADT List Java Class Library: The Class LinkedList

4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Removing an Item from a Linked Chain Possible cases 1. Remove from beginning of the chain 2. Remove from middle of the chain 3. Remove from the end of the chain Analogy of chain of desks illustrates in following slides

5 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Removing an Item form a Linked Chain Fig. 7-1 A chain of desks just prior to removing its first desk Case 1

6 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Removing an Item form a Linked Chain Fig. 7-2 A chain of desks just after removing its first desk. Case 1

7 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Removing an Item form a Linked Chain Fig. 7-3 A chain of desks just prior to removing a desk between two other desks Case 2

8 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Removing an Item form a Linked Chain Fig. 7-4 A chain of desks just after removing a desk between two other desks Case 2

9 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Removing an Item form a Linked Chain Fig. 7-5 Before and after removing the last desk from a chain Case 3

10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Method remove Fig. 7-6 A chain of nodes (a) prior to removing first node; (b) after removing the first node

11 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X The Method remove Fig. 7-7 A chain of nodes (a) prior to removing interior node; (b) after removing interior node

12 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X View Further Methods of LList Method remove remove  Note how cases are handled Method replace replace Method getEntry getEntry Method contains contains Method isFull isFull  Always returns false in this context

13 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Using Class Node that Has Set and Get Methods View new version of class NodeView new version  An inner class  The class LList can access private data fields directly  Stylistically better to use the Set and Get methods of the class Node Thus better to use statements such as: currentNode.getData(); or desiredNode.setData(newEntry);

14 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Node with Package Access Possible to make Node accessible only within a package  Omit all access modifiers except on data fields  Add after each occurrence of Node within the class (except in constructor names) View revised listing Note required changes to class LListrequired changes

15 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tail References Consider a set of data where we repeatedly add data to the end of the list Each time the getNodeAt method must traverse the whole list  This is inefficient Solution: maintain a pointer that always keeps track of the end of the chain  The tail reference

16 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tail References Fig. 7-8 A linked chain with a head and tail reference.

17 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tail References Must change the clear method  Constructor calls it public final void clear () { firstNode = null; lastNode = null; length = 0; } // end clear

18 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tail References When adding to an empty list  Both head and tail references must point to the new solitary node When adding to a non empty list  No more need to traverse to the end of the list  lastNode points to it  Adjust lastNode.next in new node and lastNode

19 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tail References Fig. 7-9 Adding a node to the end of a nonempty chain that has a tail reference Click to view source code of new versions of method add Note highlight of use of tail references

20 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Tail References Fig. 7-10 Removing a node from a chain that has both head and tail references when the chain contains (a) one node; (b) more than one node Click to view source code of new version of method remove Note highlight of use of tail references

21 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved. 0-13-237045-X Pros and Cons of a Chain for an ADT List The chain (list) can grow as large as necessary Can add and remove nodes without shifting existing entries But … Must traverse a chain to determine where to make addition/deletion Retrieving an entry requires traversal  As opposed to direct access in an array Requires more memory for links  But does not waste memory for oversized array

22 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 LinkedList The standard java package java.util contains the class LiknkedList This class implements the interface List Contains additional methods


Download ppt "Completing the Linked Implementation of a List Chapter 7 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall."

Similar presentations


Ads by Google