Lists The List ADT Reading Textbook Sections 3
Lists The List ADT Reading: Textbook Sections 3. 1 – 3. 5
List ADT n n n A list is a dynamic ordered tuple of homogeneous elements Ao, A 1, A 2, …, AN-1 where Ai is the i-th element of the list The position of element Ai is i; positions range from 0 to N-1 inclusive The size of a list is N ( a list with no elements is called an “empty list”) CMSC 341 Lists 1 2
Generic Operations on a List n n n n n create an empty list print. List() – prints all elements in the list construct a (deep) copy of a list find(x) – returns the position of the first occurrence of x remove(x) – removes x from the list if present insert(x, position) – inserts x into the list at the specified position is. Empty( ) – returns true if the list has no elements make. Empty( ) – removes all elements from the list find. Kth(int k) – returns the element in the specified position CMSC 341 Lists 1 3
Simple Array Implementation of a List n Use an array to store the elements of the list q q n Complexity for print. List: findkth, get and set: Also, arrays have a fixed capacity, but can fix with implementation. int arr[] = new int arr[10]; int new. Arr[] = new int[arr. length *2]; for(int i = 0; i < arr. length; i++) new. Arr[i] = arr[i]; arr = new. Arr; CMSC 341 Lists 1 4
Deletion Linked List Deletion A 1 -> next =A 2 -> next; CMSC 341 Lists 1 5
Insertion Notice insert and delete can be constant time if node is inserted at beginning of List; however, findkth is now O(i). CMSC 341 Lists 1 6
The List ADT in Java Collections n n n The List ADT is one of the data structures implemented in the Java Collections API. A list is abstracted using an inheritance hierarchy that stems from the Collection<E> interface , List<E>Interface in the java. util package and from the Iterable<E> interface in the java. lang package. The combination of these interfaces provides a uniform public interface for all Lists in Java CMSC 341 Lists 1 7
Methods from the Collections List //from Collection interface ADT int size( ); boolean is. Empty( ); void clear( ); boolean contains( Any. Type x ); boolean add( Any. Type x ); boolean remove( Any. Type x ); java. util. Iterator<Any. Type> iterator( ); //from List interface Any. Type get( int idx ); Any. Type set( int idx, Any. Type new. Val ); void add( int idx, Any. Type x ); void remove( int idx ); List. Iterator<Any. Type> list. Iterator(int pos); CMSC 341 Lists 1 8
The Iterator<E> Interface n n The Collections framework provides two very useful interfaces for traversing a Collection. The first is the Iterator<E> interface. When the iterator method is called on a Collection, it returns an Iterator object which has the following methods for traversing the Collection. boolean has. Next( ); Any. Type next( ); void remove( ); CMSC 341 Lists 1 9
Using an Iterator to Traverse a Collection public static <Any. Type> void print( Collection<Any. Type> coll ) { Iterator<Any. Type> itr = coll. iterator( ); while( itr. has. Next( ) ){ Any. Type item = itr. next( ); System. out. println( item ); } } CMSC 341 Lists 1 10
The Enhanced for Loop n The enhanced for loop in Java actually calls the iterator method when traversing a Collection and uses the Iterator to traverse the Collection when translated into byte code. public static <Any. Type> void print( Collection<Any. Type> coll ) { for( Any. Type item : coll ) System. out. println( item ); } CMSC 341 Lists 1 11
The List. Iterator<E> Interface The second interface for traversing a Collection is the List. Iterator<E> interface. It allows for the bidirectional traversal of a List. boolean has. Previous( ); Any. Type previous( ); void add( Any. Type x ); void set( Any. Type new. Val ); n n A List. Iterator object is returned by invoking the list. Iterator method on a List. CMSC 341 Lists 1 12
Concrete Implementations of the List ADT in the Java Collections API n Two concrete implementations of the List API in the Java Collections API with which you are already familiar are: q q n java. util. Array. List java. util. Linked. List Let’s examine the methods of these concrete classes that were developed at Sun. CMSC 341 Lists 1 13
List Operations on an Array. List<E> n Supports constant time for q q insertion at the “end” of the list using void add(E element) deletion from the “end” of the list using E remove(int index) access to any element of the list using E get(int index) changing value of any element of the list using E set(int index, E element) CMSC 341 Lists 1 14
List Operations on an Array. List<E> (cont. ) n What is the growth rate for the following? q insertion at the “beginning” of the list using void add(int index, E element) O(N) Insertion at the “end” of the list O(C) q deletion from the “beginning” of the list using E remove(int index) O(N) Deletion from the “end” of the list O(C) CMSC 341 Lists 1 15
List Operations on a Linked. List<E> n Provides doubly linked list implementation CMSC 341 Lists 1 16
List Operations on a Linked. List<E> n Supports constant time for q insertion at the “beginning” of the list using void add. First(E o) q q insertion at the “end” of the list using void add. Last(E o) deletion from the “beginning” of the list using E remove. First() deletion from the “end” of the list using E remove. Last() Accessing first element of the list using E get. First() q Accessing first element of the list using E get. Last() CMSC 341 Lists 1 17
List Operations on a Linked. List<E> n What is the running time for the following? q access to the “middle” element of the list using E get(int index) Linked. List: O(N) Array. List: O(1) CMSC 341 Lists 1 18
Example 1 –Array. List vs. Linked. List n What is the average running time for an Array. List versus a Linked. List? public static void make. List 1(List<Integer> list, int N) { list. clear(); for(int i = 0; i < N; i++) list. add(i); } Array. List: O(N); Linked. List: O(N) CMSC 341 Lists 1 19
Example 2 –Array. List vs. Linked. List What is the average running time for an Array. List versus a Linked. List? public static void make. List 2(List<Integer> list, int N) { list. clear(); for(int i = 0; i < N; i++) list. add(0, i); } Array. List: O(N^2); Linked. List: O(N) n CMSC 341 Lists 1 20
Example 3 –Array. List vs. What is the running time for an Array. List versus a Linked. List? Linked. List n public static int sum(List<Integer> list, int N) { int total = 0; for(int i = 0; i < N ; i++) total += list. get(i); return total; } Array. List: O(N), Linked. List: O(N^2) n How can we change this code so the running time for both is the same? : use iterator in Linked. List. CMSC 341 Lists 1 21
Example 4 –Array. List vs. Linked. List What is the running time for an Array. List versus a Linked. List? n public static void remove. Evens. Ver 3(List<Integer> lst ) { Iterator<Integer> itr = lst. iterator( ); while( itr. has. Next( ) ) if( itr. next( ) % 2 == 0 ) itr. remove( ); } Array. List: O(N^2): Linked. List: O(N) CMSC 341 Lists 1 22
Implementing Your Own Array. List What do you need? n 1. 2. Store elements in a parameterized array Track number of elements in array (size) and capacity of array public class My. Array. List<Any. Type> implements Iterable<Any. Type> { private static final int DEFAULT_CAPACITY=10; private int the. Size; private Any. Type [ ] the. Items; CMSC 341 Lists 1 23
3. Ability to change capacity of the array public void ensure. Capacity( int new. Capacity ) { if( new. Capacity < the. Size ) return; Any. Type [ ] old = the. Items; the. Items = (Any. Type []) new Object[ new. Capacity]; for( int i = 0; i < size( ); i++ ) the. Items[ i ] = old[ i ]; } CMSC 341 Lists 1 24
4. get and set Methods public Any. Type get( int idx ) { if( idx < 0 || idx >= size( ) ) throw new Array. Index. Out. Of. Bounds. Exception(); return the. Items[ idx ]; } public Any. Type set( int idx, Any. Type new. Val ) { if( idx < 0 || idx >= size( ) ) throw new Array. Index. Out. Of. Bounds. Exception( ); Any. Type old = the. Items[ idx ]; the. Items[ idx ] = new. Val; return old; } CMSC 341 Lists 1 25
5. size, is. Empty, and clear Methods public void clear( ){ the. Size = 0; ensure. Capacity( DEFAULT_CAPACITY ); } public int size( ){ return the. Size; } public boolean is. Empty( ){ return size( ) == 0; } // constructor invokes the clear method public My. Array. List( ){ clear( ); } CMSC 341 Lists 1 26
6. add Methods /** Add to the end of the list */ public boolean add( Any. Type x ){ add( size( ), x ); return true; } /** Add item x to the index of idx in the list */ public void add( int idx, Any. Type x ){ if( the. Items. length == size( ) ) ensure. Capacity( size( ) * 2 + 1 ); for( int i = the. Size; i > idx; i-- ) the. Items[ i ] = the. Items[ i - 1 ]; the. Items[ idx ] = x; the. Size++; } CMSC 341 Lists 1 27
7. remove and iterator Method /** Remove by index */ public Any. Type remove( int idx ){ Any. Type removed. Item = the. Items[ idx ]; for( int i = idx; i < size( ) - 1; i++ ) the. Items[ i ] = the. Items[ i + 1 ]; the. Size--; return removed. Item; } /**required by Iterable<E> interface */ public java. util. Iterator<Any. Type> iterator( ){ return new Array. List. Iterator( ); } CMSC 341 Lists 1 28
8. Iterator class // private inner class for iterator private class Array. List. Iterator implements java. util. Iterator<Any. Type> { private int current = 0; Implicit reference to outer class method public boolean has. Next( ) { return current < size( ); } Implicit ref. to outer class data public Any. Type next( ) { return the. Items[ current++ ]; } public void remove( ) { My. Array. List. this. remove( --current ); } } Explicit reference to } outer class method CMSC 341 Lists 1 29
The Iterator and Java Inner classes n The implementation of the Iterator class required an inner class to allow one or more instances of Iterator for one outer class. CMSC 341 Lists 1 30
Sorted Lists n n n Suppose we decided that the data in the lists should be stored in sorted order. What List code would need to be changed? How would sorting the data affect the performance of q q Finding an element in the list: yes: best O(lgn) Inserting an element into the list: yes new: O(n); old: O(1) Removing an element from the list: yes; depends on the find() other List functions CMSC 341 Lists 1 31
n Supl. Material: Inner class CMSC 341 Lists 32
class Outer. Class { private String some. String = “this belongs to the outer. Class”; class Inner. Class { private String some. String 2 = “this belongs to the inner. Class”; public do. Something. Here { System. println(some. String 2); } } } Class My. Test { main () { Outer. Class my. OC; // will this compile? - yes Inner. Class my. IC: // will this compile? - no Outer. Class. Inner. Class my. IC 2; // yes this works. my. IC 2. do. Something. Here(); } } CMSC 341 Lists 33
- Slides: 33