slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Advertisements

Building Java Programs Chapter 16 Linked Lists. 2 A swap method? Does the following swap method work? Why or why not? public static void main(String[]
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4 slides created by Marty Stepp
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs Interfaces reading: , 16.4.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Building Java Programs Chapter 16
CSE 373 Implementing a Stack/Queue as a Linked List
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
Building Java Programs
Building Java Programs Chapter 16
Programming Abstractions
A tree set Our SearchTree class is essentially a set.
slides created by Marty Stepp
Linked node problem 3 What set of statements turns this picture:
Linked node problem 3 What set of statements turns this picture:
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
slides created by Marty Stepp and Ethan Apter
slides created by Ethan Apter
Building Java Programs
Building Java Programs
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
A tree set Our SearchTree class is essentially a set.
Lecture 7: Linked List Basics reading: 16.2
EE 422C Sets.
More Linked Lists and Loops Based on slides by Ethan Apter
Programming Abstractions
Building Java Programs
slides created by Ethan Apter
slides adapted from Marty Stepp and Hélène Martin
slides created by Marty Stepp and Hélène Martin
Building Java Programs
Building Java Programs
CSE 143 Lecture 4 Implementing ArrayIntList
slides created by Marty Stepp
CSE 143 Lecture 5 References and Linked Nodes
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Lecture 8: Complex Linked List Code reading: 16.2 – 16.3
Building Java Programs Chapter 16
Based on slides by Alyssa Harding & Marty Stepp
CSE 143 Lecture 25 Advanced collection classes
LinkedIntList(int n) Write a constructor for LinkedIntList that accepts an int n parameter and makes a list of the number from 0 to n new LinkedIntList(3)
CSE 373 Separate chaining; hash codes; hash maps
Building Java Programs
CSC 143 Java Linked Lists.
slides created by Marty Stepp
slides created by Alyssa Harding
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
Building Java Programs
slides adapted from Marty Stepp
Building Java Programs
slides created by Marty Stepp
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 4 Implementing ArrayIntList reading:
slides adapted from Marty Stepp
slides created by Ethan Apter and Marty Stepp
slides created by Marty Stepp
Linked Lists and Loops based on slides by Ethan Apter
CSE 143 Lecture 21 Advanced List Implementation
slides created by Ethan Apter
CSE 373 Set implementation; intro to hashing
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

slides created by Marty Stepp CSE 143 Lecture 11 More Linked Lists reading: 16.2 - 16.3 slides created by Marty Stepp http://www.cs.washington.edu/143/

Conceptual questions What is the difference between a LinkedIntList and a ListNode? What is the difference between an empty list and a null list? How do you create each one? Why are the fields of ListNode public? Is this bad style? What effect does this code have on a LinkedIntList? ListNode current = front; current = null;

Conceptual answers A list consists of 0 to many node objects. Each node holds a single data element value. null list: LinkedIntList list = null; empty list: LinkedIntList list = new LinkedIntList(); It's okay that the node fields are public, because client code never directly interacts with ListNode objects. The code doesn't change the list. You can change a list only in one of the following two ways: Modify its front field value. Modify the next reference of a node in the list.

Linked vs. array lists We have implemented the following two collection classes: ArrayIntList LinkedIntList They have similar behavior. We should be able to treat them the same way in client code. index 1 2 3 value 42 -3 17 9 data next 42 data next -3 data next 17 data next 9 front

An IntList interface // Represents a list of integers. public interface IntList { public void add(int value); public void add(int index, int value); public int get(int index); public int indexOf(int value); public boolean isEmpty(); public void remove(int index); public void set(int index, int value); public int size(); } public class ArrayIntList implements IntList { ... public class LinkedIntList implements IntList { ...

Exercise Write a method addSorted that accepts an integer value as a parameter and adds that value to a sorted list in sorted order. Before addSorted(17) : After addSorted(17) : front = size = 3 data next -4 data next 8 data next 22 element 0 element 1 element 2 front = size = 3 data next -4 data next 8 data next 17 data next 22 element 0 element 1 element 2 element 3

The common case Adding to the middle of a list: addSorted(17) Which references must be changed? What sort of loop do we need? When should the loop stop? front = size = 3 data next -4 data next 8 data next 22 element 0 element 1 element 2 you knew the answer right away, it goes between the 5 and 12 when you see the 5, do you know to stop yet? when do you know to stop? while (current.data < value) { current = current.next; } should it be < or <= ? when would it matter? what if the list had 500 10s? < is more efficient problem: we have gone too far. we want to insert the 10 in *front* of the node we are at. We want to stop 1 before the 12. (James Bond on train car) Key idea: insert BETWEEN 5 and 12. in order to change the link between the 5 and 12, we must be at the node with 5, so we can change its .next link.

First attempt An incorrect loop: What is wrong with this code? ListNode current = front; while (current.data < value) { current = current.next; } What is wrong with this code? The loop stops too late to affect the list in the right way. current front = size = 3 data next -4 data next 8 data next 22 element 0 element 1 element 2

Key idea: peeking ahead An incorrect loop: ListNode current = front; while (current.next.data < value) { current = current.next; } This time the loop stops in the right place. current front = size = 3 data next -4 data next 8 data next 22 element 0 element 1 element 2

Another case to handle Adding to the end of a list: addSorted(42) Exception in thread "main": java.lang.NullPointerException Why does our code crash? What can we change to fix this case? front = size = 3 data next -4 data next 8 data next 22 element 0 element 1 element 2

Multiple loop tests A correction to our loop: ListNode current = front; while (current.next != null && current.next.data < value) { current = current.next; } We must check for a next of null before we check its .data. current front = size = 3 data next -4 data next 8 data next 22 element 0 element 1 element 2

Third case to handle Adding to the front of a list: addSorted(-10) What will our code do in this case? What can we change to fix it? front = size = 3 data next -4 data next 8 data next 22 element 0 element 1 element 2

Handling the front Another correction to our code: if (value <= front.data) { // insert at front of list front = new ListNode(value, front); } else { // insert in middle of list ListNode current = front; while (current.next != null && current.next.data < value) { current = current.next; } Does our code now handle every possible case?

Fourth case to handle Adding to (the front of) an empty list: addSorted(42) What will our code do in this case? What can we change to fix it? front = size = 3

Final version of code // Adds given value to list in sorted order. // Precondition: Existing list is sorted public void addSorted(int value) { if (front == null || value <= front.data) { // insert at front of list front = new ListNode(value, front); } else { // insert in middle of list ListNode current = front; while (current.next != null && current.next.data < value) { current = current.next; }