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[]
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 17: Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Lecture7: Queue Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
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.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
DATA STRUCTURE Presented By: Mahmoud Rafeek Alfarra Using C# MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
slides adapted from Marty Stepp and Hélène Martin
Lecture 16 Stacks and Queues Richard Gesick. Sample test questions 1.Write a definition for a Node class that holds a number. 2.Write a method that sums.
The List ADT.
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
Lecture 22 Binary Search Trees Chapter 10 of textbook
slides created by Marty Stepp
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:
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Linked node problem 3 What set of statements turns this picture:
Building Java Programs
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)
Chapter 13 Collections.
slides created by Marty Stepp and Ethan Apter
Linked List (Part I) Data structure.
Building Java Programs
Building Java Programs
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Lecture 7: Linked List Basics reading: 16.2
More Linked Lists and Loops Based on slides by Ethan Apter
Programming Abstractions
changing a list There are only two ways to change a linked list:
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
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)
Building Java Programs
CSC 143 Java Linked Lists.
slides created by Marty Stepp
Recall: stacks and queues
slides adapted from Marty Stepp
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
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-3: Complex Linked List Code reading: 16.2 – 16.3

addSorted Write a method addSorted that accepts an int as a parameter and adds it 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

Recall: changing a list There are only two ways to change a linked list: Change the value of front (modify the front of the list) Change the value of <node>.next (modify middle or end of list to point somewhere else) Implications: To add in the middle, need a reference to the previous node Front is often a special case

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

Common special cases middle: "typical" case in the middle of an existing list back: special case at the back of an existing list front: special case at the front of an existing list empty: special case of an empty list

Other list features Add the following methods to the LinkedIntList: size isEmpty clear toString indexOf contains Add a size field to the list to return its size more efficiently. Add preconditions and exception tests to appropriate methods.