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 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.
CSE 143 Lecture 21 Binary Search Trees, continued read slides created by Marty Stepp
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
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
Doubly Linked List Review - We are writing this code
Building Java Programs Chapter 16
Programming Abstractions
slides created by Marty Stepp
LinkedList Class.
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Linked node problem 3 What set of statements turns this picture:
Building Java Programs
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)
CSE 143 Lecture 9 References and Linked Nodes reading: 16.1
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Building Java Programs
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
CSE 143 Lecture 2 Collections and ArrayIntList
Lecture 7: Linked List Basics reading: 16.2
More Linked Lists and Loops Based on slides by Ethan Apter
Programming Abstractions
Lecture 2: Implementing ArrayIntList reading:
changing a list There are only two ways to change a linked list:
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
Lecture 8: Complex Linked List Code reading: 16.2 – 16.3
Building Java Programs Chapter 16
Based on slides by Alyssa Harding & Marty Stepp
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
CSE 143 Lecture 3 Implementing ArrayIntList reading:
slides created by Marty Stepp
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
Recall: stacks and queues
slides adapted from Marty Stepp
Building Java Programs
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
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
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
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 10 Linked List Basics reading: 16.1 - 16.2 slides created by Marty Stepp http://www.cs.washington.edu/143/

Linked node question Suppose we have a long chain of list nodes: How would we write code to print the data values in all the nodes in order without redundancy? data next 10 data next 20 data next 990 list ...

Algorithm pseudocode Start at the front of the list. While (there are more nodes to print): Print the current node's data. Go to the next node. How do we walk through the nodes of the list? list = list.next; // is this a good idea? data next 10 data next 20 data next 990 list ...

A "current" reference Important: A ListNode variable is NOT a ListNode object! ListNode current = list; Move along a list by advancing a ListNode reference. current = current.next; data next 10 data next 20 data next 990 list ... current ???

Printing algorithm Algorithm to print all list nodes: ListNode front = ...; ListNode current = front; while (current != null) { System.out.println(current.data); current = current.next; } Similar to array code: int[] a = ...; int i = 0; while (i < a.length) { System.out.println(a[i]); i++; }

A LinkedIntList class Let's write a class named LinkedIntList. Has the same methods as ArrayIntList: add, add, get, indexOf, remove, size, toString The list is internally implemented as a chain of linked nodes The LinkedIntList keeps a reference to its front as a field null is the end of the list; a null front signifies an empty list client program never directly interacts with ListNode objects - paint can analogy: LinkedIntList is a painter, the ListNodes are buckets of paint. I don't want the buckets, just do the painting for me! ListNode ListNode ListNode LinkedIntList data next 42 data next -3 front = size = 3 data next 17 element 0 element 1 element 2

LinkedIntList class v1 public class LinkedIntList { private ListNode front; private int size; public LinkedIntList() { front = null; size = 0; } methods go here LinkedIntList front = size = 0

Implementing add // Adds the given value to the end of the list. public void add(int value) { ... } How do we add a new node to the end of a list? Does it matter what the list's contents are before the add? data next 42 data next -3 front = size = 3 data next 17 element 0 element 1 element 2

Adding to an empty list Before adding 20: After: We must create a new node and attach it as the front of the list. data next 20 front = size = 0 front = size = 1 element 0

Adding to non-empty list Before adding value 20 to end of list: After: data next 42 data next -3 front = size = 2 element 0 element 1 data next 42 data next -3 front = size = 3 data next 20 element 0 element 1 element 2

Don't fall off the edge! To add/remove from a list, you must modify the next reference of the node before the place you want to change. Where should current be pointing, to add 20 at the end? What loop test will stop us at this place in the list? data next 42 data next -3 front = size = 2 element 0 element 1

The add method // Adds the given value to the end of the list. public void add(int value) { if (front == null) { // adding to an empty list front = new ListNode(value); } else { // adding to the end of an existing list ListNode current = front; while (current.next != null) { current = current.next; } current.next = new ListNode(value); size++;

Implementing get // Returns value in list at given index. public int get(int index) { ... } Exercise: Implement the get method. data next 42 data next -3 front = size = 3 data next 17 element 0 element 1 element 2

The get method // Returns value in list at given index. // Precondition: 0 <= index < size() public int get(int index) { ListNode current = front; for (int i = 0; i < index; i++) { current = current.next; } return current.data;

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 = size = 3 data next 17 element 0 element 1 element 2

The add method (2) // Inserts the given value at the given index. // Precondition: 0 <= index <= size() public void add(int index, int value) { if (index == 0) { // adding to an empty list front = new ListNode(value, front); } else { // inserting into an existing list ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = new ListNode(value, current.next); size++;

Implementing remove // Removes value at given index from list. public void remove(int index) { ... } How do we remove a node from a list? Does it matter what the list's contents are before the remove? data next 42 data next -3 front = size = 3 data next 17 element 0 element 1 element 2

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

Removing from the front Before removing element at index 0: After: data next 42 data next -3 front = size = 3 data next 20 element 0 element 1 element 2 data next -3 data next 20 front = size = 2 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 = size = 1 front = size = 0 element 0

The remove method // 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; size--;