Building Java Programs

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 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.
slides adapted from 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
A tree set Our SearchTree class is essentially a set.
slides created by Marty Stepp
Building Java Programs Chapter 14
LinkedList Class.
Building Java Programs
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:
Building Java Programs
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
ArrayLists.
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.
Building Java Programs
Building Java Programs
CSE 143 Lecture 6 References and Linked Nodes reading: 16.1
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:
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
Lecture 8: Complex Linked List Code reading: 16.2 – 16.3
Building Java Programs Chapter 16
Based on slides by Alyssa Harding & Marty Stepp
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
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 4 Implementing ArrayIntList reading:
CSE 143 Lecture 7 References and Linked Nodes reading: 16.1
slides adapted from Marty Stepp
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
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:

Building Java Programs Chapter 16 Lecture 16-2: Linked List Basics reading: 16.2

Linked node question Suppose we have a long chain of list nodes: We don't know exactly how long the chain is. How would we print the data values in all the nodes? 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 ...

Traversing a list? One (bad) way to print every value in the list: while (list != null) { System.out.println(list.data); list = list.next; // move to next node } What's wrong with this approach? (It loses the linked list as it prints it!) data next 10 data next 20 data next 990 list ...

A current reference Don't change list. Make another variable, and change it. A ListNode variable is NOT a ListNode object ListNode current = list; What happens to the picture above when we write: current = current.next; data next 10 data next 20 data next 990 list ... current

Traversing a list correctly The correct way to print every value in the list: ListNode current = list; while (current != null) { System.out.println(current.data); current = current.next; // move to next node } Changing current does not damage the list. data next 10 data next 20 data next 990 list ...

Linked List vs. Array Similar to array code: Print list values: int[] a = ...; int i = 0; while (i < a.length) { System.out.println(a[i]); i++; } Print list values: ListNode front = ...; ListNode current = front; while (current != null) { System.out.println(current.data); current = current.next; } Description Array Code Linked List Code Go to front of list int i = 0; ListNode current = list; Test for more elements i < size current != null Current value elementData[i] current.data Go to next element i++; current = current.next;

Abstract data types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed on it. Describes what a collection does, not how it does it An ADT can be implemented in multiple ways: ArrayList and LinkedList implement List LinkedList implement Queue The same external behavior can be implemented in many different ways, each with pros and cons.

A LinkedIntList class Let's write a collection 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 LinkedIntList front add(value) add(index, value) indexOf(value) remove(index) size() toString() ListNode ListNode ListNode data next 42 data next -3 data next 17 element 0 element 1 element 2

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

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 = 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 to the list. data next 20 front = front = element 0

The add method, 1st try // 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 ... }

Adding to non-empty list Before adding value 20 to end of list: After: data next 42 data next -3 front = element 0 element 1 data next 42 data next -3 front = 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 = 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);

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 = 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 = 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 the beginning front = new ListNode(value, front); } else { // inserting into the middle ListNode current = front; for (int i = 0; i < index - 1; i++) { current = current.next; } current.next = new ListNode(value, current.next);