03 A: Lists, Stacks, and Queues I CS1102S: Data Structures and - - PowerPoint PPT Presentation

03 a lists stacks and queues i
SMART_READER_LITE
LIVE PREVIEW

03 A: Lists, Stacks, and Queues I CS1102S: Data Structures and - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

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 26th January, 2010, 17:00 CS1102S: Data Structures and Algorithms 03 A: Lists, Stacks, and Queues I 1

slide-2
SLIDE 2

Abstract Data Types The 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

slide-3
SLIDE 3

Abstract Data Types The 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

slide-4
SLIDE 4

Abstract Data Types The 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 the

  • perations 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 4

slide-5
SLIDE 5

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple Linked Lists

1

Abstract Data Types

2

The List ADT Simple Array Implementation of Lists Simple Linked Lists

3

Lists in the Java Collections API

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

slide-6
SLIDE 6

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple Linked Lists

The List ADT

Characteristics of Lists Like in arrays, the elements of a list are numbered using indices 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 in arrays), plus adding and removing elements

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

slide-7
SLIDE 7

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple Linked Lists

The List ADT

Operations on lists makeEmpty: create an empty list get(i): retrieve element at given position i; no change of list set(i,x): change element at given position i to new value; no change of rest of list add(i,x): insert element at given position i; following elements 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 7

slide-8
SLIDE 8

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple 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 no space is left? Idea Start out with a fixed size array and store the elements starting at position 0. When the array size is exceeded, create an array

  • f double its size, and copy the elements over.

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

slide-9
SLIDE 9

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple Linked Lists

In Detail: Doubling and Copying Array

int [ ] arr = new int [ 1 0 ] ; . . . / / Later on we decide arr needs to be larger int [ ] newArr = new int [ arr . length ∗ 2 ] ; for ( int i = 0; i < arr . length ; i ++) newArr [ i ] = arr [ i ] ; arr = newArr ;

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

slide-10
SLIDE 10

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple 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 O(1) if i is high (for example N)

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

slide-11
SLIDE 11

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple Linked Lists

Simple Linked Lists

Idea Build a chain of objects called nodes, where each has a reference 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 11

slide-12
SLIDE 12

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple 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 12

slide-13
SLIDE 13

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple 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 13

slide-14
SLIDE 14

Abstract Data Types The List ADT Lists in the Java Collections API Simple Array Implementation of Lists Simple 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 14

slide-15
SLIDE 15

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

1

Abstract Data Types

2

The List ADT

3

Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

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

slide-16
SLIDE 16

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

Java’s Collections API

API An “API” (Application Programming Interface) is a library of interfaces and classes that support the programming of applications Java’s Collections API API for collections, sets of identically-typed objects Purpose Provides interfaces and implementations of the most commonly used collections, including most of the data structures studied in CS1102S!

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

slide-17
SLIDE 17

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

Excursion: Generic Types in Java

Remember IntList from crash course: public class I n t L i s t { . . . public static I n t L i s t cons ( int i , I n t L i s t l i s t ) { . . . } public static I n t L i s t n i l = . . . ; public static int car ( I n t L i s t l i s t ) { . . . } public static I n t L i s t cdr ( I n t L i s t l i s t ) { . . . } public static 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 17

slide-18
SLIDE 18

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

Excursion: Generic Types in Java

Such lists can only contain integers! How about lists of integers? public class I n t L i s t L i s t { public static 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 static I n t L i s t L i s t n i l = . . . ; public static I n t L i s t car ( I n t L i s t L i s t l i s t ) { . . . . . } public static 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 static 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 18

slide-19
SLIDE 19

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

Tired of writing “boilerplate”?

Problem For each content type, we need to introduce a new kind of list type, with identical implementation! Solution Introduce generic types: type placeholders that can be instantiated when a list object is created

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

slide-20
SLIDE 20

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

Generic Lists, Scheme Style

public class List <Any> { { public static List <Any> cons(<Any> i , List <Any> l i s t ) { . . . } public static List <Any> n i l = . . . ; public static <Any> car ( List <Any> l i s t ) { . . . } public static <Any> L i s t cdr ( List <Any> l i s t ) { . } public static boolean i s N i l ( List <Any> l i s t ) { . } } . . . List <Integer > mylist = L i s t . cons (new Integer ( 5 ) , L i s t . n i l ) ;

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

slide-21
SLIDE 21

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

The Top-level Collection Interface

public interface Collection <Any> extends Iterable <Any> { int size ( ) ; boolean isEmpty ( ) ; void clear ( ) ; boolean contains ( Any x ) ; boolean add ( Any x ) ; / / sic boolean remove ( Any x ) ; / / sic 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 21

slide-22
SLIDE 22

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

Iterable Objects and Iterators

Requirement of Iterable Interface Iterable objects must support a method iterator (), which returns an iterator of correct type public interface Collection <Any> extends Iterable <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 22

slide-23
SLIDE 23

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

What is an Iterator?

public interface 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 23

slide-24
SLIDE 24

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

Iterable Objects Provide Enhanced for-loop

public static <Any> void p r i n t ( Collection <Any> c o l l ) { for ( Any item : c o l l ) System . out . p r i n t l n ( item ) ; }

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

slide-25
SLIDE 25

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

Java Compiler Support for Iterators

for ( Any item : c o l l ) System . out . p r i n t l n ( item ) ; 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 item = i t r . next ( ) ; System . out . p r i n t l n ( item ) ; } }

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

slide-26
SLIDE 26

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

The List Interface in Collection API

public interface List <Any> extends Collection <Any> { Any get ( int idx ) ; Any set ( int idx , Any newVal ) ; void add ( int idx , Any x ) ; void remove ( int idx ) ; 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 ( int pos ) ; }

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

slide-27
SLIDE 27

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

ListIterators

Idea Provide, in addition to iterating forward also iterating backward and in addition to removal of an entry also addition and changing of an entry (a) next returns 5, previous is illegal, add places item before 5 (b) next returns 8, previous returns 5, add places item between 5 and 8 (c) next is illegal, previous returns 9, add places item after 9

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

slide-28
SLIDE 28

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

ListIterators

public interface 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 previous ( ) ; void add ( Any x ) ; void set ( Any newVal ) ; }

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

slide-29
SLIDE 29

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

ArrayList and LinkedList

public class ArrayList <Any> implements List <Any> { . . . } public class LinkedList <Any> implements List <Any> { . . . }

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

slide-30
SLIDE 30

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

Example: Remove Even Elements

Task In a given list of Integer, remove all even integers, without copying the list (in-place operation) ArrayList <Integer > myArrayList = . . . ; LinkedList <Integer > myLinkedList = . . . ; removeEvens ( myArrayList ) ; removeEvens ( myLinkedList ) ;

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

slide-31
SLIDE 31

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

ADT in Action

ArrayList <Integer > myArrayList = . . . ; LinkedList <Integer > myLinkedList = . . . ; removeEvens ( myArrayList ) ; removeEvens ( myLinkedList ) ; Observation Both ArrayList and LinkedList implement the interface List. We can define removeEvens(...) in terms of List operations! Inside and Outside The same function removeEvens behaves differently for myLinkedList than for myArrayList!

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

slide-32
SLIDE 32

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: First Version

public static void removeEvensVer1 ( List <Integer > l s t ) { int i = 0; while ( i < l s t . size ( ) ) i f ( l s t . get ( i ) % 2 == 0 ) l s t . remove ( i ) ; else i ++; }

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

slide-33
SLIDE 33

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: First Version

public static void removeEvensVer1 ( List <Integer > l s t ) { int i = 0; while ( i < l s t . size ( ) ) i f ( l s t . get ( i ) % 2 == 0 ) l s t . remove ( i ) ; else i ++; } Runtime for removeEvensVer1(myArrayList): Runtime for removeEvensVer1(myLinkedList):

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

slide-34
SLIDE 34

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: First Version

public static void removeEvensVer1 ( List <Integer > l s t ) { int i = 0; while ( i < l s t . size ( ) ) i f ( l s t . get ( i ) % 2 == 0 ) l s t . remove ( i ) ; else i ++; } Runtime for removeEvensVer1(myArrayList): O(N2) Runtime for removeEvensVer1(myLinkedList):

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

slide-35
SLIDE 35

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: First Version

public static void removeEvensVer1 ( List <Integer > l s t ) { int i = 0; while ( i < l s t . size ( ) ) i f ( l s t . get ( i ) % 2 == 0 ) l s t . remove ( i ) ; else i ++; } Runtime for removeEvensVer1(myArrayList): O(N2) Runtime for removeEvensVer1(myLinkedList): O(N2)

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

slide-36
SLIDE 36

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: Second Version

Idea Use an iterator to go through the list, and remove element when found to be even public static void removeEvensVer2 ( List <Integer > l s t ) { for ( Integer 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 36

slide-37
SLIDE 37

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: Second Version

Idea Use an iterator to go through the list, and remove element when found to be even public static void removeEvensVer2 ( List <Integer > l s t ) { for ( Integer 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 37

slide-38
SLIDE 38

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: Third Version

Idea Use the iterator’s remove operation! public static void removeEvensVer3 ( List <Integer > l s t ) { I t e r a t o r <Integer > 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 38

slide-39
SLIDE 39

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: Third Version

Idea Use the iterator’s remove operation! public static void removeEvensVer3 ( List <Integer > l s t ) { I t e r a t o r <Integer > 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 39

slide-40
SLIDE 40

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: Third Version

Idea Use the iterator’s remove operation! public static void removeEvensVer3 ( List <Integer > l s t ) { I t e r a t o r <Integer > 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 40

slide-41
SLIDE 41

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: Third Version

Idea Use the iterator’s remove operation! public static void removeEvensVer3 ( List <Integer > l s t ) { I t e r a t o r <Integer > 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 41

slide-42
SLIDE 42

Abstract Data Types The List ADT Lists in the Java Collections API Collection Interface Iterators The List Interface, ArrayList, and LinkedList ListIterators Example: Remove Even Elements

In Detail: Third Version

Idea Use the iterator’s remove operation! public static void removeEvensVer3 ( List <Integer > l s t ) { I t e r a t o r <Integer > 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 42