CSC 1052 Algorithms & Data Structures II: Lists

Similar documents
CSC 1052 Algorithms & Data Structures II: Queues

CSC 1052 Algorithms & Data Structures II: Linked Lists Revisited

CSC 1052 Algorithms & Data Structures II: Recursion

Programming II (CS300)

CSC 1052 Algorithms & Data Structures II: Linked Queues

CSC 1052 Algorithms & Data Structures II: Stacks

List ADT. B/W Confirming Pages

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

CSC 1052 Algorithms & Data Structures II: Linked Lists

Programming II (CS300)

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

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

List ADT. Announcements. The List interface. Implementing the List ADT

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

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

Data Structures and Algorithms

Lecture 6: ArrayList Implementation

CS S-05 Abstract Data Types and Lists 1

CSC 1052 Algorithms & Data Structures II: Avoiding Recursion

CS Introduction to Data Structures Week 1 Thursday

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

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

CMPSCI 187: Programming With Data Structures. Lecture #21: Array-Based Lists David Mix Barrington 26 October 2012

DM503 Programming B. Peter Schneider-Kamp.

The ArrayList class CSC 123 Fall 2018 Howard Rosenthal

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

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

CS 307 Midterm 2 Fall 2008

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

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

Introduction to Object-Oriented Programming

Java Collection Framework

What is the Java Collections Framework?

Array Based Lists. Collections

Java Review: Objects

CSE 143. Computer Programming II

Java Collections. Readings and References. Collections Framework. Java 2 Collections. CSE 403, Spring 2004 Software Engineering

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

CS201 ArrayLists, Generics, and Dynamic Data Structures (Chapters 14, 15)

Topic #9: Collections. Readings and References. Collections. Collection Interface. Java Collections CSE142 A-1

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

EXAMINATIONS 2012 Trimester 1, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CSE 143 Au03 Midterm 2 Page 1 of 7

COMP200 GENERICS. OOP using Java, from slides by Shayan Javed

COMP 250. Lecture 29. interfaces. Nov. 18, 2016

Computer Science 62. Midterm Examination

EXAMINATIONS 2016 TRIMESTER 2

Algorithm Efficiency and Big-O

Interfaces, collections and comparisons

CMSC 206: Data Structures Final Exam Reference May 2018

Building Java Programs

COMP-202. Generics. COMP Generics, 2013 Jörg Kienzle and others

Arrays and ArrayLists. David Greenstein Monta Vista High School

Lists. Chapter 12. Copyright 2012 by Pearson Education, Inc. All rights reserved

Lists and Iterators. CSSE 221 Fundamentals of Software Development Honors Rose-Hulman Institute of Technology

Lab 5 Random numbers!

Arrays. Chapter Arrays What is an Array?

Basic Data Structures

Collections and Iterators. Collections

CS 307 Final Spring 2008

Name Section Number. CS210 Exam #4 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

STUDENT LESSON A15 ArrayList

Java Collections. Readings and References. Collections Framework. Java 2 Collections. References. CSE 403, Winter 2003 Software Engineering

חומר עזר לבחינה מבוא למדעי המחשב

Basic Data Structures 1 / 24

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

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

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

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Objects and Iterators

CSC 1052 Algorithms & Data Structures II: Interfaces

Array. Prepared By - Rifat Shahriyar

2. The actual object type stored in an object of type CollectionClassName<E> is specified when the object is created.

Linked Lists. Chapter 12.3 in Savitch

Question 0. (1 point) Write the correct ID of the section you normally attend on the cover page of this exam if you have not already done so.

JAVA COLLECTION FRAMEWORK & SETS

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

Arrays.

Exam 1 CSCI 2600 Principles of Software October 6, 2015

Use of the ArrayList class

Exam 1 CSCI 2600 Principles of Software October 6, 2015

Computer Science II (Spring )

DM537 Object-Oriented Programming. Peter Schneider-Kamp.

Prelim 1. CS 2110, October 1, 2015, 5:30 PM Total Question Name True Short Testing Strings Recursion

Slides are adapted from the originals available at

Model Solutions. COMP 103: Test April, 2013

Lecture Outline. Parametric Polymorphism and Java Generics. Polymorphism. Polymorphism

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

CS Ananda Gunawardena

PRACTICE MIDTERM EXAM #2

CSC 321: Data Structures. Fall 2017

Lists. The List ADT. Reading: Textbook Sections

Test 6. moodle.yorku.ca CSE 1020

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

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

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

Here is how we use an arraylist. To access the arraylist code we can import the class via:

A Linked Structure. Chapter 17

EXAMINATIONS 2012 MID YEAR. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS

CSE 143 Au03 Midterm 2 Sample Solution Page 1 of 7

Transcription:

CSC 1052 Algorithms & Data Structures II: Lists Professor Henry Carter Spring 2018

Recap Collections hold and access elements based on content Order and index no longer considered Comparable elements implement methods to compare objects more meaningfully than comparing pointers Collections may be sorted or unsorted, with tradeoffs in the time to search and the time to add/remove elements Collections may be extended to emulate other data structures Bags Mathematical sets

New ADT: Lists Common and powerful data structure in many languages E.g., Lisp, Python A collection with a linear relation between elements Can be used to implement all of the observed ADTs so far E.g., ArrayList for implementing stacks and queues Many applications: allow similar operations to primitive arrays with fewer limitations

Assumptions Unbounded Duplicates allowed No null elements Indices are contiguous Only significant restriction when compared to primitive arrays Two indexed operations are optional: add() and set() Must be implemented, even if just to throw exceptions

Iterators Iterating over the contents of a list is a common operation In some cases, multiple indices into the list need to be maintained Rather than require the developer to hard code indices, Iterator objects simplify iterating over data structures Two interfaces are required: Iterable and Iterator

Iterable interface Implemented by ADTs that allow iteration Contains a single method: iterator() A factory method returning an iterator Allows new for loop syntax for(string temp: strings) System.out.println(temp);

Iterator interface Implemented by classes that specify how to iterate through a data structure Contains three methods: next() hasnext() remove() Must be implemented dependent on the data structure!

Set up Set up the in-class project Import ABList.java Create listdriver.java

ListInterface Extends Collections and Iterable Essentially adds index-related operations to collections Contains five new methods: add() set() get() indexof() remove()

ListInterface package ch06.lists; import java.util.*; import ch05.collections.collectioninterface; public interface ListInterface<T> extends CollectionInterface<T>, Iterable<T> { void add(int index, T element); T set(int index, T newelement); T get(int index); int indexof(t target); } T remove(int index);

Implementation: Array We implement the list first with an array Separate the method implementation into three groups: Collection methods New list methods Iterator() The internal array will match the external list ordering Unsorted

Collection methods Recall: what five methods are required for collections? What two hidden methods were helpful in implementing these? Will any of these methods change?

Order-Preserving Remove

New Methods Each of the new methods requires an index as either input or output Indexing exactly matches the indexing of the internal array The range must match number of elements, not the size of the primitive array While the collection methods add and remove return boolean, indexed methods throw exceptions Why?

Example: set() public T set(int index, T newelement) // Throws IndexOutOfBoundsException if passed an index argument // such that index < 0 or index >= size(). // Otherwise, replaces element on this list at position index with // newelement and returns the replaced element. { if ((index < 0) (index >= size())) throw new IndexOutOfBoundsException("Illegal index of " + index + " passed to ABList set method.\n"); } T hold = elements[index]; elements[index] = newelement; return hold;

ListInterface package ch06.lists; import java.util.*; import ch05.collections.collectioninterface; public interface ListInterface<T> extends CollectionInterface<T>, Iterable<T> { void add(int index, T element); T set(int index, T newelement); T get(int index); int indexof(t target); } T remove(int index);

Iteration To make the list iterable, we need to define the iterator() method Simply return an Iterator object What is an Iterator object? Where is it implemented?

Implementing the List Iterator Public class Requires an external class to modify internal state in the List Inner class Allows hidden definition used only inside the List Anonymous class Allows in-line definition used only inside the iterator() method

Implement the Iterator Three required methods: next() hasnext() remove() Use a private inner class

Anonymous Inner Class

Efficiency Collection methods match array implementation With the exception of add at an index Indexed observers run in constant time O(1) Indexed modifiers run in linear O(N) time Iterator methods run in what time?

Next Time... Review session Bring questions! Check the course webpage for practice problems Peer Tutors http://www.csc.villanova.edu/help/ 22