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:

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Why not just use Arrays? Java ArrayLists.
Chapter 7. Binary Search Trees
Singly linked lists Doubly linked lists
Stacks, Queues, and Linked Lists
Linked Lists Chapter 4.
DATA STRUCTURES USING C++ Chapter 5
Linked Lists Linear collections.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
1 Trees Tree nomenclature Implementation strategies Traversals –Depth-first –Breadth-first Implementing binary trees Reading: L&C 9.1 – 9.7.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Binary Search Trees Cmput Lecture 23 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Container Traversal Cmput Lecture 20 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Stacks Cmput Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
Circularly-Linked Lists Cmput Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
Sorting - Insertion Sort Cmput Lecture 11 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
Vectors Cmput Lecture 4 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from.
Code Clarity - Comments, Preconditions and Postconditions Cmput Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999.
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Dictionaries and Hash Tables Cmput Lecture 24 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture.
Chapter 3: Arrays, Linked Lists, and Recursion
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
Priority Queues Dr. David Matuszek
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Winter 2006CISC121 - Prof. McLeod1 Stuff Deadline for assn 3 extended to Monday, the 13 th. Please note that the testing class for assn 3 has changed.
Chapter 5 Linked Lists II
COM S 228 Collections and Iterators Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1 Lecture 8 b Data Structures b Abstraction b The “Structures” package b Preconditions and postconditions b Interfaces b Polymorphism b Vector class b.
CSCS-200 Data Structure and Algorithms Lecture
Collections Dwight Deugo Nesa Matic
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
CSC 243 – Java Programming, Spring, 2014 Week 4, Interfaces, Derived Classes, and Abstract Classes.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
CSCI 62 Data Structures Dr. Joshua Stough September 23, 2008.
Linked Data Structures
Pointers and Linked Lists
Pointers and Linked Lists
Trees Tree nomenclature Implementation strategies Traversals
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked Lists [AJ 15].
Data Structures & Algorithms
Recursive Objects Singly Linked Lists.
Presentation transcript:

Cmput 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

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

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

©Duane Szafron 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"

©Duane Szafron 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"

©Duane Szafron 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"

©Duane Szafron 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”

©Duane Szafron 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

©Duane Szafron 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.

©Duane Szafron 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.

©Duane Szafron 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

©Duane Szafron 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”)

©Duane Szafron 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 ( )

©Duane Szafron 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

©Duane Szafron 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

©Duane Szafron 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.

©Duane Szafron 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

©Duane Szafron 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

©Duane Szafron 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

©Duane Szafron 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

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

©Duane Szafron 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

©Duane Szafron 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”

©Duane Szafron 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 temp “Fred”“Wilma”“Barney”

©Duane Szafron 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

©Duane Szafron 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

©Duane Szafron 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

©Duane Szafron 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

©Duane Szafron 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

©Duane Szafron 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