COMP 213
Advanced Object-oriented Programming Lecture 13 Linked Lists.
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
Advanced Object-oriented Programming Lecture 13 Linked Lists.
more complex data structures.
more complex data structures.
more complex data structures.
We call this small separate block of memory a “node”
and also to be able to build custom linked list classes when needed.
and also to be able to build custom linked list classes when needed.
accessed in a sequential manner.
accessed in a sequential manner.
allocated on an “as needed” basis. When no longer needed, the nodes can be returned to the memory manager.
array, e.g., to place an element into the front of a collection:
allocated on an “as needed” basis. When no longer needed, the nodes can be returned to the memory manager.
array, e.g., to place an element into the front of a collection:
allocated on an “as needed” basis. When no longer needed, the nodes can be returned to the memory manager.
array, e.g., to place an element into the front of a collection:
have been using for all of our objects —the new operation.
have been using for all of our objects —the new operation.
have been using for all of our objects —the new operation.
have been using for all of our objects —the new operation.
information, e.g., a string, plus a link to another node.
linked list.
reference node class objects. We call this type of class a self-referential class.
information, e.g., a string, plus a link to another node.
linked list.
reference node class objects. We call this type of class a self-referential class.
information, e.g., a string, plus a link to another node.
linked list.
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.
string), and
public class LLStringNode { private String info; // information stored in list private LLStringNode link; // reference to a node ... }
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.
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.
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. That other LLStringNode can hold a reference to a String (info) and a reference to another LLStringNode object, …
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. That other LLStringNode can hold a reference to a String (info) and a reference to another LLStringNode object, … Chain ends when a LLStringNode holds the value null in its link.
public class LLStringNode { ... public LLStringNode(String info) { this.info = info; link = null; } ... }
public class LLStringNode { ... public LLStringNode(String info) { this.info = info; link = null; } ... }
E.g., LLStringNode node1 = new LLStringNode(“basketball”); results in:
node1
LLStringNode theList;
LLStringNode theList;
LLStringNode theList;
public class LLStringNode { ... public void setInfo(String info) { this.info = info; } public String getInfo(){ return info; } public void setLink(LLStringNode link) { this.link = link; } public LLStringNode getLink() { return link; } }
node1
LLStringNode node1 = new LLStringNode(“basketball”); LLStringNode node2 = new LLStringNode(“baseball”); node1.setLink(node2); results in:
LLStringNode node1 = new LLStringNode(“basketball”); LLStringNode node2 = new LLStringNode(“baseball”); node1.setLink(node2); results in:
node1 node2
With an array we use an index variable; that will not work here because a linked list is not indexed.
Set currNode to first node on the list while (currNode is not pointing off the end of the list) display the information at currNode change currNode to point to the next node on the list
LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) display the information at currNode change currNode to point to the next node on the list
LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) display the information at currNode change currNode to point to the next node on the list
LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) System.out.println(currNode.getInfo( )) ; change currNode to point to the next node on the list
LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) System.out.println(currNode.getInfo( )) ; change currNode to point to the next node on the list
LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) System.out.println(currNode.getInfo( )) ; currNode = currNode.getLink();
LLStringNode currNode = letters; //letters is the name of our linked list while (currNode is not pointing off the end of the list) System.out.println(currNode.getInfo( )) ; currNode = currNode.getLink();
LLStringNode currNode = letters; //letters is the name of our linked list while (currNode != null){ System.out.println(currNode.getInfo( )) ; currNode = currNode.getLink(); }
LLStringNode currNode = letters; //letters is the name of our linked list while (currNode != null){ System.out.println(currNode.getInfo( )) ; currNode = currNode.getLink(); } Does this code still work for empty linked lists? Always double-check when dealing with linked lists!
We need to consider 3 cases:
newNode.setLink(letters); letters = newNode; // the order of these lines of //code matters! What happens //if you reverse them?
newNode.setLink(letters.getLink()); letters.setLink(newNode);
LLStringNode currNode = letters; while (currNode.getLink() != null) { currNode = currNode.getLink(); } currNode.setLink(newNode);