CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

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

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

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

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

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

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

CS350: Data Structures Stacks

Linked Lists. Chapter 12.3 in Savitch

Introduction to Computer Science II CS S-18 Linked Lists

17 Multiple Inheritance and ADT Extensions

16 Multiple Inheritance and Extending ADTs

Subclassing for ADTs Implementation

Programming II (CS300)

Object-Oriented Design Lecture 11 CS 3500 Spring 2010 (Pucella) Tuesday, Feb 16, 2010

CMSC 206: Data Structures Final Exam Reference May 2018

Topic 11 Linked Lists

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

CSC 321: Data Structures. Fall 2017

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

Array Based Lists. Collections

Introduction to Computer Science II CS S-20 Linked Lists III

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

Abstract Data Types - Lists Arrays implementa4on Linked- lists implementa4on

List ADT. B/W Confirming Pages

CS Introduction to Data Structures Week 1 Thursday

CS 1316 Exam 2 Summer 2009

Midterm #1. CMSC 433: Programming Language Technologies and Paradigms. October 14, 2013

Topic 10. Abstract Classes. I prefer Agassiz in the abstract, rather than in the concrete.

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

CS 307 Midterm 2 Spring 2008

Lecture 5: Implementing Lists, Version 1

Announcements. Prelude (2) Prelude (1) Data Structures and Information Systems Part 1: Data Structures. Lecture 6: Lists.

Lecture 6: ArrayList Implementation

Stacks and Queues. David Greenstein Monta Vista

Queues. Stacks and Queues

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue

Programming II (CS300)

Object-Oriented Design Lecture 23 CS 3500 Fall 2009 (Pucella) Tuesday, Dec 8, 2009

Arrays.

EXAMINATIONS 2017 TRIMESTER 2

Data Structures and Algorithms

CS S-05 Abstract Data Types and Lists 1

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

Data Structures G5029

More Data Structures (Part 1) Stacks

Basic Data Structures

Midterm Exam 2 CS 455, Spring 2011

Name: Pennkey (eg, sweirich): CIS 120 Final Exam May 8, 2012

Associate Professor Dr. Raed Ibraheem Hamed

Computer Science 62. Midterm Examination

CS 151. Binary Trees. Friday, October 5, 12

16. Dynamic Data Structures

CS18000: Programming I

LinkedList Implementation Mini-project intro

CMSC 341 Lecture 7 Lists

CS 61B, Spring 1999 MT3 Professor M. Clancy

CS S-20 Linked Lists III 1. We can then use the next pointer of the previous node to do removal (example on board)

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

CS 307 Midterm 2 Fall 2009

Lecture 7: Implementing Lists, Version 2

CS S-20 Linked Lists IV 1

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Building Java Programs

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

"apple" "grape" "grape" "grape" "apple"

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

Recursion (Part 3) 1

What is an Iterator? An iterator is an abstract data type that allows us to iterate through the elements of a collection one by one

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

Model Solutions. COMP 103: Mid-term Test. 19th of August, 2016

Class 26: Linked Lists

Adam Blank Lecture 5 Winter 2015 CSE 143. Computer Programming II

CSC 1052 Algorithms & Data Structures II: Lists

Introduction to Computer Science II (ITI 1121) Final Examination

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018

CS 314 Midterm 2 Fall 2012

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

+ Abstract Data Types

CSIS 10B Lab 2 Bags and Stacks

Queues. Data Structures and Design with Java and JUnit Rick Mercer 18-1

UNIVERSITY OF WATERLOO DEPARTMENT OF ELECTRICAL AND COMPUTER ENGINEERING E&CE 250 ALGORITHMS AND DATA STRUCTURES

CS18000: Problem Solving and Object-Oriented Programming

CS427 Inheritance and Virtual Functions. Linked lists. 2/27. 01Replacement.cpp link! i n t GetY ( void ) { return ( y ) ; } ;

CS 307 Midterm 2 Fall 2008

Queues Fall 2018 Margaret Reid-Miller

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

Introduction to Object-Oriented Programming

Arrays. Chapter Arrays What is an Array?

Introduction to Computer Science II CS S-20 Linked Lists IV

CmpSci 187: Programming with Data Structures Spring 2015

Introduction to Computing II (ITI 1121) Final Examination

CS 61B Discussion 5: Inheritance II Fall 2014

Basic Data Structures 1 / 24

ITI Introduction to Computing II

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

20 Subclassing and Mutation

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Queues. CITS2200 Data Structures and Algorithms. Topic 5

Stacks Fall 2018 Margaret Reid-Miller

Transcription:

CS 151 Linked Lists, Recursively Implemented 1

2 Linked Lists, Revisited Recall that a linked list is a structure that represents a sequence of elements that are stored non-contiguously in memory. We can equivalently define a LL recursively, as follows: A linked list is either: an empty list, or an item of type followed by a linked list. Both definitions represent the same structure (a linked list), but they imply different implementations. he non-recursive definition used a Node class to represent each element and their pointers in memory. he recursive definition will use inheritance to define the recursive relationships. Our methods will thus be implemented differently as well, using recursion. Disclaimer: Java is not the best language in which to implement LLs this way, but we re going to push through it anyway so that you can you can see it. 2

(Original) Linked List Implementation Recall that our old implementation started out like this: class LinkedList<> { class Node<> { private private Node<> next public Node(, Node<> next ) this. = this.next = next public getdata() return public Node<> getnext() return next public void setdata( ) this. = public void setnext( Node<> next ) this.next = next Node<> front // points to the front of the list int size // the number of elements in the list // Linked List class methods go here 3 3

Recursive Linked List Implementation Our new implementation will have 3 separate public classes: /* his abstract class represents a Linked List. It defines the methods that every linked list must have, but doesn t implement them. We will have different implementations depending on whether our list is empty, or is a ConsList, i.e. an item followed by a Linked List. */ // method headers for LL methods go here public class EmptyList<> extends LinkedList<> { // method implementations for empty lists go here public class ConsList<> extends LinkedLlist<> { // the of the front element LinkedList<> rest // the LL that follows the front elt // method implementations for non-empty lists go here 4 4

How to Construct Recursively Implemented LLs A user of a LinkedList knows there are 2 actual (non-abstract) classes. hey can store lists of either type in a variable of type LinkedList. For example, to construct an empty list, you could do this: LinkedList<Integer> e = new Empty<Integer>(); o make the list with one element, say [3], you would do this: LinkedList<Integer> c = new ConsList<Integer>( 3, e ); o make the list [5,3], you would do: new ConsList<Integer>( 3, new ConsList<Integer>(3,e) );... public class EmptyList<> extends LinkedList<> {... public class ConsList<> extends LinkedLlist<> { // the of the front element LinkedList<> rest // the LL that follows the front elt 5 5

o add a method to our Linked List class, we must do three things: add the method as an abstract method header in the LinkedList class implement the method in the Empty class implement the method in the Cons class For example, let s start with the isempty() method. We first add the method header to the LinkedList class, to declare that any class that implements LinkedList must have the isempty method. public abstract boolean isempty(); In the EmptyList class, we implement the method. Is an EmptyList empty? public boolean isempty() { return true; // always. In the ConsList class, we implement the method. Is an ConsList empty? public boolean isempty() { return false; // always. 6 6

Now the makeempty() method. Note the change in return type. We add the method header to the abstract LinkedList class: public abstract LinkedList<> makeempty(); We implement the method in the Empty class. public LinkedList<> makeempty() { return new EmptyList<>() We implement the method in the Cons class. LinkedList<> rest public LinkedList<> makeempty() { return new EmptyList<>() 7 7

Now let s try the size() method. We use recursion on the *structure*... We add the method header to the abstract LinkedList class: public abstract int size(); We implement the method in the Empty class. What is the size of an empty list? public int size() { return 0; // base case We implement size in the Cons class. What is the size of this cons list? LinkedList<> rest public int size() { return 1 + rest.size() // because of our recursive definition 8 8

Now let s try the get(int index) method. public abstract get(int index); How to Implement LL Methods We add the method header to the abstract LinkedList class: We implement the method in the Empty class. public get(int index) { throw IndexOutOfBoundsException We implement the method in the Cons class. LinkedList<> rest public get(int index) { if( index < 0 ) throw IndexOutOfBoundsException if( index == 0 ) return else return rest.get(index-1) 9 9

Now let s try the add( item, int index) method. Note change in return type. public abstract LinkedList<> add( item, int index); public LinkedList<> add( item, int index) { if( index!= 0 ) throw IndexOutOfBoundsException return new ConsList<>( item, new EmptyList<>() ) LinkedList<> rest public LinkedList<> add( item, int index) { if( index < 0 ) throw IndexOutOfBoundsException if( index == 0 ) return new ConsList<>( item, this ) else return new ConsList<>(,rest.add(item, index-1)) 10 10

Now the remove( item, int index) method. Note change in return type. public abstract LinkedList<> remove(int index); public LinkedList<> remove(int index) { throw NoSuchElementException LinkedList<> rest public LinkedList<> remove(int index) { if( index < 0 ) throw IndexOutOfBoundsException if( index == 0 ) return rest else return new ConsList<>(, rest.remove(index-1)) 11 11

Let s try to implement a new method: reverse() that reverses the list: public abstract LinkedList<> reverse(); public LinkedList<> reverse() { return this LinkedList<> rest public LinkedList<> reverse() { return rest.reverse().add(, size()-1 ) 12 12

Another new method: find( item ) that returns whether item is in the list: public abstract boolean find( item ); public boolean find( item ) { return false // item is never in an empty list LinkedList<> rest public boolean find( item ) { if( item.equals( ) ) return true else return rest.find( item ) 13 13

One more: append( LinkedList<> list ) that appends list onto end of our list public abstract LinkedList<> append( LinkedList<> list ); public LinkedList<> append( LinkedList<> list ) { return list LinkedList<> rest public LinkedList<> append( LinkedList<> list ) { return new ConsList<>(, rest.append( list ) ) 14 14