More Linked Lists and Loops Based on slides by Ethan Apter

Slides:



Advertisements
Similar presentations
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Advertisements

CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 143 Lecture 6 Linked Lists slides created by Ethan Apter
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
CSE 143 Lecture 10 Linked List Basics reading: slides created by Marty Stepp
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.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
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
Review Linked List Insertion Description Deletion Description Basic Node Implementation Conclusion 1.
Discussion 4 eecs 183 Hannah Westra.
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
Doubly Linked List Review - We are writing this code
Building Java Programs Chapter 16
Programming Abstractions
slides created by Marty Stepp
Wednesday Questions How do I debug? We love to teach this at IPL/OH!
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
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:
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)
slides created by Marty Stepp and Ethan Apter
slides created by Ethan Apter
Building Java Programs
Building Java Programs
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Lecture 7: Linked List Basics reading: 16.2
Programming Abstractions
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
slides created by Ethan Apter
Introduction to C++ Linear Linked Lists
Insertion sort.
CSE 143 Lecture 5 References and Linked Nodes
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)
CSE 373 Separate chaining; hash codes; hash maps
Building Java Programs
CSC 143 Java Linked Lists.
CSC 143 Binary Search Trees.
slides created by Marty Stepp
Linked Lists based on slides created by Ethan Apter
slides created by Ethan Apter
slides adapted from Marty Stepp
Building Java Programs
Lecture 7: Linked List Basics reading: 16.2
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!
slides created by Marty Stepp
Linked Lists and Loops based on slides by Ethan Apter
slides created by Ethan Apter
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
slides created by Marty Stepp
Insertion sort.
Insertion sort.
slides created by Marty Stepp
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

More Linked Lists and Loops Based on slides by Ethan Apter CSE 143 Lecture 8 More Linked Lists and Loops Based on slides by Ethan Apter

LinkedIntList Constructors // post: constructs an empty list public LinkedIntList() { front = null; } What if we want to create some lists containing values? // post: constructs a list initialized to contain // the values 1 through n in descending order. public LinkedIntList(int n) {

LinkedIntList Constructors // post: constructs a list initialized to contain // the values 1 through n in descending order. public LinkedIntList(int n) { }

LinkedIntList Constructors // pre: n >= 0 // post: constructs a list initialized to contain // the values 1 through n in descending order. public LinkedIntList(int n) { front = null; for (int i = 1; i <= n; i++) { front = new ListNode(i, front); }

addSorted Let’s try something harder: let’s write addSorted addSorted is just like the add method of SortedIntList: // pre : list is in sorted (non-decreasing) order // post: given value is inserted into list so as // to preserve sorted order public void addSorted(int value) { ... }

addSorted (1st attempt) Assume we have a list containing 3, 7, and 12. Let’s try to write general code for adding a 10 to the list data next data next data next front 3 7 12 First Try:

addSorted (1st attempt) Assume we have a list containing 3, 7, and 12. Let’s try to write general code for adding a 10 to the list data next data next data next front 3 7 12 First Try: ListNode current = front; while (current.data < value) { current = current.next; }

addSorted (2nd attempt) add a 10 to the list data next data next data next front 3 7 12 We need to stop one node early to change the link. Second Try: ListNode current = front; while (current.next.data < value) { current = current.next; } Continue looping until the next value in the list is >= to the value we want to insert

addSorted (3rd attempt) add a 10 to the list data next data next data next front 3 7 12 Third Try: ListNode current = front; while (current.next.data < value) { current = current.next; }

addSorted (3rd attempt) add a 10 to the list data next data next data next front 3 7 12 Third Try: ListNode current = front; while (current.next.data < value) { current = current.next; } ListNode temp = new ListNode(value, current.next); current.next = temp;

addSorted (3rd attempt) Third Try: ListNode current = front; while (current.next.data < value) { current = current.next; } ListNode temp = new ListNode(value, current.next); current.next = temp; Or replace the last two lines with: current.next = new ListNode(value, current.next);

addSorted Now we need to insert our new node: current.next = new ListNode(value, current.next); Which modifies our list to look like this: data next data next data next data next front 3 7 10 12 current Some people prefer to use a temporary variable when inserting a new node into a list: ListNode temp = new ListNode(value, current.next); current.next = temp;

addSorted (add 13?) Third Try: ListNode current = front; add a 13 to the list data next data next data next front 3 7 12 Third Try: ListNode current = front; while (current.next.data < value) { current = current.next; } current.next = new ListNode(value, current.next);

addSorted (4th attempt) add a 13 to the list data next data next data next front 3 7 12 Fourth Try: ListNode current = front; while(current.next.data < value && current.next !=null ){ current = current.next; } current.next = new ListNode(value, current.next);

addSorted What if we try to use our code to add 13? our loop test will continue forever! or until current.next is null, which will make current.next.data throw a NullPointerException We can modify our loop test to check for this: while (current.next != null && current.next.data < value) This works because the && operator short-circuits this means if the first test is false, it won’t try the second test Notice that the order of the loop test is important! we can’t switch the tests. Why not?

addSorted (5th attempt) So we can update our addSorted code: public void addSorted(int value) { ListNode current = front; while (current.next != null && current.next.data < value) { current = current.next; } current.next = new ListNode(value, current.next); And now we can successfully add 13 to the end: data next data next data next data next data next front 3 7 10 12 13 What happens if we try to add a 0 to our list?

addSorted (Adding 0?) Adding 0? public void addSorted(int value) { ListNode current = front; while (current.next != null && current.next.data < value) { current = current.next; } current.next = new ListNode(value, current.next); Adding 0? data next data next data next data next data next front 3 7 10 12 13 What happens if we try to add a 0 to our list?

addSorted If we try to add a 0, we add it in the wrong place: data next data next data next data next data next front 3 7 10 12 13 data next current 0 We need special code to add an element at the front: front = new ListNode(value, front); And we need to know when to execute the above add code: if (value <= front.data) { // add at front }

addSorted (6th attempt) public void addSorted(int value) { if (value <= front.data) { front = new ListNode(value, front); } else { ListNode current = front; while (current.next !=null && current.next.data < value){ current = current.next; } current.next = new ListNode(value, current.next); And now we can successfully add 0 to the front: data next data next data next data next data next data next front 0 3 7 10 12 13

addSorted What happens if the list is empty when we call addSorted? When we have an empty list, front is null our first line of code asks for front.data NullPointerException! We need to update the first test to be more robust: if (front == null || value <= front.data) Just like the && operator, the || operator also short-circuits so, if front is null, we simply insert at the front if front isn’t null, we still check front.data to decide if we’re still going to insert at the front

addSorted (final version) The final, correct version of addSorted: public void addSorted(int value) { if (front == null || value <= front.data) { front = new ListNode(value, front); } else { ListNode current = front; while (current.next != null && current.next.data < value) { current = current.next; } current.next = new ListNode(value, current.next); That was surprisingly hard! It had four possible cases: empty list value <= [all values in list] [some value in list] < value <= [some value in list] value > [all values in list]