CS : Data Structures

Size: px
Start display at page:

Download "CS : Data Structures"

Transcription

1 CS : Data Structures Michael Schatz Oct 3, 2016 Lecture 13: More Lists

2 Assignment 4: Due Sunday Oct 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently written!

3 Part 1: Inheritence

4 Why Inheritance? Code Reuse Subclass gets to use all methods of the parent class for free Overriding Subclass can have more specific implementation than the parent class Design constraints Subclasses get all of the features of the parent, whether you like them or not! Saying B inherits from A is a very strong relationship: anytime that A could be used, B could be instead

5 Inheritance Types Single Inheritance Multilevel Inheritance Multiple Inheritance B isa A Square isa Rectangle C isa B, B isa A Square isa Rectangle Rectangle isa Shape => Square isa Shape C isa A, C isa B Mike isa CS Prof Mike isa Bio Prof

6 Inheritance versus Encapsulation B isa A Pokemon Trainer Position pos String name String team List<Pokemon> List<Eggs> Backpack pack Position Double lat Double long Double altitude Backpack Int capacity List<Items> items Square isa Rectangle Trainer hasa Position, Trainer hasa Backpack, etc Encapsulation is used to hide the values or state of a structured data object inside a class,

7 Part 2: Lists

8 List Queue insertfront insertback addme. = first; first = addme; last. = addme; addme. = null removefront removeback first = first.;???

9 removelast (Singly Linked List) Queue first null last??? front Oops, just made remove an O(n) operation How might you address this?

10 Deque with Doubly Linked List Deque first null last null Very similar to a singly linked list, except each node has a reference to both the and ious node in the list A little more overhead, but significantly increased flexibility: supports insertfront, insertback, removefront, removeback, insertbefore, removemiddle

11 Dequeue Interface public interface Dequeue<T> { boolean empty(); int length(); T front(); T back(); void insertfront(t t); void insertback(t t); void removefront(); void removeback(); public class MyDeque implements Dequeue<T> { private static class <T>{ T data <T> ; <T> ; private <T> front; private <T> back; T front() { return front.data; T back() { return back.data;... How would you implement a deque with a doubly linked list? How would you implement a general List<T> class?

12 List v1 public interface <T> { void setvalue(t t); T getvalue(); void setnext(<t> n); void setprev(<t> n); void getnext(<t> n); void getprev(<t> n); public interface List<T> { boolean empty(); int length(); Is this a good design? No! We expose that Lists use s for underlying storage. Worse, client programs may incorrectly edit the / references <T> front() throws EmptyException; <T> back() throws EmptyException; void insertfront(<t> t); void insertback(<t> t); void insertbefore(<t> t); void insertafter(<t> t); void removefront() throws EmptyException; void removeback() throws EmptyException; void remove(<t> t);

13 List v2 public class MyList<T> implements List<T> { private static class <T> { T data; <T> ; <T> ; boolean empty() {... int length() {... T front() throws EmptyException {... T back() throws EmptyException {... void insertfront(t t) {... void insertback(t t) {... void insertbefore(???) {... void insertafter(???) {... Some implementations will have public node classes with private / references. However they also have public setnext(), setprev() => poor encapsulation! void removefront() throws EmptyException {... void removeback() throws EmptyException {... void remove(???) {... Is this a good design? Better, the nested class encapsulates the storage medium, but restricts what methods can be implemented to very first/last elements

14 List v3 public class MyList<T> implements List<T> { private static class <T> { T data; <T> ; <T> ; boolean empty() {... int length() {... T front() throws EmptyException {... T back() throws EmptyException {... void insertfront(t t) {... void insertback(t t) {... void insertbefore(int idx) {... void insertafter(int idx) {... void removefront() throws EmptyException {... void removeback() throws EmptyException {... void remove(int idx) {... Is this a good design? Slightly better: More flexible, but now insertbefore and remove() are O(n) operations

15 public interface <T> { void setvalue(t t); T getvalue(); void setnext(<t> n); void setprev(<t> n); List v4 void getnext(<t> n); void getprev(<t> n); public interface List<T> { boolean empty(); int length(); <T> front(); <T> back(); void insertfront(<t> t); void insertback(<t> t); void removefront(); void removeback();

16 List v4 public interface <T> { void setvalue(t t); T getvalue(); void setnext(<t> n); void setprev(<t> n); public interface Position<T> { // empty on purpose public interface List<T> { // simplified interface int length(); void getnext(<t> n); void getprev(<t> n); Position<T> insertfront(t t); Position<T> insertback(t t); void insertbefore(position<t> t); void insertafter(position<t> t); public interface List<T> { boolean empty(); int length(); <T> front(); <T> back(); void insertfront(<t> t); void insertback(<t> t); void removeat(position<t> p); void removefront(); void removeback();

17 List v4 public interface <T> { I am void a position setvalue(t and while t); you can T hold getvalue(); on to me, you can t do anything else with me! void setnext(<t> n); void setprev(<t> n); public interface Position<T> { // empty on purpose public interface List<T> { // simplified interface int length(); void getnext(<t> n); void getprev(<t> n); Inserting at front or back creates the Position objects. public interface List<T> { If you boolean want, empty(); you could keep references int length(); to the Position objects even in the middle of <T> front(); <T> the back(); list Pass void in a insertfront(<t> Position, and it will t); void remove insertback(<t> it from the list t); Position<T> insertfront(t t); Position<T> insertback(t t); void insertbefore(position<t> t); void insertafter(position<t> t); void removeat(position<t> p); void removefront(); void removeback();

18 List v4 public class List<T> implements List<T> private static class <T> implements Position<T> { <T> ; <T> ; T data; List<T> color; // avoid fake positions private <T> front; private <T> back; private int elements; public int length() { return this.elements; public Position<T> insertfront(t t) {... public Position<T> insertback(t t) {... public void remoteat(position<t> p) {... Public Position interface, but nested (private static) Implementation

19 List v4 List l = new List<String>(); Position a = l.insertfront( Mike ); Position b = l.insertback( Peter ); Position c = l.insertfront( Kelly ); l.removeat(a); public interface Position<T> { // empty on purpose public interface List<T> { // simplified interface int length(); Position<T> insertfront(t t); Position<T> insertback(t t); // TODO: void is temporary void removeat(position<t> p); a b c List front Kelly Mike Peter null back null

20 List v4 public String tostring () { String s ="["; <T> n = this.front; while (n!= null) { s += n.data.tostring(); if (n.!= null) { s +=""; n = n.; s +="]"; return s; public static void main(string[] args) { List l = new List(); Position a = l.insertfront("mike"); System.out.println(l); Position b = l.insertback("peter"); System.out.println(l); Position c = l.insertfront("kelly"); System.out.println(l); l.remove(a); System.out.println(l); $ java List [Mike] [Mike, Peter] [Kelly, Mike, Peter] [Kelly, Peter]

21 How to test the code? public class List<T> implements List<T> { private static final class <T> implements Position<T> { <T> ; <T> ; T data; List<T> owner; public T get() { return this.data; public void put(t t) { this.data = t;... public Position<T> front() throws EmptyException { public Position<T> back() throws EmptyException { public Position<T> insertfront(t t) { public Position<T> insertback(t t) { public void removefront() throws EmptyException { public void removeback() throws EmptyException { public Position<T> insertbefore(position<t> p, T t) throws PositionException { public Position<T> insertafter(position<t> p, T t) throws PositionException { public void remove(position<t> p) throws PositionException { public String tostring() {

22 How to test the code? public class List<T> implements List<T> { private static final class <T> implements Position<T> { <T> ; <T> ; T data; The output for every method should be tested, including List<T> owner; any exceptions for invalid inputs public T get() { return this.data; public void put(t t) { this.data = t; Use tostring() method to test the state of private member... variables public Position<T> front() throws EmptyException { public Position<T> back() throws EmptyException { public Position<T> insertfront(t t) { public Position<T> insertback(t t) { public void removefront() throws EmptyException { public void removeback() throws EmptyException { public Position<T> insertbefore(position<t> p, T t) throws PositionException { public Position<T> insertafter(position<t> p, T t) throws PositionException { public void remove(position<t> p) throws PositionException { public String tostring() {

23 TestList.java public void testremoveatfakeposition () { List <String> faker = new List<String>(); Position <String> fake = faker.insertfront("hehehe"); list.insertback("paul"); list.insertback ("Mary"); assertequals("[paul, Mary]", list.tostring ()); assertequals(2, list.length()); list.removeat(fake); Testing code is significantly longer than the implementation J

24 Part 3: Make Lists Useful Again J

25 List v4 List l = new List<String>(); Position a = l.insertfront( Mike ); Position b = l.insertback( Peter ); Position c = l.insertfront( Kelly ); l.removeat(a); public interface Position<T> { // empty on purpose public interface List<T> { // simplified interface int length(); Position<T> insertfront(t t); Position<T> insertback(t t); // TODO: void is temporary void removeat(position<t> p); a b c List front Kelly Mike Peter null back null

26 List v4 List l = new List<String>(); Position a = l.insertfront( Mike ); Position b = l.insertback( Peter ); Position c = l.insertfront( Kelly ); l.removeat(a); public interface Position<T> { // empty on purpose public interface List<T> { // simplified interface int length(); Position<T> insertfront(t t); Position<T> insertback(t t); // TODO: void is temporary void removeat(position<t> p); b a c List front Kelly Mike This interface protects the integrity of the List, but is weird in that we can have references to objects but cant get their value or do anything else with them except remove Peter them null back null

27 List v5 public interface Position<T> { T get(); void put(t t); public interface List<T> { private static class <T> implements Position<T> { <T> ; <T> ; T data; List<T> color; public T get() { return this.data; public void put(t t) { this.data = t;...

28 List v5 public interface Position<T> { T get(); void put(t t); Hooray, now we can get/set the value from a Position public interface List<T> { private static class <T> implements Position<T> { <T> ; <T> ; T data; List<T> color; public T get() { return this.data; public void put(t t) { this.data = t;... Why wouldn t you want to do it this way? What if you wanted a UniqueList<T> that only stored unique items? This would have to be checked in the UniqueList<T> implementation

29 List v5 public void testpositionget() { Position<String> p1 = list.insertback("peter"); Position<String> p2 = list.insertback("paul"); assertequals("[peter Paul]", list.tostring()); assertequals("peter", p1.get()); assertequals("paul", public void testpositionput() { Position<String> p1 = list.insertback("peter"); list.insertback("paul"); assertequals("[peter Paul]", list.tostring()); p1.put("mary"); assertequals("mary", p1.get()); assertequals("[mary Paul]", list.tostring()); What else are we missing?

30 List v6 public interface List<T> {... Position<T> front() throws EmptyListException; Position<T> back() throws EmptyListException; Position<T> (Position<T> p) throws InvalidPositionException; Position<T> ious(position<t> p) throws InvalidPositionException; boolean hasnext(position<t> p) throws InvalidPositionException; boolean hasprevious(position<t> p) throws InvalidPositionException; Why do we put () and () into the list and not Position? boolean valid(position<t> p); For more complex data structures, like trees or graphs, () and () will be more complicated

31 List v6 Iterating // Very C++ like Position<String> current = list.front(); Position<String> last = list.back(); while (current!= last) { // do whatever we need to do at the current position current = list.(current); // More Java-like Position<String> current = list.front(); for (;;) { // do whatever we need to do at the current position if (list.hasnext(current)) { current = list.(current); else { break; // Very Java-like Position<String> current = list.front(); while (list.valid(current)) { // do whatever we need to do at the current position current = list.(current);

32 Iterator Interface public interface Iterator<T> { boolean valid(); void (); // element, not necessarily T get(); // get ok, but put may break invariants public interface List<T> {... Iterator<T> forwarditerator(); Iterator<T> backwarditerator();... Iterator<String> i = list.forwarditerator(); while (i.valid()) { String e = i.get(); // do whatever with the element e i.();

33 Iterator Interface private static class ListIterator<T> implements Iterator<T> { private <T> current; private boolean forward; ListIterator(<T> start, boolean forward) { this.current = start; Ternary operator: this.forward = forward; If (this.forward) { public boolean valid() { this.current = this.current.; return this.current!= else null; { this.current = this.current.; public void () { this.current = this.forward? this.current. : this.current.; public T get() { return this.current.get();

34 Iterator Interface public Iterator<T> forwarditerator() { return new ListIterator<T>(this.front, true); public Iterator<T> backwarditerator() { return new ListIterator<T>(this.back, public void testforwarditerator() { list.insertback("peter"); list.insertback("paul"); list.insertback("mary"); String[] expected = {"Peter", "Paul", "Mary"; int current = 0; Iterator<String> i = list.forwarditerator(); while (i.valid()) { String e = i.get(); assertequals(expected[current], e); i.(); current += 1; assertequals(3, current);

35 Iterator Interface public Iterator<T> forwarditerator() { return new ListIterator<T>(this.front, true); public Iterator<T> backwarditerator() { return new ListIterator<T>(this.back, public void testbackwarditerator() { list.insertback("peter"); list.insertback("paul"); list.insertback("mary"); String[] expected = {"Mary", "Paul", "Peter"; int current = 0; Iterator<String> i = list.backwarditerator(); while (i.valid()) { String e = i.get(); assertequals(expected[current], e); i.(); current += 1; assertequals(3, current);

36 Java Iterators

37 Java Iterators Will become even more important for more complex data structures to simplify certain operations like printing every element in sorted order

38 Java Iterable

39 List v7 public interface List<T> extends Iterable<T> {... private static class ListIterator<T> implements Iterator<T> {... Iterator<String> i = list.iterator(); while (i.hasnext()) { String e = i.(); // do something with element e for (String e: list) { // do something with element e

40 Living in a null world List List front null front Mike null back back null List front Mike Peter null back null

41 Living in a null world public Position <T> insertback(t t) {... if (this.back List!= null) { List this.back. = n; public Position <T> insertfront(t t ) {... if (this.front == null) { Mike null if (this.front!= null) { this.front front = n; null this.front. =n;... if (this.back==null) { this.back null = n; back back... List public void removeat(position<t> p) { Mike Peter null... if (n. front!= null) { n.. = n.; if (n.!= null) { n.. = n.;... null back

42 Doubly Linked List with Sentinels List first f b null last null

43 Doubly Linked List with Sentinels List first f 1 b null last null

44 Doubly Linked List with Sentinels List first f 1 2 b null last null For the cost of a tiny bit of extra memory, the code gets significantly simpler!

45 Part 4: Midterm Review!

46 Next Steps 1. Reflect on the magic and power of stacks, queues, deques! 2. Work on Assignment 4: Due Sunday 3 Oct 10:00 pm 3. Start to review for Midterm on Monday Oct Your notes from class 2. Lecture Notes on Piazza 3. Slides on course webpage 4. Online & printed textbooks 5. Sample Midterm!!!

47 Welcome to CS Questions?

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Oct 3, 2016 Lecture 14: Machine Code Optimization Assignment 5: Due Sunday Oct 9 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Oct 7, 2016 Lecture 15: More Machine Code Optimization ;-) Assignment 5: Due Sunday Oct 9 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Oct 14, 2016 Lecture 18: Tree Implementation Assignment 5: Due Sunday Oct 9 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently

More information

CS : Data Structures Michael Schatz. Sept Lecture 5: Iterators

CS : Data Structures Michael Schatz. Sept Lecture 5: Iterators CS 600.226: Data Structures Michael Schatz Sept 10 2018 Lecture 5: Iterators Agenda 1. Review HW1 2. References and Linked Lists 3. Nested Classes and Iterators Assignment 1: Due Friday Sept 14 @ 10pm

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Oct 12, 2016 Lecture 17: Trees Assignment 5: Due Sunday Oct 9 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently written!

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Oct 17, 2016 Lecture 19: Trees and Graphs Assignment 6: Due Sunday Oct 23 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently

More information

CS : Data Structures Michael Schatz. Oct Lecture 18. Midterm review 2

CS : Data Structures Michael Schatz. Oct Lecture 18. Midterm review 2 CS 600.226: Data Structures Michael Schatz Oct 10 2018 Lecture 18. Midterm review 2 Midterm Topics Topics 01.Intro (kd-tree) 02.Interfaces 03.ArraysGenericsExceptions 04.Lists 05.Iterators 06.Complexity

More information

CS : Data Structures Michael Schatz. Oct 22, 2018 Lecture 22. Ordered Sets

CS : Data Structures Michael Schatz. Oct 22, 2018 Lecture 22. Ordered Sets CS 600.226: Data Structures Michael Schatz Oct 22, 2018 Lecture 22. Ordered Sets HW5 Assignment 5: Six Degrees of Awesome Out on: October 17, 2018 Due by: October 26, 2018 before 10:00 pm Collaboration:

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Oct 20, 2016 Lecture 21: Graphs and Sets Assignment 6: Due Monday Oct 24 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Sept 23, 2016 Lecture 9: Stacks Assignment 3: Due Sunday Sept 25 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently

More information

lecture09: Linked Lists

lecture09: Linked Lists lecture09: Largely based on slides by Cinda Heeren CS 225 UIUC 24th June, 2013 Announcements mp2 due tonight mt1 tomorrow night! mt1 review instead of lab tomorrow morning mp3 released tonight, mp3.1 extra

More information

CS : Data Structures Michael Schatz. Oct 15, 2018 Lecture 20. Sets

CS : Data Structures Michael Schatz. Oct 15, 2018 Lecture 20. Sets CS 600.226: Data Structures Michael Schatz Oct 15, 2018 Lecture 20. Sets Agenda 1. Recap on Graphs 2. Sets Part 1:Graphs Graphs are Everywhere! Computers in a network, Friends on Facebook, Roads & Cities

More information

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

CS231 - Spring 2017 Linked Lists. ArrayList is an implementation of List based on arrays. LinkedList is an implementation of List based on nodes. CS231 - Spring 2017 Linked Lists List o Data structure which stores a fixed-size sequential collection of elements of the same type. o We've already seen two ways that you can store data in lists in Java.

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Sept 21, 2016 Lecture 8: Sorting Assignment 3: Due Sunday Sept 25 @ 10pm Remember: javac Xlint:all & checkstyle *.java Solutions should be independently written!

More information

CS : Data Structures Michael Schatz. Oct Lecture 15. Graphs

CS : Data Structures Michael Schatz. Oct Lecture 15. Graphs CS 600.226: Data Structures Michael Schatz Oct 3 2018 Lecture 15. Graphs 1. Questions on HW4 2. Recap on Trees 3. Graphs genda ssignment 4: Due riday Oct 5 @ 10pm https://github.com/schatzlab/datastructures2018/blob/master/assignments/assignment04/redme.md

More information

Lists. ordered lists unordered lists indexed lists 9-3

Lists. ordered lists unordered lists indexed lists 9-3 The List ADT Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations Compare list implementations 9-2 Lists A list is a

More information

Programming Exercise 14: Inheritance and Polymorphism

Programming Exercise 14: Inheritance and Polymorphism Programming Exercise 14: Inheritance and Polymorphism Purpose: Gain experience in extending a base class and overriding some of its methods. Background readings from textbook: Liang, Sections 11.1-11.5.

More information

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

Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Queues CSE 2011 Fall 2009 9/28/2009 7:56 AM 1 Queues: FIFO Insertions and removals follow the Fist-In First-Out rule: Insertions: at the rear of the queue Removals: at the front of the queue Applications,

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Sept 18, 2016 Lecture 7: JUnit and Complexity Analysis Assignment 2: Due Sunday Sept 18 @ 10pm Remember: javac Xlint:all & checkstyle *.java Solutions should

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019

Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019 CS18 Integrated Introduction to Computer Science Fisler Homework 2: Imperative Due: 5:00 PM, Feb 15, 2019 Contents 1 Overview of Generic/Parameterized Types 2 2 Double the Fun with Doubly-Linked Lists

More information

CS : Data Structures Michael Schatz. Sept 5, 2018 Lecture 3: Introduction to Interfaces

CS : Data Structures Michael Schatz. Sept 5, 2018 Lecture 3: Introduction to Interfaces CS 600.226: Data Structures Michael Schatz Sept 5, 2018 Lecture 3: Introduction to Interfaces Agenda 1. Quick Review 2. Introduction to Java Interfaces 3. Introduction to Generics, Exceptions and Arrays

More information

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013 Linear Structure Linear Structures Chapter 4 CPTR 318 Every non-empty linear structure has A unique element called first A unique element called last Every element except last has a unique successor Every

More information

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism

COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism COS226 - Spring 2018 Class Meeting # 13 March 26, 2018 Inheritance & Polymorphism Ibrahim Albluwi Composition A GuitarString has a RingBuffer. A MarkovModel has a Symbol Table. A Symbol Table has a Binary

More information

About this exam review

About this exam review Final Exam Review About this exam review I ve prepared an outline of the material covered in class May not be totally complete! Exam may ask about things that were covered in class but not in this review

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 24 October 29, 2018 Arrays, Java ASM Chapter 21 and 22 Announcements HW6: Java Programming (Pennstagram) Due TOMORROW at 11:59pm Reminder: please complete

More information

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

COMP 250. Lecture 29. interfaces. Nov. 18, 2016 COMP 250 Lecture 29 interfaces Nov. 18, 2016 1 ADT (abstract data type) ADT s specify a set of operations, and allow us to ignore implementation details. Examples: list stack queue binary search tree priority

More information

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

CSE 143 Lecture 26. Advanced collection classes. (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , CSE 143 Lecture 26 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, 15.3-15.4, 16.4-16.5 slides created by Marty Stepp, adapted by Alyssa Harding

More information

Lecture 8: Iterators and More Mutation

Lecture 8: Iterators and More Mutation Integrated Introduction to Computer Science Fisler, Nelson Contents 1 Traversing Lists 1 2 Motivating Iterators 2 3 Writing an Iterator 3 4 Writing Sum with an Iterator 4 Objectives By the end of this

More information

CS61B Lecture #19. Last modified: Mon Oct 13 12:02: CS61B: Lecture #19 1

CS61B Lecture #19. Last modified: Mon Oct 13 12:02: CS61B: Lecture #19 1 CS61B Lecture #19 Administrative: HKN midterm review session, 320 Soda, 6:30-8 Tuesday (10/14). Review session Wednesday 5:30 6:30PM in 306 Soda. Need alternative test time? Make sure you send me mail.

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 07: Linked Lists and Iterators MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Linked Lists 2 Introduction Linked List Abstract Data Type General Implementation of the ListADT

More information

Solutions to a few of the review problems:

Solutions to a few of the review problems: Solutions to a few of the review problems: Problem 1 The interface Deque can have array based and linked implementations. Partial versions of implementation classes follow. Supply implementations for the

More information

Topic 10: The Java Collections Framework (and Iterators)

Topic 10: The Java Collections Framework (and Iterators) Topic 10: The Java Collections Framework (and Iterators) A set of interfaces and classes to help manage collections of data. Why study the Collections Framework? very useful in many different kinds of

More information

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

CSC 172 Data Structures and Algorithms. Lecture #9 Spring 2018 CSC 172 Data Structures and Algorithms Lecture #9 Spring 2018 SINGLY LINKED LIST 3.1.3 Linked lists We will consider these for Singly linked lists Doubly linked lists Basic Singly Linked List class Node

More information

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004 Type Hierarchy Comp-303 : Programming Techniques Lecture 9 Alexandre Denault Computer Science McGill University Winter 2004 February 16, 2004 Lecture 9 Comp 303 : Programming Techniques Page 1 Last lecture...

More information

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray();

Converting Collections to Arrays. A Bad Approach to Array Conversion. A Better Approach to Array Conversion. public Object[] toarray(); Converting Collections to Arrays Every Java collection can be converted to an array This is part of the basic Collection interface The most elementary form of this method produces an array of base-type

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Introduction to Linked Lists

Introduction to Linked Lists Introduction to Linked Lists In your previous programming course, you organized and processed data items sequentially using an array (or possibly an arraylist, or a vector). You probably performed several

More information

Announcements. CS18000: Problem Solving And Object-Oriented Programming

Announcements. CS18000: Problem Solving And Object-Oriented Programming Announcements Exam 1 Monday, February 28 Wetherill 200, 4:30pm-5:20pm Coverage: Through Week 6 Project 2 is a good study mechanism Final Exam Tuesday, May 3, 3:20pm-5:20pm, PHYS 112 If you have three or

More information

Java Review: Objects

Java Review: Objects Outline Java review Abstract Data Types (ADTs) Interfaces Class Hierarchy, Abstract Classes, Inheritance Invariants Lists ArrayList LinkedList runtime analysis Iterators Java references 1 Exam Preparation

More information

Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski

Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski CS 190 - Java Introduction to Java Andy Mroczkowski uamroczk@cs.drexel.edu Department of Computer Science Drexel University February 11, 2008 / Lecture 4 Outline Course Status Course Information & Schedule

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 24, 2013 Abstract These lecture notes are meant to be looked

More information

CMPSCI 187: Programming With Data Structures. Lecture #23: Linked Lists David Mix Barrington 31 October 2012

CMPSCI 187: Programming With Data Structures. Lecture #23: Linked Lists David Mix Barrington 31 October 2012 CMPSCI 187: Programming With Data Structures Lecture #23: Linked Lists David Mix Barrington 31 October 2012 Linked Lists Implementing Lists With References The find Method The RefUnsortedList Class Methods

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Dec 7, 2016 Lecture 38: Union-Find Assignment 10: Due Monday Dec 5 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently

More information

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures

Abstract Data Types (ADTs) Example ADTs. Using an Abstract Data Type. Class #08: Linear Data Structures Abstract Data Types (ADTs) Class #08: Linear Data Structures Software Design III (CS 340): M. Allen, 08 Feb. 16 An ADT defines a kind of computational entity: A set of objects, with possible values A set

More information

COMP 250 Midterm #2 March 11 th 2013

COMP 250 Midterm #2 March 11 th 2013 NAME: STUDENT ID: COMP 250 Midterm #2 March 11 th 2013 - This exam has 6 pages - This is an open book and open notes exam. No electronic equipment is allowed. 1) Questions with short answers (28 points;

More information

Class 26: Linked Lists

Class 26: Linked Lists Introduction to Computation and Problem Solving Class 26: Linked Lists Prof. Steven R. Lerman and Dr. V. Judson Harward 2 The Java Collection Classes The java.util package contains implementations of many

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Nov 7, 2016 Lecture 28: HashTables Assignment 8: Due Thursday Nov 10 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be independently

More information

CS 261. List Bag List Queue List Deque. by Tim Budd Ron Metoyer Sinisa Todorovic

CS 261. List Bag List Queue List Deque. by Tim Budd Ron Metoyer Sinisa Todorovic CS 261 List Bag List Queue List Deque by Tim Budd Ron Metoyer Sinisa Todorovic List Bag struct { TYPE value; struct *; ; struct ListBag { struct *sentinel; List Bag Init, Add operations are similar to

More information

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University

Lecture 6. COMP1006/1406 (the OOP course) Summer M. Jason Hinek Carleton University Lecture 6 COMP1006/1406 (the OOP course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments A1,A2,A3 are all marked A4 marking just started A5 is due Friday, A6 is due Monday a quick

More information

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux CS 231 Data Structures and Algorithms Fall 2018 Binary Search Trees Lecture 23 October 29, 2018 Prof. Zadia Codabux 1 Agenda Ternary Operator Binary Search Tree Node based implementation Complexity 2 Administrative

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Sept 7, 2016 Lecture 2: Introduction to Interfaces Course Webpage: Course Discussions: Welcome! http://www.cs.jhu.edu/~cs226/ http://piazza.com Office Hours:

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 26, 2013 Abstract These lecture notes are meant to be looked

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Iterator 1 (part I) Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 26, 2013 Abstract These lecture notes are meant to be looked

More information

CS : Data Structures

CS : Data Structures CS 600.226: Data Structures Michael Schatz Nov 16, 2016 Lecture 32: Mike Week pt 2: BWT Assignment 9: Due Friday Nov 18 @ 10pm Remember: javac Xlint:all & checkstyle *.java & JUnit Solutions should be

More information

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam

CIS 110 Introduction to Computer Programming Spring 2016 Final Exam CIS 110 Introduction to Computer Programming Spring 2016 Final Exam Name: Recitation # (e.g., 201): Pennkey (e.g., eeaton): My signature below certifies that I have complied with the University of Pennsylvania

More information

Recursive Objects. Singly Linked List (Part 2)

Recursive Objects. Singly Linked List (Part 2) Recursive Objects Singly Linked List (Part 2) 1 Operations at the head of the list operations at the head of the list require special handling because there is no node before the head node 2 Adding to

More information

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

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 13 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

More information

Linked lists. Yet another Abstract Data Type Provides another method for providing space-efficient storage of data

Linked lists. Yet another Abstract Data Type Provides another method for providing space-efficient storage of data Linked lists One of the classic "linear structures" What are linked lists? Yet another Abstract Data Type Provides another method for providing space-efficient storage of data What do they look like? Linked

More information

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

CMSC 132, Object-Oriented Programming II Summer Lecture 9: CMSC 132, Object-Oriented Programming II Summer 2018 Lecturer: Anwar Mamat Lecture 9: Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 9.1 QUEUE

More information

DNHI Homework 3 Solutions List, Stacs and Queues

DNHI Homework 3 Solutions List, Stacs and Queues Solutions List, Stacs and Queues Problem 1 Given the IntegerQueue ADT below state the return value and show the content of the, initially empty, queue of Integer objects after each of the following operations.

More information

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage: Discussion 2C Notes (Week 3, January 21) TA: Brian Choi (schoi@cs.ucla.edu) Section Webpage: http://www.cs.ucla.edu/~schoi/cs32 Abstraction In Homework 1, you were asked to build a class called Bag. Let

More information

CMSC 132: Object-Oriented Programming II

CMSC 132: Object-Oriented Programming II CMSC 132: Object-Oriented Programming II Object-Oriented Programming Intro Department of Computer Science University of Maryland, College Park Object-Oriented Programming (OOP) Approach to improving software

More information

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014

Lists. CSC212 Lecture 8 D. Thiebaut, Fall 2014 Lists CSC212 Lecture 8 D. Thiebaut, Fall 2014 Review List = Organization of Data in a Linear Fashion, where Order is Important Set of actions that can be carried out efficiently on the data. Typical Actions

More information

Lists. CITS2200 Data Structures and Algorithms. Topic 9

Lists. CITS2200 Data Structures and Algorithms. Topic 9 CITS2200 Data Structures and Algorithms Topic 9 Lists Why lists? List windows Specification Block representation Singly linked representation Performance comparisons Reading: Lambert and Osborne, Sections

More information

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2

CSCI 136 Data Structures & Advanced Programming. Lecture 13 Fall 2018 Instructors: Bill 2 CSCI 136 Data Structures & Advanced Programming Lecture 13 Fall 2018 Instructors: Bill 2 Announcements Lab today! After mid-term we ll have some non-partner labs It s Lab5 not Lab 4 Mid-term exam is Wednesday,

More information

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015

MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015 MERGESORT & QUICKSORT cs2420 Introduction to Algorithms and Data Structures Spring 2015 1 administrivia 2 -assignment 4 due tonight at midnight -assignment 5 is out -midterm next Tuesday 3 last time 4

More information

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

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List. Implementing a List in Java CSE 143 Java List Implementation Using Arrays Reading: Ch. 22 Two implementation approaches are most commonly used for simple lists: Arrays Linked list Java Interface List concrete

More information

CE204 Data Structures and Algorithms Part 2

CE204 Data Structures and Algorithms Part 2 CE204 Data Structures and Algorithms Part 2 14/01/2018 CE204 Part 2 1 Abstract Data Types 1 An abstract data type is a type that may be specified completely without the use of any programming language.

More information

Project 2 (Deques and Randomized Queues)

Project 2 (Deques and Randomized Queues) Project 2 (Deques and Randomized Queues) Prologue Project goal: implement generic and iterable data structures, such as double-ended and randomized queues, using arrays and linked lists Relevant lecture

More information

Midterm Exam (REGULAR SECTION)

Midterm Exam (REGULAR SECTION) Data Structures (CS 102), Professor Yap Fall 2014 Midterm Exam (REGULAR SECTION) October 28, 2014 Midterm Exam Instructions MY NAME:... MY NYU ID:... MY EMAIL:... Please read carefully: 0. Do all questions.

More information

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS ASSIGNMENTS h0 available and due before 10pm on Monday 1/28 h1 available and due before 10pm on Monday 2/4 p1 available and due before 10pm on Thursday 2/7 Week 2 TA Lab Consulting - See schedule (cs400

More information

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

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 Iterators 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 9-2 2-2 What is an Iterator? An iterator is an abstract data

More information

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006

Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006 Cornell University Computer Science 211 Second Preliminary Examination 18 April 2006 There are 4 problems on this exam. It is 8 pages long, so make sure you have the whole exam. You will have 1 1 hours

More information

Inheritance and Polymorphism

Inheritance and Polymorphism Object Oriented Programming Designed and Presented by Dr. Ayman Elshenawy Elsefy Dept. of Systems & Computer Eng.. Al-Azhar University Website: eaymanelshenawy.wordpress.com Email : eaymanelshenawy@azhar.edu.eg

More information

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017

COMP 250. Lecture 6. doubly linked lists. Sept. 20/21, 2017 COMP 250 Lecture 6 doubly linked lists Sept. 20/21, 2017 1 Singly linked list head tail 2 Doubly linked list next prev element Each node has a reference to the next node and to the previous node. head

More information

CS18000: Programming I

CS18000: Programming I CS18000: Programming I Linked Data Structures 5 April 2010 Prof. Chris Clifton Multiple Items: Beyond Arrays interface Set { boolean contains(e item); /* true iff x s.t. item.equals(x) */ void add(e

More information

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure.

January 24, Abstract Data Types (ADTs) An ADT is an abstraction of a data structure. Lists CSE 2011 Winter 2007 January 24, 2007 1 Abstract Data Types (ADTs) An ADT is an abstraction of a data structure. An ADT specifies: data stored operations on the data error conditions associated with

More information

Sequential Containers Cont'd

Sequential Containers Cont'd Cont'd Carlos Moreno cmoreno @ uwaterloo.ca EIT-4103 https://ece.uwaterloo.ca/~cmoreno/ece250 Today's class: Sequential Containers We'll complete our discussion on sequential containers We'll briefly talk

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

More information

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz

COE 312 Data Structures. Welcome to Exam II Monday November 23, Instructor: Dr. Wissam F. Fawaz 1 COE 312 Data Structures Welcome to Exam II Monday November 23, 2016 Instructor: Dr. Wissam F. Fawaz Name: Student ID: Instructions: 1. This exam is Closed Book. Please do not forget to write your name

More information

Classes, interfaces, & documentation. Review of basic building blocks

Classes, interfaces, & documentation. Review of basic building blocks Classes, interfaces, & documentation Review of basic building blocks Objects Data structures literally, storage containers for data constitute object knowledge or state Operations an object can perform

More information

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism

Outline. Subtype Polymorphism, Subtyping vs. Subclassing, Liskov Substitution Principle. Benefits of Subtype Polymorphism. Subtype Polymorphism Subtype, Subtyping vs. Subclassing, Liskov Substitution Principle Outline Subtype polymorphism Subtyping vs. subclassing Liskov Substitution Principle (LSP) Function subtyping Java subtyping Composition:

More information

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4

n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from HW4 Subtype, Subtyping vs. Subclassing, Liskov Substitution Principle Announcements n HW5 out, due Tuesday October 30 th n Part 1: Questions on material we ll cover today n Part 2: BFS using your graph from

More information

ITI Introduction to Computing II

ITI Introduction to Computing II index.pdf March 17, 2013 1 ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 17, 2013 Definitions A List is a linear abstract

More information

Arrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Arrays. https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html 1 Arrays Arrays in Java an array is a container object that holds a fixed number of values of a single type the length of an array is established when the array is created 2 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

More information

CS 2230 CS II: Data structures. Meeting 26: the Set ADT Brandon Myers University of Iowa

CS 2230 CS II: Data structures. Meeting 26: the Set ADT Brandon Myers University of Iowa CS 2230 CS II: Data structures Meeting 26: the Set ADT Brandon Myers University of Iowa Today s learning objectives Interpret code that uses the Set interface Describe how to use a Set for a simple application

More information

Objects and Aspects: Ownership Types

Objects and Aspects: Ownership Types Objects and Aspects: Ownership Types Neel Krishnaswami Department of Computer Science Carnegie Mellon University neelk@cs.cmu.edu Overview The Problem An Introduction to Ownership Types Evaluating How

More information

The class Object. Lecture CS1122 Summer 2008

The class Object.  Lecture CS1122 Summer 2008 The class Object http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html Lecture 10 -- CS1122 Summer 2008 Review Object is at the top of every hierarchy. Every class in Java has an IS-A relationship

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Information Technology and Engineering Iterator (part II) Inner class Implementation: fail-fast Version of March 20, 2011 Abstract These

More information

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP

Administration. Conditional Statements. Agenda. Syntax. Flow of control. Lab 2 due now on floppy Lab 3 due tomorrow via FTP Administration Conditional Statements CS 99 Summer 2000 Michael Clarkson Lecture 4 Lab 2 due now on floppy Lab 3 due tomorrow via FTP need Instruct account password Lab 4 posted this afternoon Prelim 1

More information

Data Structures and Algorithms Notes

Data Structures and Algorithms Notes Data Structures and Algorithms Notes Notes by Winst Course taught by Dr. G. R. Baliga 256-400 ext. 3890 baliga@rowan.edu Course started: September 4, 2012 Last generated: December 18, 2013 Interfaces -

More information

CS 10, Fall 2015, Professor Prasad Jayanti

CS 10, Fall 2015, Professor Prasad Jayanti Problem Solving and Data Structures CS 10, Fall 2015, Professor Prasad Jayanti Midterm Practice Problems We will have a review session on Monday, when I will solve as many of these problems as possible.

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

Section 05: Solutions

Section 05: Solutions Section 05: Solutions 1. Memory and B-Tree (a) Based on your understanding of how computers access and store memory, why might it be faster to access all the elements of an array-based queue than to access

More information

Faculty of Science FINAL EXAMINATION

Faculty of Science FINAL EXAMINATION Faculty of Science FINAL EXAMINATION COMPUTER SCIENCE COMP 250 INTRODUCTION TO COMPUTER SCIENCE Examiner: Prof. Michael Langer April 27, 2010 Associate Examiner: Mr. Joseph Vybihal 9 A.M. 12 P.M. Instructions:

More information

CMSC 202H. Containers and Iterators

CMSC 202H. Containers and Iterators CMSC 202H Containers and Iterators Container Definition A container is a data structure whose purpose is to hold objects. Most languages support several ways to hold objects Arrays are compiler-supported

More information

CS 520 Theory and Practice of Software Engineering Fall 2017

CS 520 Theory and Practice of Software Engineering Fall 2017 CS 520 Theory and Practice of Software Engineering Fall 2017 OO design principles September 14, 2017 Today Code review and (re)design of an MVC application OO design principles Information hiding (and

More information

cs Java: lecture #6

cs Java: lecture #6 cs3101-003 Java: lecture #6 news: homework #5 due today little quiz today it s the last class! please return any textbooks you borrowed from me today s topics: interfaces recursion data structures threads

More information

CS 101 Spring 2006 Final Exam Name: ID:

CS 101 Spring 2006 Final Exam Name:  ID: This exam is open text book but closed-notes, closed-calculator, closed-neighbor, etc. Unlike the midterm exams, you have a full 3 hours to work on this exam. Please sign the honor pledge here: Page 1

More information