The List ADT Textbook Sections

Slides:



Advertisements
Similar presentations
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Advertisements

Chapter 22 Implementing lists: linked implementations.
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Introduction to Computation and Problem
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Chapter 17 Linked Lists.
Topic 12 The List ADT. 9-2 Objectives Examine list processing and various ordering techniques Define a list abstract data type Examine various list implementations.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Linked List A linked list consists of a number of links, each of which has a reference to the next link. Adding and removing elements in the middle of.
DATA STRUCTURES AND ALGORITHMS Prepared by İnanç TAHRALI
Linked Lists Chapter 4.
List Implementations That Use Arrays
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
ITEC200 Week04 Lists and the Collection Interface.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Data Structures Using C++
Concrete collections in Java library
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.
COSC 1P03 Data Structures and Abstraction 10.1 The List If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping.
Double-Linked Lists and Circular Lists
John Hurley Cal State LA
1 Joe Meehean. Ordered collection of items Not necessarily sorted 0-index (first item is item 0) Abstraction 2 Item 0 Item 1 Item 2 … Item N.
1 Lists A List ADT Types of Lists Lists in Java Collections API Using ordered lists – Tournament Maker Using indexed lists – Josephus Problem Implementing.
Page 1 – Spring 2010Steffen Vissing Andersen Software Development with UML and Java 2 SDJ I2, Spring 2010 Agenda – week 7, 2010 • Pakages • Looking back.
VOORBLAD.
Review Pseudo Code Basic elements of Pseudo code
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
Linked Lists.
© 2012 National Heart Foundation of Australia. Slide 2.
Copyright © 2013 by John Wiley & Sons. All rights reserved. HOW TO CREATE LINKED LISTS FROM SCRATCH CHAPTER Slides by Rick Giles 16 Only Linked List Part.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
25 seconds left…...
Chapter 9: Data Structures I
PSSA Preparation.
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Linked Lists. RHS – SOC 2 Linked lists We can already store collec- tions of objects in arrays and array lists – why would we need other data structures…?
The Java Collections Framework Chapters 7.5. Outline Introduction to the Java Collections Framework Iterators Interfaces, Abstract Classes and Classes.
LinkedList Many slides from Horstmann modified by Dr V.
1/20/03A2-1 CS494 Interfaces and Collection in Java.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
List Interface and Linked List Mrs. Furman March 25, 2010.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
The List ADT Reading: Textbook Sections 3.1 – 3.5
Linked Lists.
Linked Lists, Stacks and Queues Textbook Sections
The List ADT Reading: Textbook Sections 3.1 – 3.5
Collections Framework
Presentation transcript:

The List ADT Textbook Sections 3.1 - 3.4 Lists - I The List ADT Textbook Sections 3.1 - 3.4

List ADT A list is a dynamic ordered tuple of homogeneous elements Ao, A1, A2, …, 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”) dynamic means the list can change over time homogeneous means the elements are all of the same type ordered means that A1+1 follows (or succeeds) Ai and Ai-1 precedes Ai 8/3/2007 CMSC 341 Lists1

Generic Operations on a List create an empty list printList() – 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 isEmpty( ) – returns true if the list has no elements makeEmpty( ) – removes all elements from the list findKth(int k) – returns the element in the specified position The implementations of these operations in a class may have different names than the generic names above 8/3/2007 CMSC 341 Lists1

Simple Array Implementation of a List Use an array to store the elements of the list printList is O(n) findkth,get and set are constant time Insert and delete? Also, arrays have a fixed capacity, but can fix with implementation. int arr[] = new int arr[10]; int newArr[] = new int[arr.length *2]; for(int i = 0; i < arr.length; i++) newArr[i] = arr[i]; arr = newArr; 8/3/2007 CMSC 341 Lists1

Simple Linked List Implementation Deletion 8/3/2007 CMSC 341 Lists1

Linked List Implementation of a List Insertion Notice insert and delete can be constant time if node is inserted at beginning of List; however, findkth is now O(i). 8/3/2007 CMSC 341 Lists1

The List ADT in Java Collections 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 8/3/2007 CMSC 341 Lists1

Methods from the Collections List ADT //from Collection interface int size( ); boolean isEmpty( ); void clear( ); boolean contains( AnyType x ); boolean add( AnyType x ); boolean remove( AnyType x ); java.util.Iterator<AnyType> iterator( ); //from List interface AnyType get( int idx ); AnyType set( int idx, AnyType newVal ); void add( int idx, AnyType x ); void remove( int idx ); ListIterator<AnyType> listIterator(int pos); 8/3/2007 CMSC 341 Lists1

The Iterator<E> Interface 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 hasNext( ); AnyType next( ); void remove( ); 8/3/2007 CMSC 341 Lists1

Using an Iterator to Traverse a Collection public static <AnyType> void print( Collection<AnyType> coll ) { Iterator<AnyType> itr = coll.iterator( ); while( itr.hasNext( ) ){ AnyType item = itr.next( ); System.out.println( item ); } 8/3/2007 CMSC 341 Lists1

The Enhanced for Loop 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 <AnyType> void print( Collection<AnyType> coll ) { for( AnyType item : coll ) System.out.println( item ); } This code is actually changed into the code on the previous slide when it is compiled to byte code. 8/3/2007 CMSC 341 Lists1

The ListIterator<E> Interface The second interface for traversing a Collection is the ListIterator<E> interface. It allows for the bidirectional traversal of a List. boolean hasPrevious( ); AnyType previous( ); void add( AnyType x ); void set( AnyType newVal ); A ListIterator object is returned by invoking the listIterator method on a List. 8/3/2007 CMSC 341 Lists1

Concrete Implementations of the List ADT in the Java Collections API Two concrete implementations of the List API in the Java Collections API with which you are already familiar are: java.util.ArrayList java.util.LinkedList Let’s examine the methods of these concrete classes that were developed at Sun. 8/3/2007 CMSC 341 Lists1

List Operations on an ArrayList<E> Supports constant time for 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) 8/3/2007 CMSC 341 Lists1

List Operations on an ArrayList<E> (cont.) What is the growth rate for the following? insertion at the “beginning” of the list using void add(int index, E element) deletion from the “beginning” of the list using E remove(int index) 8/3/2007 CMSC 341 Lists1

List Operations on a LinkedList<E> Provides doubly linked list implementation 8/3/2007 CMSC 341 Lists1

List Operations on a LinkedList<E> Supports constant time for insertion at the “beginning” of the list using void addFirst(E o) insertion at the “end” of the list using void addLast(E o) deletion from the “beginning” of the list using E removeFirst() deletion from the “end” of the list using E removeLast() Accessing first element of the list using E getFirst() E getLast() 8/3/2007 CMSC 341 Lists1

List Operations on a LinkedList<E> What is the growth rate for the following? access to the “middle” element of the list using E get(int index) 8/3/2007 CMSC 341 Lists1

Example 1 –ArrayList vs. LinkedList What is the running time for an ArrayList versus a LinkedList? public static void makeList1(List<Integer> list, int N) { list.clear(); for(int i = 0; i < N; i++) list.add(i); } O(N) for both 8/3/2007 CMSC 341 Lists1

Example 2 –ArrayList vs. LinkedList What is the running time for an ArrayList versus a LinkedList? public static void makeList2(List<Integer> list, int N) { list.clear()l for(int i = 0; i < N; i++) list.add(0,i); } O(n) for LinkedList but O(n^2) for ArrayList 8/3/2007 CMSC 341 Lists1

Example 3 –ArrayList vs. LinkedList What is the running time for an ArrayList versus a LinkedList? 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; } How can we change this code so the running time for both is the same? O(n) for ArrayList but O(n^2) for LinkedList Use the enhanced for loop 8/3/2007 CMSC 341 Lists1

Example 4 –ArrayList vs. LinkedList What is the running time for an ArrayList versus a LinkedList? public static void removeEvensVer3(List<Integer> lst ) { Iterator<Integer> itr = lst.iterator( ); while( itr.hasNext( ) ) if( itr.next( ) % 2 == 0 ) itr.remove( ); } Removes even numbers in a list. O(n^2) for ArrayList but O(n) for LinkedList 8/3/2007 CMSC 341 Lists1

Implementing Your Own ArrayList What do you need? Store elements in a parameterized array Track number of elements in array (size) and capacity of array public class MyArrayList<AnyType> implements Iterable<AnyType> { private static final int DEFAULT_CAPACITY=10; private int theSize; private AnyType [ ] theItems; 8/3/2007 CMSC 341 Lists1

3. Ability to change capacity of the array public void ensureCapacity( int newCapacity ) { if( newCapacity < theSize ) return; AnyType [ ] old = theItems; theItems = (AnyType []) new Object[ newCapacity ]; for( int i = 0; i < size( ); i++ ) theItems[ i ] = old[ i ]; } 8/3/2007 CMSC 341 Lists1

4. get and set Methods public AnyType get( int idx ) { if( idx < 0 || idx >= size( ) ) throw new ArrayIndexOutOfBoundsException(); return theItems[ idx ]; } public AnyType set( int idx, AnyType newVal ) throw new ArrayIndexOutOfBoundsException( ); AnyType old = theItems[ idx ]; theItems[ idx ] = newVal; return old; 8/3/2007 CMSC 341 Lists1

5. size, isEmpty, and clear Methods public void clear( ){ theSize = 0; ensureCapacity( DEFAULT_CAPACITY ); } public int size( ){ return theSize; public boolean isEmpty( ){ return size( ) == 0; // constructor invokes the clear method public MyArrayList( ){ clear( ); 8/3/2007 CMSC 341 Lists1

6. add Methods public boolean add( AnyType x ){ add( size( ), x ); return true; } public void add( int idx, AnyType x ){ if( theItems.length == size( ) ) ensureCapacity( size( ) * 2 + 1 ); for( int i = theSize; i > idx; i-- ) theItems[ i ] = theItems[ i - 1 ]; theItems[ idx ] = x; theSize++; 8/3/2007 CMSC 341 Lists1

7. remove and iterator Method public AnyType remove( int idx ){ AnyType removedItem = theItems[ idx ]; for( int i = idx; i < size( ) - 1; i++ ) theItems[ i ] = theItems[ i + 1 ]; theSize--; return removedItem; } //required by Iterable<E> interface public java.util.Iterator<AnyType> iterator( ){ return new ArrayListIterator( ); 8/3/2007 CMSC 341 Lists1

8. Iterator class // private inner class for iterator private class ArrayListIterator implements java.util.Iterator<AnyType> { private int current = 0; public boolean hasNext( ) { return current < size( ); } public AnyType next( ) { return theItems[ current++ ]; } public void remove( ) { MyArrayList.this.remove( --current ); } } Implicit reference to outer class method Implicit ref. to outer class data Explicit reference to outer class method 8/3/2007 CMSC 341 Lists1

The Iterator and Java Inner classes The implementation of the Iterator class required an inner class to allow one or more instances of Iterator for one outer class. 8/3/2007 CMSC 341 Lists1

Sorted Lists Suppose we decided that the data in the lists should be stored in sorted order. How would the sorted order be determined? What List code would need to be changed? How would sorting the data affect the performance of Finding an element in the list Inserting an element into the list Removing an element from the list other List functions 8/3/2007 CMSC 341 Lists1