Chapter 4 Linked Structures
Chapter Objectives <ul><li>Describe the use of references to create linked structures </li></ul><ul><li>Compare linked str...
References as Links <ul><li>There are many ways to implement a collection </li></ul><ul><li>In chapter 3 we explored an ar...
FIGURE 4.1   An object reference variable pointing to an object
Self-Referential Objects <ul><li>A  Person  object, for instance, could contain a reference variable to another  Person  o...
Linked Lists <ul><li>This type of reference can be used to form a  linked list , in which one object refers to the next, w...
FIGURE 4.2   A linked list
Non-linear Structures <ul><li>A linked list, as the name implies, is a linear structure </li></ul><ul><li>Object reference...
FIGURE 4.3   A complex linked structure
Managing Linked Lists <ul><li>The references in a linked list must be carefully managed to maintain the integrity of the s...
FIGURE 4.4   Inserting a node at the front of a linked list
FIGURE 4.5   Inserting a node in the middle of a linked list
FIGURE 4.6   Deleting the first node in a linked list
FIGURE 4.7   Deleting an interior node from a linked list
Elements without Links <ul><li>The problem with self-referential objects is that they &quot;know&quot; they are part of a ...
FIGURE 4.8   Using separate node objects to store and link elements
Doubly Linked Lists <ul><li>There are variations on the implementation of linked lists that may be useful in particular si...
FIGURE 4.9   A doubly linked list
Listing 4.1
Listing 4.1   (cont.)
Listing 4.1   (cont.)
FIGURE 4.10   A linked implementation of a set collection
LinkedSet - Constructor
LinkedSet - the add Operation
LinkedSet - the removeRandom Operation
LinkedSet - the removeRandom Operation
LinkedSet - the remove Operation
LinkedSet - the remove Operation
LinkedSet - the remove Operation
LinkedSet - the iterator Operation
Listing 4.2
Listing 4.2   (cont.)
Listing 4.2   (cont.)
FIGURE 4.11   UML description of the LinkedSet<T> class
Analysis of Linked Operations <ul><li>Since the order is irrelevant, and there is no capacity to expand, adding an element...
Upcoming SlideShare
Loading in …5
×

Ch04 Linked List Structure

2,195 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,195
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
56
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Ch04 Linked List Structure

  1. 1. Chapter 4 Linked Structures
  2. 2. Chapter Objectives <ul><li>Describe the use of references to create linked structures </li></ul><ul><li>Compare linked structures to array-based structures </li></ul><ul><li>Explore the techniques for managing a linked list </li></ul><ul><li>Discuss the need for a separate node to form linked structures </li></ul><ul><li>Implement a set collection using a linked list </li></ul>
  3. 3. References as Links <ul><li>There are many ways to implement a collection </li></ul><ul><li>In chapter 3 we explored an array-based implementation of a set collection </li></ul><ul><li>A linked structure uses object reference variables to link one object to another </li></ul><ul><li>Recall that an object reference variable stores the address of an object </li></ul><ul><li>In that sense, an object reference is a pointer to an object </li></ul>
  4. 4. FIGURE 4.1 An object reference variable pointing to an object
  5. 5. Self-Referential Objects <ul><li>A Person object, for instance, could contain a reference variable to another Person object: </li></ul>public class Person { private String name; private String address; private Person next; // a link to another Person object // whatever else }
  6. 6. Linked Lists <ul><li>This type of reference can be used to form a linked list , in which one object refers to the next, which refers to the next, etc. </li></ul><ul><li>Each object in a list is often generically called a node </li></ul><ul><li>A linked list is a dynamic data structure in that its size grows and shrinks as needed, unlike an array, whose size is fixed </li></ul><ul><li>Java objects are created dynamically when they are instantiated </li></ul>
  7. 7. FIGURE 4.2 A linked list
  8. 8. Non-linear Structures <ul><li>A linked list, as the name implies, is a linear structure </li></ul><ul><li>Object references also allow us to create non-linear structures such as hierarchies and graphs </li></ul>
  9. 9. FIGURE 4.3 A complex linked structure
  10. 10. Managing Linked Lists <ul><li>The references in a linked list must be carefully managed to maintain the integrity of the structure </li></ul><ul><li>Special care must be taken to ensure that the entry point into the list is maintained properly </li></ul><ul><li>The order in which certain steps are taken is important </li></ul><ul><li>Consider inserting and deleting nodes in various positions within the list </li></ul>
  11. 11. FIGURE 4.4 Inserting a node at the front of a linked list
  12. 12. FIGURE 4.5 Inserting a node in the middle of a linked list
  13. 13. FIGURE 4.6 Deleting the first node in a linked list
  14. 14. FIGURE 4.7 Deleting an interior node from a linked list
  15. 15. Elements without Links <ul><li>The problem with self-referential objects is that they &quot;know&quot; they are part of a list </li></ul><ul><li>A better approach is to manage a separate list of nodes that also reference the objects stored in the list </li></ul><ul><li>The list is still managed using the same techniques </li></ul><ul><li>The objects stored in the list need no special implementation to be part of the list </li></ul><ul><li>A generic list collection can be used to store any kind of object </li></ul>
  16. 16. FIGURE 4.8 Using separate node objects to store and link elements
  17. 17. Doubly Linked Lists <ul><li>There are variations on the implementation of linked lists that may be useful in particular situations </li></ul><ul><li>For example, in a doubly linked list each node has a reference to both the next and previous nodes in the list </li></ul><ul><li>This makes traversing the list easier </li></ul>
  18. 18. FIGURE 4.9 A doubly linked list
  19. 19. Listing 4.1
  20. 20. Listing 4.1 (cont.)
  21. 21. Listing 4.1 (cont.)
  22. 22. FIGURE 4.10 A linked implementation of a set collection
  23. 23. LinkedSet - Constructor
  24. 24. LinkedSet - the add Operation
  25. 25. LinkedSet - the removeRandom Operation
  26. 26. LinkedSet - the removeRandom Operation
  27. 27. LinkedSet - the remove Operation
  28. 28. LinkedSet - the remove Operation
  29. 29. LinkedSet - the remove Operation
  30. 30. LinkedSet - the iterator Operation
  31. 31. Listing 4.2
  32. 32. Listing 4.2 (cont.)
  33. 33. Listing 4.2 (cont.)
  34. 34. FIGURE 4.11 UML description of the LinkedSet<T> class
  35. 35. Analysis of Linked Operations <ul><li>Since the order is irrelevant, and there is no capacity to expand, adding an element to the set is O(1) </li></ul><ul><li>Removing a particular element, because it must be found, is O(n) </li></ul><ul><li>Removing a random element requires a traversal of the list, and therefore is O(n) </li></ul>

×