03 a: lists, stacks, and queues i - comp.nus.edu.sgcs1102s/slides/slides_03_a.color.pdf · lists in...

73
Abstract Data Types The List ADT Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures and Algorithms Martin Henz January 27, 2010 Generated on Tuesday 26 th January, 2010, 17:00 CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 1

Upload: others

Post on 19-Jul-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

03 A: Lists, Stacks, and Queues I

CS1102S: Data Structures and Algorithms

Martin Henz

January 27, 2010

Generated on Tuesday 26th January, 2010, 17:00

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 1

Page 2: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

1 Abstract Data Types

2 The List ADT

3 Lists in the Java Collections API

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 2

Page 3: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

1 Abstract Data Types

2 The List ADT

3 Lists in the Java Collections API

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 3

Page 4: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Abstract Data Types

What is an ADT?

A set of objects together with a set of operations involving them

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 4

Page 5: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Abstract Data Types

What is an ADT?

A set of objects together with a set of operations involving them

Inside and Outside

ADTs allow for a clear separation of the use of data objects(outside view), and their implementation (inside view)

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 5

Page 6: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Abstract Data Types

What is an ADT?

A set of objects together with a set of operations involving them

Inside and Outside

ADTs allow for a clear separation of the use of data objects(outside view), and their implementation (inside view)

Outside view in Java

ADTs are represented in Java by interfaces that define theoperations on its members

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 6

Page 7: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Abstract Data Types

What is an ADT?

A set of objects together with a set of operations involving them

Inside and Outside

ADTs allow for a clear separation of the use of data objects(outside view), and their implementation (inside view)

Outside view in Java

ADTs are represented in Java by interfaces that define theoperations on its members

Inside view in Java

ADTs programmed through classes that implement interfaces

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 7

Page 8: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

1 Abstract Data Types

2 The List ADTSimple Array Implementation of ListsSimple Linked Lists

3 Lists in the Java Collections API

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 8

Page 9: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Characteristics of Lists

Like in arrays, the elements of a list are numbered usingindices from 0 to the current size of the list minus one:

A0, A1, A2, . . . , AN−1

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 9

Page 10: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Characteristics of Lists

Like in arrays, the elements of a list are numbered usingindices from 0 to the current size of the list minus one:

A0, A1, A2, . . . , AN−1

The position if element Ai is the integer i

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 10

Page 11: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Characteristics of Lists

Like in arrays, the elements of a list are numbered usingindices from 0 to the current size of the list minus one:

A0, A1, A2, . . . , AN−1

The position if element Ai is the integer i

But: Arrays have fixed size, whereas lists start out empty,and then grow and shrink

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 11

Page 12: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Characteristics of Lists

Like in arrays, the elements of a list are numbered usingindices from 0 to the current size of the list minus one:

A0, A1, A2, . . . , AN−1

The position if element Ai is the integer i

But: Arrays have fixed size, whereas lists start out empty,and then grow and shrink

Operations: Accessing and changing elements (like inarrays), plus adding and removing elements

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 12

Page 13: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Operations on lists

makeEmpty: create an empty list

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 13

Page 14: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Operations on lists

makeEmpty: create an empty list

get(i): retrieve element at given position i; no change oflist

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 14

Page 15: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Operations on lists

makeEmpty: create an empty list

get(i): retrieve element at given position i; no change oflist

set(i,x): change element at given position i to newvalue; no change of rest of list

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 15

Page 16: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Operations on lists

makeEmpty: create an empty list

get(i): retrieve element at given position i; no change oflist

set(i,x): change element at given position i to newvalue; no change of rest of list

add(i,x): insert element at given position i; followingelements will change their index

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 16

Page 17: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Operations on lists

makeEmpty: create an empty list

get(i): retrieve element at given position i; no change oflist

set(i,x): change element at given position i to newvalue; no change of rest of list

add(i,x): insert element at given position i; followingelements will change their index

remove(i): remove element from given position;following elements will change their index

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 17

Page 18: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

The List ADT

Operations on lists

makeEmpty: create an empty list

get(i): retrieve element at given position i; no change oflist

set(i,x): change element at given position i to newvalue; no change of rest of list

add(i,x): insert element at given position i; followingelements will change their index

remove(i): remove element from given position;following elements will change their index

How can we implement such a list?

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 18

Page 19: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Array Implementation

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 19

Page 20: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Array Implementation

Question

How can we know how large an array to start with?

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 20

Page 21: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Array Implementation

Question

How can we know how large an array to start with?

Problem

What do we do when we want to insert an element and nospace is left?

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 21

Page 22: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Array Implementation

Question

How can we know how large an array to start with?

Problem

What do we do when we want to insert an element and nospace is left?

Idea

Start out with a fixed size array and store the elements startingat position 0.

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 22

Page 23: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Array Implementation

Question

How can we know how large an array to start with?

Problem

What do we do when we want to insert an element and nospace is left?

Idea

Start out with a fixed size array and store the elements startingat position 0. When the array size is exceeded, create an arrayof double its size, and copy the elements over.

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 23

Page 24: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

In Detail: Doubling and Copying Array

i n t [ ] a r r = new i n t [ 1 0 ] ;. . ./ / La te r on we decide a r r needs to be l a r g e ri n t [ ] newArr = new i n t [ a r r . l eng th ∗ 2 ] ;for ( i n t i = 0 ; i < a r r . l eng th ; i ++)

newArr [ i ] = a r r [ i ] ;a r r = newArr ;

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 24

Page 25: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Quick Analysis: Array Implementation of Lists

get(i) and set(i,x) require

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 25

Page 26: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Quick Analysis: Array Implementation of Lists

get(i) and set(i,x) require O(1) time (array access)

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 26

Page 27: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Quick Analysis: Array Implementation of Lists

get(i) and set(i,x) require O(1) time (array access)add(i,x) and remove(i) require:

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 27

Page 28: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Quick Analysis: Array Implementation of Lists

get(i) and set(i,x) require O(1) time (array access)add(i,x) and remove(i) require:

O(N) if i is low (for example 0), and

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 28

Page 29: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Quick Analysis: Array Implementation of Lists

get(i) and set(i,x) require O(1) time (array access)add(i,x) and remove(i) require:

O(N) if i is low (for example 0), andO(1) if i is high (for example N)

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 29

Page 30: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Page 31: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Simple Linked Lists

Idea

Build a chain of objects called nodes, where each has areference to the next one

Pros and Cons

No need for copying, but now access is expensive

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 31

Page 32: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Removing and Adding Elements

Example linked list:

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 32

Page 33: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Removing and Adding Elements

Example linked list:

Removing an element:

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 33

Page 34: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Removing and Adding Elements

Example linked list:

Removing an element:

Inserting an element:

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 34

Page 35: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Quick Analysis: Array Implementation of Lists

get(i), set(i,x), add(i,x) and remove(i) require:

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 35

Page 36: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Quick Analysis: Array Implementation of Lists

get(i), set(i,x), add(i,x) and remove(i) require:

O(1) if i is low (for example 0), and

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 36

Page 37: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Quick Analysis: Array Implementation of Lists

get(i), set(i,x), add(i,x) and remove(i) require:

O(1) if i is low (for example 0), and

O(N) if i is high (for example N)

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 37

Page 38: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Quick Analysis: Array Implementation of Lists

get(i), set(i,x), add(i,x) and remove(i) require:

O(1) if i is low (for example 0), and

O(N) if i is high (for example N)

Question

Can we improve the runtime for insertion at the end of the list?

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 38

Page 39: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Optimization: Doubly-linked Lists

Idea

Keep track of the current end of the chain, to add a new node,using a last field

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 39

Page 40: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Optimization: Doubly-linked Lists

Idea

Keep track of the current end of the chain, to add a new node,using a last field

Problem

How to update last field when removing last element?

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 40

Page 41: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Optimization: Doubly-linked Lists

Idea

Keep track of the current end of the chain, to add a new node,using a last field

Problem

How to update last field when removing last element?

Solution

Keep track of the previous node

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 41

Page 42: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Simple Array Implementation of ListsSimple Linked Lists

Optimization: Doubly-linked Lists

Idea

Keep track of the current end of the chain, to add a new node,using a last field

Problem

How to update last field when removing last element?

Solution

Keep track of the previous node

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 42

Page 43: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

1 Abstract Data Types

2 The List ADT

3 Lists in the Java Collections APICollection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 43

Page 44: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Java’s Collections API

API

An “API” (Application Programming Interface) is a library ofinterfaces and classes that support the programming ofapplications

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 44

Page 45: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Java’s Collections API

API

An “API” (Application Programming Interface) is a library ofinterfaces and classes that support the programming ofapplications

Java’s Collections API

API for collections, sets of identically-typed objects

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 45

Page 46: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Java’s Collections API

API

An “API” (Application Programming Interface) is a library ofinterfaces and classes that support the programming ofapplications

Java’s Collections API

API for collections, sets of identically-typed objects

Purpose

Provides interfaces and implementations of the most commonlyused collections, including most of the data structures studiedin CS1102S!

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 46

Page 47: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Excursion: Generic Types in Java

Remember IntList from crash course:

public class I n t L i s t{ . . .

public s t a t i c I n t L i s t cons ( i n t i ,I n t L i s t l i s t ) { . . . }

public s t a t i c I n t L i s t n i l = . . . ;public s t a t i c i n t car ( I n t L i s t l i s t ) { . . . }public s t a t i c I n t L i s t cdr ( I n t L i s t l i s t ) { . . . }public s t a t i c boolean i s N i l ( I n t L i s t l i s t ) { . . . }

}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 47

Page 48: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Excursion: Generic Types in Java

Such lists can only contain integers! How about lists ofintegers?

public class I n t L i s t L i s t{

public s t a t i c I n t L i s t L i s t cons ( I n t L i s t i ,I n t L i s t L i s t l i s t ) { . }

public s t a t i c I n t L i s t L i s t n i l = . . . ;public s t a t i c I n t L i s t car ( I n t L i s t L i s t l i s t ) { . . . . . }public s t a t i c I n t L i s t L i s t cdr ( I n t L i s t L i s t l i s t ) { . }public s t a t i c boolean i s N i l ( I n t L i s t L i s t l i s t ) { . . . }

}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 48

Page 49: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Tired of writing “boilerplate”?

Problem

For each content type, we need to introduce a new kind of listtype, with identical implementation!

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 49

Page 50: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Tired of writing “boilerplate”?

Problem

For each content type, we need to introduce a new kind of listtype, with identical implementation!

Solution

Introduce generic types: type placeholders that can beinstantiated when a list object is created

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 50

Page 51: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Generic Lists, Scheme Style

public class L i s t <Any> {{

public s t a t i c L i s t <Any>cons(<Any> i , L i s t <Any> l i s t ) { . . . }

public s t a t i c L i s t <Any> n i l = . . . ;public s t a t i c <Any> car ( L i s t <Any> l i s t ) { . . . }public s t a t i c <Any> L i s t cdr ( L i s t <Any> l i s t ) { . }public s t a t i c boolean i s N i l ( L i s t <Any> l i s t ) { . }

}. . .L i s t <In teger > m y l i s t

= L i s t . cons (new I n t ege r ( 5 ) , L i s t . n i l ) ;

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 51

Page 52: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

The Top-level Collection Interface

public inter face Co l l ec t i on <Any>extends I t e r a b l e <Any>

{i n t s ize ( ) ;boolean isEmpty ( ) ;void c l ea r ( ) ;boolean conta ins ( Any x ) ;boolean add ( Any x ) ; / / s i cboolean remove ( Any x ) ; / / s i cjava . u t i l . I t e r a t o r <Any> i t e r a t o r ( ) ;

}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 52

Page 53: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Iterable Objects and Iterators

Requirement of Iterable Interface

Iterable objects must support a method iterator () , whichreturns an iterator of correct type

public inter face Co l l ec t i on <Any>extends I t e r a b l e <Any>

{. . .java . u t i l . I t e r a t o r <Any> i t e r a t o r ( ) ;

}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 53

Page 54: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

What is an Iterator?

public inter face I t e r a t o r <Any> {boolean hasNext ( ) ;Any next ( ) ;void remove ( ) ;

}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 54

Page 55: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Iterable Objects Provide Enhanced for-loop

public s t a t i c <Any> voidp r i n t ( Co l l ec t i on <Any> c o l l ) {

for ( Any i tem : c o l l )System . out . p r i n t l n ( i tem ) ;

}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 55

Page 56: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Java Compiler Support for Iterators

for ( Any i tem : c o l l )System . out . p r i n t l n ( i tem ) ;

becomes

I t e r a t o r i t r = c o l l . i t e r a t o r ( ) ;while ( i t r . hasNext ( ) ) {

Any i tem = i t r . next ( ) ;System . out . p r i n t l n ( i tem ) ;

} }

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 56

Page 57: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

The List Interface in Collection API

public inter face L i s t <Any>extends Co l l ec t i on <Any>

{Any get ( i n t i dx ) ;Any set ( i n t idx , Any newVal ) ;void add ( i n t idx , Any x ) ;void remove ( i n t i dx ) ;

L i s t I t e r a t o r <Any> l i s t I t e r a t o r ( i n t pos ) ;}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 57

Page 58: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

ListIterators

Idea

Provide, in addition to iterating forward also iterating backwardand in addition to removal of an entry also addition andchanging of an entry

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 58

Page 59: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

ListIterators

public inter face L i s t I t e r a t o r <Any>extends I t e r a t o r <Any>

{boolean hasPrevious ( ) ;Any prev ious ( ) ;void add ( Any x ) ;void set ( Any newVal ) ;

}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 59

Page 60: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

ArrayList and LinkedList

public class Ar rayL i s t <Any>implements L i s t <Any> { . . . }

public class L inkedL is t <Any>implements L i s t <Any> { . . . }

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 60

Page 61: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

Example: Remove Even Elements

Task

In a given list of Integer, remove all even integers, withoutcopying the list (in-place operation)

A r rayL i s t <In teger > myArrayLis t = . . . ;L inkedL is t <In teger > myLinkedList = . . . ;removeEvens ( myArrayLis t ) ;removeEvens ( myLinkedList ) ;

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 61

Page 62: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

ADT in Action

Ar rayL i s t <In teger > myArrayLis t = . . . ;L inkedL is t <In teger > myLinkedList = . . . ;removeEvens ( myArrayLis t ) ;removeEvens ( myLinkedList ) ;

Observation

Both ArrayList and LinkedList implement the interface List . Wecan define removeEvens(...) in terms of List operations!

Inside and Outside

The same function removeEvens behaves differently formyLinkedList than for myArrayList!

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 62

Page 63: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: First Version

public s t a t i c void removeEvensVer1 (L i s t <In teger > l s t ) {

i n t i = 0 ;while ( i < l s t . s i ze ( ) )

i f ( l s t . get ( i ) % 2 == 0 )l s t . remove ( i ) ;

elsei ++;

}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 63

Page 64: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: First Version

public s t a t i c void removeEvensVer1 (L i s t <In teger > l s t ) {

i n t i = 0 ;while ( i < l s t . s i ze ( ) )

i f ( l s t . get ( i ) % 2 == 0 )l s t . remove ( i ) ;

elsei ++;

}

Runtime for removeEvensVer1(myArrayList):Runtime for removeEvensVer1(myLinkedList):

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 64

Page 65: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: First Version

public s t a t i c void removeEvensVer1 (L i s t <In teger > l s t ) {

i n t i = 0 ;while ( i < l s t . s i ze ( ) )

i f ( l s t . get ( i ) % 2 == 0 )l s t . remove ( i ) ;

elsei ++;

}

Runtime for removeEvensVer1(myArrayList): O(N2)Runtime for removeEvensVer1(myLinkedList):

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 65

Page 66: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: First Version

public s t a t i c void removeEvensVer1 (L i s t <In teger > l s t ) {

i n t i = 0 ;while ( i < l s t . s i ze ( ) )

i f ( l s t . get ( i ) % 2 == 0 )l s t . remove ( i ) ;

elsei ++;

}

Runtime for removeEvensVer1(myArrayList): O(N2)Runtime for removeEvensVer1(myLinkedList): O(N2)

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 66

Page 67: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: Second Version

Idea

Use an iterator to go through the list, and remove elementwhen found to be even

public s t a t i c void removeEvensVer2 (L i s t <In teger > l s t ) {

for ( I n tege r x : l s t )i f ( x % 2 == 0 )

l s t . remove ( x ) ;}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 67

Page 68: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: Second Version

Idea

Use an iterator to go through the list, and remove elementwhen found to be even

public s t a t i c void removeEvensVer2 (L i s t <In teger > l s t ) {

for ( I n tege r x : l s t )i f ( x % 2 == 0 )

l s t . remove ( x ) ;}

Runtime for removeEvensVer2(myArrayList): runtime error!Runtime for removeEvensVer2(myLinkedList): runtime error!

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 68

Page 69: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: Third Version

Idea

Use the iterator’s remove operation!

public s t a t i c void removeEvensVer3 (L i s t <In teger > l s t ) {

I t e r a t o r <In teger > i t r = l s t . i t e r a t o r ( ) ;while ( i t r . hasNext ( ) )

i f ( i t r . next ( ) % 2 == 0)i t r . remove ( ) ;

}

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 69

Page 70: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: Third Version

Idea

Use the iterator’s remove operation!

public s t a t i c void removeEvensVer3 (L i s t <In teger > l s t ) {

I t e r a t o r <In teger > i t r = l s t . i t e r a t o r ( ) ;while ( i t r . hasNext ( ) )

i f ( i t r . next ( ) % 2 == 0)i t r . remove ( ) ;

}

Runtime for removeEvensVer3(myArrayList):

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 70

Page 71: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: Third Version

Idea

Use the iterator’s remove operation!

public s t a t i c void removeEvensVer3 (L i s t <In teger > l s t ) {

I t e r a t o r <In teger > i t r = l s t . i t e r a t o r ( ) ;while ( i t r . hasNext ( ) )

i f ( i t r . next ( ) % 2 == 0)i t r . remove ( ) ;

}

Runtime for removeEvensVer3(myArrayList): O(N2)

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 71

Page 72: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: Third Version

Idea

Use the iterator’s remove operation!

public s t a t i c void removeEvensVer3 (L i s t <In teger > l s t ) {

I t e r a t o r <In teger > i t r = l s t . i t e r a t o r ( ) ;while ( i t r . hasNext ( ) )

i f ( i t r . next ( ) % 2 == 0)i t r . remove ( ) ;

}

Runtime for removeEvensVer3(myArrayList): O(N2)Runtime for removeEvensVer3(myLinkedList):

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 72

Page 73: 03 A: Lists, Stacks, and Queues I - comp.nus.edu.sgcs1102s/slides/slides_03_A.color.pdf · Lists in the Java Collections API 03 A: Lists, Stacks, and Queues I CS1102S: Data Structures

Abstract Data TypesThe List ADT

Lists in the Java Collections API

Collection InterfaceIteratorsThe List Interface, ArrayList, and LinkedListListIteratorsExample: Remove Even Elements

In Detail: Third Version

Idea

Use the iterator’s remove operation!

public s t a t i c void removeEvensVer3 (L i s t <In teger > l s t ) {

I t e r a t o r <In teger > i t r = l s t . i t e r a t o r ( ) ;while ( i t r . hasNext ( ) )

i f ( i t r . next ( ) % 2 == 0)i t r . remove ( ) ;

}

Runtime for removeEvensVer3(myArrayList): O(N2)Runtime for removeEvensVer3(myLinkedList): O(N)

CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 73