CSE 143. Lecture 7: Linked List Basics reading: 16.2

Similar documents
Linked Lists. References and objects

CSE 143X. Accelerated Computer Programming I/II

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, ,

Building Java Programs

Today s Goals 2. Get familiar with the idea of references (things that point to objects) So far, the only real data structure we ve seen is arrays

Building Java Programs

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

Array Based Lists. Collections

CSE 143 Lecture 2. reading:

CSE143 Midterm Summer Name of Student: Section (e.g., AA): Student Number:

Building Java Programs

CSE 143 Lecture 14. Interfaces; Abstract Data Types (ADTs) reading: 9.5, 11.1; 16.4

CSE 143 SAMPLE MIDTERM

Linked Lists. Chapter 12.3 in Savitch

Building Java Programs

Building Java Programs

Using arrays to store data

CSE 143 Lecture 4. ArrayList. Reading: slides created by Marty Stepp

CSE 143. Lecture 13: Interfaces, Comparable reading: , 16.4, 10.2

CSE 143 SAMPLE MIDTERM SOLUTION

CSE 143. Computer Programming II

Building Java Programs. Interfaces and Comparable reading: , 10.2, 16.4

CSE 143 Lecture 20. Circle

1. (9 points) Recall the definition of the ListNode class: public class ListNode { int data; ListNode next; }

Building Java Programs

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes.

CSE143 Notes for Monday, 7/06/15

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

Intro to Programming II

CSE 143, Winter 2009 Sample Midterm Exam #2

COMPUTER SCIENCE DEPARTMENT PICNIC. Operations. Push the power button and hold. Once the light begins blinking, enter the room code

CompSci 201, LinkedLists

CSE 143 Lecture 4. Preconditions

CSE 143 Sample Midterm Exam #4

CSE 373. Java Collection Framework. reading: Weiss Ch. 3, 4.8. slides created by Marty Stepp

CS 171: Introduction to Computer Science II. Linked List. Li Xiong

Building Java Programs

CSE 373 Data Structures and Algorithms. Lecture 2: Queues

Building Java Programs

CSE 143 Sample Midterm Exam #1

Lecture 9. Pointer, linked node, linked list TDDD86: DALP. Content. Contents Pointer and linked node. 1.1 Introduction. 1.

16. Dynamic Data Structures

Linked List. ape hen dog cat fox. tail. head. count 5

Programming II (CS300)

CSC 321: Data Structures. Fall 2013

EXAMINATIONS 2016 TRIMESTER 2

A List Implementation That Links Data

CSC 321: Data Structures. Fall 2012

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CS 106X, Lecture 16 More Linked Lists

CSE 143 Section Handout #13 Practice Midterm #5

Review: List Implementations. CSE 143 Java. Links. A Different Strategy: Lists via Links. Linked Links. CSE143 Au List

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

ITI Introduction to Computing II

Programming II (CS300)

List ADT. B/W Confirming Pages

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

Building Java Programs

Java Review: Objects

Computer Science 62. Midterm Examination

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Data abstractions: ADTs Invariants, Abstraction function. Lecture 4: OOP, autumn 2003

Last time s big ideas

CSC 1052 Algorithms & Data Structures II: Lists

CSE 143 Sample Final Exam #5

Announcements. Container structures so far. IntSet ADT interface. Sets. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am

CSE 143 Sample Midterm Exam #7 (11wi)

Facebook. / \ / \ / \ Accenture Nintendo

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

Agenda. Inner classes and implementation of ArrayList Nested classes and inner classes The AbstractCollection class Implementation of ArrayList

11-1. Collections. CSE 143 Java. Java 2 Collection Interfaces. Goals for Next Several Lectures

CSE 143, Winter 2010 Midterm Exam Wednesday February 17, 2010

Winter 2016 COMP-250: Introduction to Computer Science. Lecture 6, January 28, 2016

Linked Lists. Linked List Nodes. Walls and Mirrors Chapter 5 10/25/12. A linked list is a collection of Nodes: item next -3.

Advanced Java Concepts Unit 2: Linked Lists.

Building Java Programs

Linked Lists. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

A Linked Structure. Chapter 17

CSE 143 Sample Final Exam #3

CSE 143 Sample Final Exam #1

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Introduction to Computer Science II CS S-18 Linked Lists

CSE 143, Winter 2010 Final Exam Thursday, March 18, 2010

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Stacks Fall 2018 Margaret Reid-Miller

NAME: c. (true or false) The median is always stored at the root of a binary search tree.

Java Magistère BFA

Linked Lists. Linked List vs Array vs Python List Singly Linked List Bag ADT Revisited Building Linked Lists

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

ArrayLists. Chapter 12.1 in Savitch

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

CSE 143. Lecture 28: Hashing

EXAMINATIONS 2017 TRIMESTER 2

Parsing JSON, Using Libraries, Java Collections, Generics. Slides adapted from Craig Zilles

Introduction Data Structures

Announcements. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am

Taking Stock. IE170: Algorithms in Systems Engineering: Lecture 7. (A subset of) the Collections Interface. The Java Collections Interfaces

Transcription:

CSE 143 Lecture 7: Linked List Basics reading: 16.2

References vs. objects variable = value; a variable (left side of = ) is an arrow (the base of an arrow) a value (right side of = ) is an object (a box; what an arrow points at) For the list at right: a.next = value; means to adjust where points 1 a 10 1 2 20 variable = a.next; means to make variable point at 2 2

Reassigning references when you say: a.next = b.next; you are saying: "Make variable a.next refer to the same value as b.next." Or, "Make a.next point to the same place that b.next points." a b 10 30 20 40 3

Linked node question Suppose we have a long chain of list nodes: list 10 20... 990 We don't know exactly how long the chain is. How would we print the data values in all the nodes? 4

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? list 10 20... 990 5

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!) list 10 20... 990 6

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

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. list 10 20... 990 8

Linked List vs. Array Print list values: ListNode list=...; Similar to array code: int[] a =...; ListNode current = list; while (current!= null) { System.out.println(current.data); current = current.next; int i = 0; while (i < a.length) { System.out.println(a[i]); i++; 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; 9

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 Java's collection framework describes several ADTs: Queue, List, Collection, Deque, List, Map, Set An ADT can be implemented in multiple ways: ArrayList and LinkedList implement List HashSet and TreeSet LinkedList, ArrayDeque, etc. implement Set implement Queue The same external behavior can be implemented in many different ways, each with pros and cons. 10

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 42-3 17 element 0 element 1 element 2 11

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

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

Adding to an empty list Before adding 20: After: front = front = 20 element 0 We must create a new node and attach it to the list. 14

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... 15

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

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

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); 18

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

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; 20

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

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); 22