Presentation is loading. Please wait.

Presentation is loading. Please wait.

Single-Linked Lists.

Similar presentations


Presentation on theme: "Single-Linked Lists."— Presentation transcript:

1 Single-Linked Lists

2 Two Choices to Implement
Implement a linked data structure manually inside an enclosing ADT – e.g. your queue class has an internal node class and your enqueue / dequeue methods take care of manipulating the internal linked list. Use a pre-made LinkedList class. This is what the following slides describe how to create. Instead of #1, you actually create a private linked list object as an instance variable. It may have more methods than you need to create your queue/stack/whatever. Your methods then delegate to the methods that come with your linked list instead of you implementing those linked list operations manually.

3 Single-Linked Lists A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and remove methods operate in linear time—requiring a loop to shift elements A linked list can add and remove elements at a known location in constant time In a linked list, instead of an index, each element is linked to the following element

4 A List Node A node can contain: A link is a reference to a list node
a data item one or more links A link is a reference to a list node In our structure, the node contains a data field named data of type E and a reference to the next node, named next

5 List Nodes for Single-Linked Lists
private static class Node<E> { private E data; private Node<E> next; /** Creates a new node with a null next dataItem The data stored */ private Node(E dataItem) { data = dataItem; next = null; } /** Creates a new node that references another nodeRef The node referenced by new node private Node(E dataItem, Node<E> nodeRef) { next = nodeRef;

6 List Nodes for Single-Linked Lists (cont.)
private static class Node<E> { private E data; private Node<E> next; /** Creates a new node with a null next dataItem The data stored */ private Node(E dataItem) { data = dataItem; next = null; } /** Creates a new node that references another nodeRef The node referenced by new node private Node(E dataItem, Node<E> nodeRef) { next = nodeRef; The keyword static indicates that the Node<E> class will not reference its outer class Static inner classes are also called nested classes

7 List Nodes for Single-Linked Lists (cont.)
private static class Node<E> { private E data; private Node<E> next; /** Creates a new node with a null next dataItem The data stored */ private Node(E dataItem) { data = dataItem; next = null; } /** Creates a new node that references another nodeRef The node referenced by new node private Node(E dataItem, Node<E> nodeRef) { next = nodeRef; Generally, all details of the Node class should be private. This applies also to the data fields and constructors. But nested class variables are visible to the enclosing class.

8 Connecting Nodes value= "Dave"

9 Connecting Nodes (cont.)
Node<String> tom = new Node<String>("Tom"); Node<String> dave = new Node<String>("Dave"); Node<String> harry = new Node<String>("Harry"); Node<String> sam = new Node<String>("Sam"); tom.next = dave; dave.next = harry; harry.next = sam;

10 A Single-Linked List Class
Generally, we do not have individual references to each node. A SingleLinkedList object has a data field head, the list head, which references the first list node (sometimes called first or front) public class SingleLinkedList<E> { private Node<E> head = null; private int size = 0; ... }

11 API for SingleLinkedList
These public methods help us implement the List interface in Java. We will use private helper methods (carefully) behind the scenes. Note that we may not always want to implement the List interface. (Why?)

12 SLList: An Example List
head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String>

13 Implementing addFirst(E item)
head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String> next = data = "Ann" Node<String> The element added to the list

14 Write the code: addFirst(E item)
private void addFirst (E item) { }

15 Implementing addFirst(E item) (cont.)
private void addFirst (E item) { Node<E> temp = new Node<E>(item, head); head = temp; size++; } or, more simply ... head = new Node<E>(item, head); This works even if head is null

16 Implementing addAfter(Node<E> node, E item)
head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String> next = data = "Ann" Node<String> The element added to the list

17 Write the code: addAfter(Node<E> node, E item)
private void addAfter (Node<E> node, E item) { }

18 Implementing addAfter(Node<E> node, E item) (cont.)
private void addAfter (Node<E> node, E item) { Node<E> temp = new Node<E>(item, node.next); node.next = temp; size++; } or, more simply ... node.next = new Node<E>(item, node.next); We declare this method private since it should not be called from outside the class. Later we will see how this method is used to implement the public add methods.

19 Implementing removeAfter(Node<E> node)
temp head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String> next = data = "Ann" Node<String> The Node parameter

20 Implementing removeAfter(Node<E> node) (cont.)
private E removeAfter (Node<E> node) { }

21 Implementing removeAfter(Node<E> node) (cont.)
private E removeAfter (Node<E> node) { Node<E> temp = node.next; if (temp != null) { node.next = temp.next; size--; return temp.data; } else { return null; }

22 Implementing removeFirst()
head = SLList<String> next = data = "Dave" Node<String> next = data = "Tom" Node<String> temp

23 Implementing removeFirst() (cont.)
private E removeFirst () { }

24 Implementing removeFirst() (cont.)
private E removeFirst () { Node<E> temp = head; if (head != null) { head = head.next; } if (temp != null) { size--; return temp.data } else { return null;

25 Traversing a Single-Linked List
head = SLList<String> next = data = "Tom" Node<String> next = data = "Dave" Node<String> Do something with nodeRef Do something with nodeRef Do something with nodeRef next = data = "Ann" null Node<String> nodeRef

26 Traversing a Single-Linked List (cont.)
toString()can be implemented with a traversal: public String toString() { }

27 Traversing a Single-Linked List (cont.)
toString()can be implemented with a traversal: public String toString() { Node<String> nodeRef = head; StringBuilder result = new StringBuilder(); while (nodeRef != null) { result.append(nodeRef.data); if (nodeRef.next != null) { result.append(" ==> "); } nodeRef = nodeRef.next; return result.toString();

28 getNode(int) In order to implement methods required by the List interface, we need an additional helper method: private Node<E> getNode(int index) { }

29 getNode(int) In order to implement methods required by the List interface, we need an additional helper method: private Node<E> getNode(int index) { Node<E> node = head; for (int i=0; i<index && node != null; i++) { node = node.next; } return node;

30 Completing the SingleLinkedList Class

31 Implement These Methods
public E get(int index) public E set(int index, E newValue) public void add(int index, E item) public boolean add(E item) USE getNode AND OTHER HELPER METHODS!

32 public E get(int index)
}

33 public E get(int index)
if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node<E> node = getNode(index); return node.data;

34 public E set(int index, E newValue)
public E set (int index, E anEntry) { }

35 public E set(int index, E newValue)
public E set (int index, E anEntry) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } Node<E> node = getNode(index); E result = node.data; node.data = anEntry; return result;

36 public void add(int index, E item)

37 public void add(int index, E item)
public void add (int index, E item) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException(Integer.toString(index)); } if (index == 0) { addFirst(item); } else { Node<E> node = getNode(index-1); addAfter(node, item);

38 public boolean add(E item)
To add an item to the end of the list public boolean add (E item) { }

39 public boolean add(E item)
To add an item to the end of the list public boolean add (E item) { add(size, item); return true; }


Download ppt "Single-Linked Lists."

Similar presentations


Ads by Google