The Singly-Linked Structure © Rick Mercer
Store elements stored in a linked structure A linked structure provides an alternative implementation of collection classes "Linked" structures are a collection of nodes Each node stores a reference to an object and a reference to another node first references the linked structure The last element references nothing (we’ll use null) first Ryan Ali Alex
Sequential processing, not direct access like arrays Linked structures store collections sequentially Start at the beginning to find anything Each element has a successor and predecessor except the first and last element The collection of nodes has each storing a reference to some value it will be called data a reference to another node it will be called next Maintains memory on an as needed basis
Store one element that can be referenced from another // Allow elements to be stored in a linked structure private class Node { private E data; // Reference to the element private Node next; // Reference to next node public Node(){ data = null; next = null; } } // end inner class You will be able to access the instance variables data and next from any place inside a class that contains private class Node
Hard code a linked structure One node can be reached from another via references Hard code a linked structure of three nodes: // Build the first node Node first = new Node(); first.data = "Bob"; // Construct a second Node referenced // by the first Node's next first.next = new Node(); first.next.data = "Chris"; // Link a third and fourth node first.next.next = new Node("Yean"); first.next.next.next = new Node("Zorro");
A linked structure with 4 nodes You end up with four linked nodes What are the values of first.data ___ first.next.data _____ first.next.next.data ____ first.next.next.next.next ____
Traverse the linked structure Using the same linked structure, what is the output generated by this code? for(Node ref = first; ref != null; ref = ref.next) { System.out.print(ref.data.toString()); }
Node as an Inner class Other authors use the old-fashioned way to represent a node: a public class with private instance variables public getter and setter methods such as getNext, setNext, setItem, getItem Sun Microsystems added inner classes to Java The enclosing class can access the private instance variables data and next We will access these instance variable directly from the enclosing class
Construct an empty list and isEmpty public class LinkedBag<E> implements Bag<E> { private class Node { private E data; // Reference to the element private Node next; // null, or reference to next node public Node(E element) { // Different constructor data = element; next = null; } // An external reference // to the first element private Node first; // Create an empty Bag public LinkedBag() { first = null; // Explicitly assign null first Empty list reference is null
add Implement add in code to match these pictures of computer memory Consider that adding to an empty list is a special case. Do this only if empty LinkedBag<String> list = new LinkedBag<String>(); list.add("First"); first
// If the List is not empty list.add("Second"); first first first
Is there a better way? Would this work? Simpler with a 2 argument constructor public void add(E element) { first = new Node(element, first); } first