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[]
CSE 373 Data Structures and Algorithms Lecture 2: Queues.
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets read slides created by Marty Stepp and Hélène Martin
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.
CSE 143 Lecture 22 Binary Search Trees continued; Tree Sets read slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CSE 143 Lecture 20 Binary Search Trees read 17.3 slides created by Marty Stepp
slides adapted from Marty Stepp and Hélène Martin
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Building Java Programs Chapter 16
CSE 373 Implementing a Stack/Queue as a Linked List
CSE 143 Lecture 20 Binary Search Trees continued; Tree Sets
Stacks and Queues.
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
slides created by Marty Stepp
Stacks and Queues.
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)
Defining New Types of Objects, part 3
slides created by Marty Stepp and Ethan Apter
Building Java Programs
Building Java Programs
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Building Java Programs
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
More Linked Lists and Loops Based on slides by Ethan Apter
CSE 143 Lecture 20 Binary Search Trees, continued read 17.3
Programming Abstractions
changing a list There are only two ways to change a linked list:
Building Java Programs
Stacks and Queues CLRS, Section 10.1.
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
slides created by Marty Stepp
Binary Search Trees continued; Tree Sets
CSE 143 Lecture 4 Implementing ArrayIntList
slides created by Marty Stepp
CSE 143 Lecture 5 References and Linked Nodes
Lecture 8: Complex Linked List Code reading: 16.2 – 16.3
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)
CSE 373 Separate chaining; hash codes; hash maps
Building Java Programs
Suggested self-checks: Section 7.11 #1-11
CSC 143 Java Linked Lists.
slides adapted from Marty Stepp
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
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
Presentation transcript:

slides created by Marty Stepp CSE 143 Lecture 7 More Linked Lists 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.

IMPORTANT this changes the starting point of the list There are only two ways to change the structure of a linked list: change the value of front this changes the starting point of the list example: front = null; change the value of <something>.next, where <something> is a temporary variable that refers to a node in the list this changes a link in the list example: current.next = null;

Implementing remove // Removes and returns the list's first value. public int remove() { ... } How do we remove the front node from a list? Does it matter what the list's contents are before the remove?

Removing front element Before removing front element: After first removal: After second removal: data next 42 data next 20 front = element 0 element 1 data next 20 front = front = element 0

remove solution // Removes and returns the first value. // Throws a NoSuchElementException on empty list. public int remove() { if (front == null) { throw new NoSuchElementException(); } else { int result = front.data; front = front.next; return result; }

Implementing remove (2) // Removes value at given index from list. // Precondition: 0 <= index < size public void remove(int index) { ... } How do we remove any node in general from a list? Does it matter what the list's contents are before the remove?

Removing from a list Before removing element at index 1: After: data next 42 data next -3 front = data next 20 element 0 element 1 element 2 data next 42 data next 20 front = element 0 element 1

Removing from the front Before removing element at index 0: After: data next 42 data next -3 front = data next 20 element 0 element 1 element 2 data next -3 data next 20 front = element 0 element 1

Removing the only element Before: After: We must change the front field to store null instead of a node. Do we need a special case to handle this? data next 20 front = front = element 0

remove (2) solution // Removes value at given index from list. // Precondition: 0 <= index < size() public void remove(int index) { if (index == 0) { // special case: removing first element front = front.next; } else { // removing from elsewhere in the list ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = current.next.next;

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 = 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

Key idea: peeking ahead Corrected version of the loop: ListNode current = front; while (current.next.data < value) { current = current.next; } This time the loop stops in the right place. 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

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 = 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

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; } current.next = new ListNode(value, 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 =

Final version of code // Adds given value to list in sorted order. // Precondition: Existing elements are 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; } current.next = new ListNode(value, current.next);

Exercise Write a method reverse that reverses the order of the elements in the list. For example, if the list initially stores this sequence of integers: [1, 8, 19, 4, 17] It should store the following sequence of integers after reverse is called: [17, 4, 19, 8, 1]

Solution public void reverse() { ListNode current = front; ListNode previous = null; while (current != null) { ListNode nextNode = current.next; current.next = previous; previous = current; current = nextNode; } front = previous;