Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cmput 115 - Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:

Similar presentations


Presentation on theme: "Cmput 115 - Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:"— Presentation transcript:

1 Cmput 115 - Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 2/24/00 Singly-Linked Lists

2 ©Duane Szafron 2000 2 About This Lecture In this lecture we will learn about an implementation of the List Interface called Singly Linked List.

3 ©Duane Szafron 2000 3 Outline Drawing Lists and List Nodes SinglyLinkedListElement class SinglyLinkedList class

4 ©Duane Szafron 2000 4 External Container Diagrams We can draw an external or implementation- independent diagram of a container by just showing its elements. For example, here is the diagram for a general container where the elements are not even ordered: "Fred" "Barney" "Wilma"

5 ©Duane Szafron 2000 5 Indexed Container Diagrams We can modify the diagram when the interface is more specific. For example, here is a diagram for an indexed container. Note that it still might be implemented as a vector or as an array. 012 "Fred""Barney""Wilma"

6 ©Duane Szafron 2000 6 External List Diagrams Since a List is not indexed, the elements are not numbered. However, since the elements are ordered, they must be “connected” somehow to maintain this order. Since different implementation classes “connect” the elements differently, we do not show the connections in an external diagram. "Fred""Barney""Wilma"

7 ©Duane Szafron 2000 7 Internal List Diagrams - Nodes internal structure When we want to highlight a particular class implementation of the List interface, we add internal structure. node links Each element is put in a list node and the nodes connect to each other with links. The end of the list is denoted by a dot instead of a link to another node. “Fred”“Wilma”“Barney”“Fred”“Wilma”“Barney”

8 ©Duane Szafron 2000 8 Complex List Node Diagrams If the elements of a list are complex objects, it is not always possible to draw the elements inside the node. In this case, an arrow is used in the node to represent a reference to the element. This diagram is actually more accurate since the node always contains a reference to an object instead of an object. “Fred” 8/14/1976

9 ©Duane Szafron 2000 9 Terminology - Elements and Nodes value The textbook uses the term value to refer to an object in a list. element StringsIntegersPersons In these notes we use the term element instead of value, since value could be confused with primitive value and the elements are objects like Strings, Integers or Persons, not primitive values. node element objectlinks We use the term node to refer to an object that contains an element object and links to adjacent list nodes. element The book uses the term element to refer to one of our nodes.

10 ©Duane Szafron 2000 10 Singly-Linked Lists singly-linked list “next” In a singly-linked list, each list node contains an element and a link to the “next” node in the list. self-referencing Since a node contains a link to the next node, this is an example of self-referencing of objects. nodes list We need to define two classes to implement a singly-linked list, one for the nodes and one for the list itself. SinglyLinkedListElement SinglyLinkedList In the textbook’s structure package, the list node class is called SinglyLinkedListElement and the list class is called SinglyLinkedList.

11 ©Duane Szafron 2000 11 SinglyLinkedListElement Class SinglyLinkedListElement two instance variables Each instance of SinglyLinkedListElement represents a single list node with two instance variables. data The instance variable, data, is a reference to an element object. next tail The instance variable, next, is a reference to the next node (another instance of SinglyLinkedListElement) or null if it is the last node (tail node). “Fred” 8/14/1976 “Wilma” 10/14/1979 “Barney” 3/10/1978 null

12 ©Duane Szafron 2000 12 SinglyLinkedListElement 1 SinglyLinkedListElement public class SinglyLinkedListElement data { protected Object data; next protected SinglyLinkedListElement next; SinglyLinkedListElement public SinglyLinkedListElement (Object element, SinglyLinkedListElement nextNode) { //post: initializes the node to contain the given // object and link to the given next node. this.data = element; this.next = nextNode; } SinglyLinkedListElement public SinglyLinkedListElement (Object element) { //post: initializes the node to be a tail node // containing the given value. this(element, null); } code based on Bailey pg. 106 datanext “Wilma”next SinglyLinkedListElement( “Wilma”, next) “Wilma”null SinglyLinkedListElement(“Wilma”)

13 ©Duane Szafron 2000 13 code based on Bailey pg. 107 “Fred” 8/14/1976 “Wilma” 10/14/1979 “Barney” 3/10/1978 null Last this SinglyLinkedListElement 2 next( ) public SinglyLinkedListElement next( ) { // post: returns the next node. return this.next; } setNext public void setNext (SinglyLinkedListElement next) { // post: sets the next node to be the given node this.next = next; } setNext (Last) next ( )

14 ©Duane Szafron 2000 14 SinglyLinkedListElement 2 public SinglyLinkedListElement next ( ) { // post: returns the next node. return this.next; } public void setNext (SinglyLinkedListElement next) { // post: sets the next node to be the given node this.next = next; } public Object value ( ) { // post: returns the element in this node return this.data; } public void setValue (Object anObject) { // post: sets the element in this node to the given object. this.data = anObject; } code based on Bailey pg. 107 1429

15 ©Duane Szafron 2000 15 SinglyLinkedList Class SinglyLinkedList object head Each SinglyLinkedList object has an instance variable, head, that is a reference to the first SinglyLinkedListElement object of the list. int valued count It also maintains an int valued instance variable, count, that is the size of the list. “Fred”“Wilma”“Barney” 3 headcount

16 ©Duane Szafron 2000 16 Caching the List Size size The list size could be computed by traversing the list and counting nodes. cached size( ) However, the size is cached as an instance variable so that the size( ) method can be computed faster. updated The disadvantage of caching the size as an instance variable is that the instance variable must be updated each time an element is added or removed from the list.

17 ©Duane Szafron 2000 17 List Traversal traversal Many list methods will involve traversal of the list elements from the head to the tail or from the head to a particular element or location. cursor reference We use a cursor reference for traversal. “Fred”“Wilma”“Barney”3 cursor “Fred”“Wilma”“Barney”3“Fred”“Wilma”“Barney”3“Fred”“Wilma”“Barney”3 cursor = this.head; cursor = cursor.next; cursor null cursor

18 ©Duane Szafron 2000 18 SinglyLinkedList - State and Constructor public class SinglyLinkedList implements List { protected int count; protected SinglyLinkedListElement head; public SinglyLinkedList() { //post: initializes the list to be empty. this.head = null; this.count = 0; } code based on Bailey pg. 107

19 ©Duane Szafron 2000 19 SinglyLinkedList - Store Interface /* Interface Store Methods */ public int size() { //post: returns the number of elements in the list. return this.count; } public boolean isEmpty() { // post: returns the true iff store is empty. return this.size() == 0 // why call to size()? } public void clear() { // post: clears the list so that it contains no elements. this.head = null; this.count = 0; // change constructor to call clear()? } code based on Bailey pg. 108

20 ©Duane Szafron 2000 20 SinglyLinkedList - Collection Interface 1 /* Interface Collection Methods */ contains public boolean contains (Object anObject); // pre: anObject is non-null // post: returns true iff the collection contains the // object cursor SinglyLinkedListElement cursor; cursor = this.head; // partial traversal while ((cursor != null) && (!cursor.value().equals(anObject)) cursor = cursor.next(); return cursor != null; } code based on Bailey pg. 114

21 ©Duane Szafron 2000 21 Trace of contains method “Fred”“Wilma”“Barney” 3 headcount cursor Looking for “Barney”? Yes!

22 ©Duane Szafron 2000 22 SinglyLinkedList Collection Interface 2 public void add (Object anObject) { // pre: anObject is non-null // post: the object is added to the collection. The // replacement policy is not specified this.addToHead(anObject); } public Object remove (Object anObject); // pre: anObject is non-null // post: removes object “equal” to anObject and // returns it; otherwise returns nil } public Iterator elements( ) { // post: return an iterator for traversing the collection // Ignore this one until textbook Chapter 8! } code based on Bailey pg. 108

23 ©Duane Szafron 2000 23 SinglyLinkedList - addToHead() /* Interface List Methods */ public void addToHead(Object anObject) { // pre: anObject is non-null // post: the object is added to the beginning of the list this.head = new SinglyLinkedListElement(anObject, this.head); this.count++; } code based on Bailey pg. 110 “Fred”“Wilma”“Barney” 3“Pebbles” 1 1 2 2 3 3 4

24 ©Duane Szafron 2000 24 SinglyLinkedList - removeFromHead() public Object removeFromHead() { // pre: list is not empty // post: removes and returns first object from the list SinglyLinkedListElement temp; temp = this.head; this.head = this.head.next(); this.count--; return temp.value(); } code based on Bailey pg. 110 1 temp 1 2 3 “Fred”“Wilma”“Barney” 3 2 3 2

25 ©Duane Szafron 2000 25 SinglyLinkedList - peek() and tailPeek() public Object peek ( ) { // pre: list is not empty // post: returns the first object in the list without // modifying the list return this.head.value(); } public Object tailPeek ( ) { // pre: list is not empty // post: returns the last object in the list without // modifying the list // left as an exercise } code based on Bailey pg. 111

26 ©Duane Szafron 2000 26 SinglyLinkedList - addToTail() public void addToTail (Object anObject) { // pre: anObject is non-null // post: the object is added at the end of the list SinglyLinkedListElement temp; SinglyLinkedListElement cursor; temp = new SinglyLinkedListElement(anObject, null); if (this.head == null) // list was empty this.head = temp; else { cursor = this.head; while (cursor.next() != null) cursor = cursor.next(); cursor.setNext(temp); // add at end } this.count++; } code based on Bailey pg. 113

27 ©Duane Szafron 2000 27 Removal From Tail - Problem We cannot remove from the tail by traversing to the last node and removing it. We need a reference to the second last node so we can set its “next” reference to null. “Fred”“Wilma”“Barney”3 cursor previous.setNext(null); “Fred”“Wilma”“Barney”3 cursorprevious To find the second last node, we need to traverse the list with a second cursor following the first. null

28 ©Duane Szafron 2000 28 SinglyLinkedList - removeFromTail() problem cursor = this.head; while (cursor.next() != null) { previous = cursor; cursor = cursor.next(); } previous.setNext(null); this.count--; return cursor.value(); } 1 previous “Fred”1 cursor 1 null code based on Bailey pg. 113 In this case we just want to bind this.head to null. Check the boundaries! If the list has one node, previous is unbound

29 ©Duane Szafron 2000 29 SinglyLinkedList - removeFromTail() public Object removeFromTail() { // pre: list is not empty // post: the last object in the list is removed and returned SinglyLinkedListElement cursor; SinglyLinkedListElement previous; Assert.pre(!this.isEmpty(), “List is not empty”); cursor = this.head; previous = null; while (cursor.next() != null) { previous = cursor; cursor = cursor.next(); } if (previous == null); this.head = null; else previous.setNext(null); this.count--; return cursor.value(); } code based on Bailey pg. 113

30 ©Duane Szafron 2000 30 Some Principles from the Textbook 8.When manipulating references, draw pictures. 9. Every public method of an object should leave the object in a consistent state. 10. Symmetry is good. 11. Test the boundaries of your structures and methods. principles from Bailey ch. 6


Download ppt "Cmput 115 - Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:"

Similar presentations


Ads by Google