Lecture 8: Complex Linked List Code reading: 16.2 – 16.3

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 Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh) reading: 9.5, 11.1, slides created by Marty Stepp
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2
The List ADT.
Building Java Programs Chapter 16
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
Doubly Linked List Review - We are writing this code
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:
Prof. Neary Adapted from slides by Dr. Katherine Gibson
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
Building Java Programs
CSE 143 Lecture 6 Interfaces; Complexity (Big-Oh)
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
More Linked Lists and Loops Based on slides by Ethan Apter
Lecture 5: complexity reading:
Programming Abstractions
changing a list There are only two ways to change a linked list:
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Building Java Programs
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
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)
Building Java Programs
CSC 143 Java Linked Lists.
CSE 373 Implementing a Stack Reading: Weiss Ch. 3; 3.6; 1.5
slides created by Marty Stepp
Generics, Stack, Queue Based on slides by Alyssa Harding
slides adapted from Marty Stepp
Building Java Programs
slides created by Marty Stepp
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 6 interfaces; eclipse; testing
slides adapted from 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
slides created by Marty Stepp
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Data Structures & Programming
Presentation transcript:

Lecture 8: Complex Linked List Code reading: 16.2 – 16.3 CSE 143 Lecture 8: Complex Linked List Code reading: 16.2 – 16.3 In some languages (C++), -> is used for dereferencing

Implementing add (2) // Inserts the given value at the given index. public void add(int index, int value) { ... } Exercise: Implement the two-parameter add method. data next 42 data next -3 front = data next 17 element 0 element 1 element 2

addSorted Write a method addSorted that accepts an int as a parameter and adds it to a sorted list in sorted order. Before addSorted(17) : After addSorted(17) : front = data next -4 data next 8 data next 22 element 0 element 1 element 2 front = 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 = data next -4 data next 8 data next 22 element 0 element 1 element 2

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 = 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 = 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 = data next -4 data next 8 data next 22 element 0 element 1 element 2

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 =

Common cases middle: "typical" case in the middle of an existing list back: special case at the back of an existing list front: special case at the front of an existing list empty: special case of an empty list

Other list features Add the following methods to the LinkedIntList: size isEmpty clear toString indexOf contains remove Add preconditions and exception tests to appropriate methods.

Interfaces interface: A list of methods that a class can promise to implement. Inheritance gives you an is-a relationship and code sharing. A Lawyer can be treated as an Employee and inherits its code. Interfaces give you an is-a relationship without code sharing. A Rectangle object can be treated as a Shape but inherits no code. Always declare variables using the interface type. List<String> list = new ArrayList<String>();