CSE 143 Lecture 10 Linked List Basics reading

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: list data next 10 data next 20 . . . 990 • How would we write code to print the data values in all the nodes in order without redundancy? 2

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; list data next 10 // is this a good idea? data next 20 . . . 990 3

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

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

A Linked. Int. List class • Let's write a class named Linked. Int. List. – Has the same methods as Array. Int. List: • add, get, index. Of, remove, size, to. String – The list is internally implemented as a chain of linked nodes • The Linked. Int. List keeps a reference to its front as a field • null is the end of the list; a null front signifies an empty list Linked. Int. List. Node data next front = 42 -3 17 size element 0 element 1 element 2 = 3 6

Linked. Int. List class v 1 public class Linked. Int. List { private List. Node front; private int size; public Linked. Int. List() { front = null; size = 0; } Linked. Int. List front = size = 0 methods go here } 7

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 front = 42 -3 17 size element 0 element 1 element 2 = 3 8

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

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

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. data next front = 42 -3 size element 0 element 1 = 2 – Where should current be pointing, to add 20 at the end? – What loop test will stop us at this place in the list? 11

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 List. Node(value); } else { // adding to the end of an existing list List. Node current = front; while (current. next != null) { current = current. next; } current. next = new List. Node(value); } size++; } 12

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

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

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 front = 42 -3 17 size element 0 element 1 element 2 = 3 15

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 List. Node(value, front); } else { // inserting into an existing list List. Node current = front; for (int i = 0; i < index - 1; i++) { current = current. next; } current. next = new List. Node(value, current. next); } size++; } 16

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

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

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

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

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 List. Node current = front; for (int i = 0; i < index - 1; i++) { current = current. next; } current. next = current. next; } size--; } 21
- Slides: 21