comp 213
play

COMP 213 Advanced Object-oriented Programming Lecture 13 Linked - PowerPoint PPT Presentation

COMP 213 Advanced Object-oriented Programming Lecture 13 Linked Lists. Linked Lists The array and the linked list are the two primary building blocks for the more complex data structures. Difference: underlying layout of the data in


  1. COMP 213 Advanced Object-oriented Programming Lecture 13 Linked Lists.

  2. Linked Lists • The array and the linked list are the two primary building blocks for the more complex data structures. • Difference: underlying layout of the data in memory & way of accessing

  3. Linked Lists • The array and the linked list are the two primary building blocks for the more complex data structures. • Difference: underlying layout of the data in memory & way of accessing

  4. Linked Lists We call this small separate block of memory a “node” • The array and the linked list are the two primary building blocks for the more complex data structures. • Difference: underlying layout of the data in memory & way of accessing

  5. Linked Lists • The linked lists in the Collections library are sufficient for many applications. • We need to understand how they work, both to appreciate their limitations, and also to be able to build custom linked list classes when needed.

  6. Linked Lists • The linked lists in the Collections library are sufficient for many applications. • We need to understand how they work, both to appreciate their limitations, and also to be able to build custom linked list classes when needed.

  7. Linked Lists • The array structure allows us to access any element directly via its index. • The linked list structure seems very limited, as its nodes can only be accessed in a sequential manner. • So why bother using Linked Lists?

  8. Linked Lists • The array structure allows us to access any element directly via its index. • The linked list structure seems very limited, as its nodes can only be accessed in a sequential manner. • So why bother using Linked Lists?

  9. Why Linked Lists? • The size of an array is fixed. The size of a linked list varies. The nodes of a linked list can be allocated on an “as needed” basis. When no longer needed, the nodes can be returned to the memory manager. • For some operations a linked list is a more efficient implementation approach than an array, e.g., to place an element into the front of a collection:

  10. Why Linked Lists? • The size of an array is fixed. The size of a linked list varies. The nodes of a linked list can be allocated on an “as needed” basis. When no longer needed, the nodes can be returned to the memory manager. • For some operations a linked list is a more efficient implementation approach than an array, e.g., to place an element into the front of a collection:

  11. Why Linked Lists? • The size of an array is fixed. The size of a linked list varies. The nodes of a linked list can be allocated on an “as needed” basis. When no longer needed, the nodes can be returned to the memory manager. • For some operations a linked list is a more efficient implementation approach than an array, e.g., to place an element into the front of a collection:

  12. Creating our Linked List Class • To create a linked list we need to know how to do two things: • allocate space for a node dynamically, and • allow a node to link to, or reference, another node. • Java supplies an operation for dynamically allocating space, an operation we have been using for all of our objects — the new operation. • How can we allow a node to reference another node?

  13. Creating our Linked List Class • To create a linked list we need to know how to do two things: • allocate space for a node dynamically, and • allow a node to link to, or reference, another node. • Java supplies an operation for dynamically allocating space, an operation we have been using for all of our objects — the new operation. • How can we allow a node to reference another node?

  14. Creating our Linked List Class • To create a linked list we need to know how to do two things: • allocate space for a node dynamically, and • allow a node to link to, or reference, another node. • Java supplies an operation for dynamically allocating space, an operation we have been using for all of our objects — the new operation. • How can we allow a node to reference another node?

  15. Creating our Linked List Class • To create a linked list we need to know how to do two things: • allocate space for a node dynamically, and • allow a node to link to, or reference, another node. • Java supplies an operation for dynamically allocating space, an operation we have been using for all of our objects — the new operation. • How can we allow a node to reference another node?

  16. Self-referencing our Linked List Class • Essentially a node in a linked list is an object that holds some important information, e.g., a string, plus a link to another node. • That other node is the exact same type of object — it is also a node in the linked list. • When we define our node class, we must allow the objects created from it to reference node class objects. We call this type of class a self-referential class.

  17. Self-referencing our Linked List Class • Essentially a node in a linked list is an object that holds some important information, e.g., a string, plus a link to another node. • That other node is the exact same type of object — it is also a node in the linked list. • When we define our node class, we must allow the objects created from it to reference node class objects. We call this type of class a self-referential class.

  18. Self-referencing our Linked List Class • Essentially a node in a linked list is an object that holds some important information, e.g., a string, plus a link to another node. • That other node is the exact same type of object — it is also a node in the linked list. • When we define our node class, we must allow the objects created from it to reference node class objects. We call this type of class a self-referential class. Self-referential class: A class that includes an instance variable or variables that can hold a reference to an object of the same class.

  19. Creating our Linked List Class • We include in the class definition two instance variables: • one that holds the important information that the linked list is maintaining (e.g., a string), and • one that is a reference to an object of its same class.

  20. Example: The LLStringNode Class public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... }

  21. Example: The LLStringNode Class public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... } Instance variable info to hold a reference to the string represented by the node.

  22. Example: The LLStringNode Class public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... } Instance variable link to hold a reference to another LLStringNode object.

  23. Example: The LLStringNode Class public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... } That other LLStringNode can hold a reference to a Instance variable link to hold a reference String ( info ) and a to another LLStringNode object. reference to another LLStringNode object, …

  24. Example: The LLStringNode Class public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... } That other LLStringNode Chain ends when a can hold a reference to a Instance variable link to hold a reference LLStringNode holds the String ( info ) and a to another LLStringNode object. value null in its link. reference to another LLStringNode object, …

  25. Example of LLStringNode linked list

  26. The LLStringNode Class public class LLStringNode { ... public LLStringNode(String info) { this.info = info; link = null; } ... }

  27. The LLStringNode Class public class LLStringNode { ... public LLStringNode(String info) { this.info = info; link = null; } ... } E.g., LLStringNode node1 = new LLStringNode (“basketball”); results in: node1

  28. How to represent an empty linked list? • Declare a variable of the class LLStringNode . • Do not instantiate with the new operation. • Then, the value held in the string node variable is null : LLStringNode theList;

  29. How to represent an empty linked list? • Declare a variable of the class LLStringNode . • Do not instantiate with the new operation. • Then, the value held in the string node variable is null : LLStringNode theList;

  30. How to represent an empty linked list? • Declare a variable of the class LLStringNode . • Do not instantiate with the new operation. • Then, the value held in the string node variable is null : LLStringNode theList;

  31. The LLStringNode Class (cont.) public class LLStringNode { ... public void setInfo(String info) { this.info = info; } public String getInfo(){ return info; } public void setLink(LLStringNode link) { this.link = link; node1 } public LLStringNode getLink() { return link; } }

  32. Example LLStringNode node1 = new LLStringNode (“basketball”); LLStringNode node2 = new LLStringNode (“baseball”); node1.setLink(node2); results in:

  33. Example LLStringNode node1 = new LLStringNode (“basketball”); LLStringNode node2 = new LLStringNode (“baseball”); node1.setLink(node2); results in: node1 node2

Recommend


More recommend