1 / 20

CS203 Programming with Data Structures Lists and Iterators

CS203 Programming with Data Structures Lists and Iterators. Chengyu Sun California State University, Los Angeles. Data Structures. A bstract D ata T ype (ADT) A collection of data and a set of operations that can be performed on the data

Download Presentation

CS203 Programming with Data Structures Lists and Iterators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS203 Programming with Data StructuresLists and Iterators Chengyu Sun California State University, Los Angeles

  2. Data Structures • Abstract Data Type (ADT) • A collection of data and a set of operations that can be performed on the data • Abstract – operations are defined, but how to implements these operations are not

  3. Some Simple ADTs • List • Queue • Stack • Tree • … • Just look under java.util package

  4. Why Do We Study Data Structures? • Common to all languages, not just Java • Building blocks for more complex algorithms • Programming techniques • Complexity analysis and performance tradeoffs

  5. List • A ordered collection of objects beef beer biscuits broccoli … …

  6. Insert Remove Get Search Clear Size Output List Operations

  7. The List Interface • http://sun.calstatela.edu/~cysun/www/teaching/cs203/extras/List.java

  8. Array Implementation public class ArrayList implements List { private Object elements[]; … }

  9. Performance Concerns of ArrayList • Which operations are efficient?? • Which are not?? • How much space are we wasting?? • What can we do about it??

  10. Linked List beef beer biscuits broccoli

  11. ListNode class ListNode { Object data; ListNode next; … }

  12. Implementation Considerations • How do we insert at the front of the list?? • How do we remove the first element of the list?? • How do we remove the last element of the list??

  13. Head and Tail head beef beer biscuits broccoli tail

  14. Performance Concerns of LinkedList • What wrong with this code?? // assume list is a linked list of Integers int sum = 0 for( int i=0 ; i < list.size() ; ++i ) sum += (Integer) list.get(i);

  15. Iterator • Iterates though each element of a collection … • … without exposing the internal of the collection class

  16. The Iterator Interface public interface Iterator { public boolean hasNext(); public Object next(); }

  17. The ListIterator Interface public interface ListIterator extends Iterator { public boolean hasPrevious(); public Object previous(); }

  18. ListIterator Operations ListIterator it = list.iterator(); it.hasPrevious(); // false it.hasNext(); // true it.next(); // “beef” it.next(); // “beer” it.previous(); // “beer” it.next(); // “beer” beef beer biscuits broccoli

  19. LinkedListIterator • An iterator class for LinkedList

  20. Refactoring List Classes List Iterator iterator() implements extends AbstractList ListIterator extends implements ArrayList LinkedList ArrayListIterator ListNode LinkedListIterator

More Related