8 List and Iterator ADTs  List concepts  List applications  A list ADT: requirements, contract  Iterators  Implementations of lists: using arrays.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Lists: An internal look
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)
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.
The List ADT Textbook Sections
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++)
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks.
Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists.
7 Queue ADTs  Queue concepts  Queue applications  A queue ADT: requirements, contract  Implementations of queues: using arrays and linked-lists  Queues.
9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
7-1 7 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
4 Linked-List Data Structures  Linked-lists: singly-linked-lists, doubly-linked-lists  Insertion  Deletion  Searching © 2008 David A Watt, University.
Data Structures 4 Lists and Linked List Data Structures Prof A Alkhorabi.
Stacks. 2 Outline and Reading The Stack ADT (§4.2.1) Applications of Stacks (§4.2.3) Array-based implementation (§4.2.2) Growable array-based stack.
1 L41 Collections (1). 2 OBJECTIVES  What collections are.  To use class Arrays for array manipulations.  To use the collections framework (prepackaged.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CM0551 Exam Prep. What are an algorithm’s time and space complexity? (2 marks) Answer: The growth rate of the algorithm’s time requirement and the computer.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
2-1 Week 2 Sets Set concepts (you should know these!) Set applications. A set ADT (abstract data type): requirements, contract. Implementations of sets:
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
7.2 Priority Queue ADTs Priority queue concepts
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 16 – Basic Data Structures.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
11 Map ADTs  Map concepts  Map applications  A map ADT: requirements, contract.  Implementations of maps: using key-indexed arrays, entry arrays, linked-lists,
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
9-1 9 Set ADTs Set concepts. Set applications. A set ADT: requirements, contract. Implementations of sets: using arrays, linked lists, boolean arrays.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
List Interface and Linked List Mrs. Furman March 25, 2010.
Queue. Avoid confusion Britain Italy 6 Applications of Queues Direct applications –Waiting lists, bureaucracy –Access to shared resources (e.g.,
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
9 Set ADTs  Set concepts  Set applications  A set ADT: requirements, contract  Implementations of sets: using member arrays, linked lists, boolean.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Course: Programming II - Abstract Data Types HeapsSlide Number 1 The ADT Heap So far we have seen the following sorting types : 1) Linked List sort by.
Click to edit Master text styles Stacks Data Structure.
Linked Lists, Queues, Stacks
Lists and Iterators 5/3/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
11 Map ADTs Map concepts. Map applications.
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Ch7. List and Iterator ADTs
Stacks.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Lists and Iterators 3/9/15 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Queue.
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Stacks.
" A list is only as strong as its weakest link. " - Donald Knuth
Dynamic Data Structures and Generics
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Recall What is a Data Structure Very Fundamental Data Structures
Copyright © Aiman Hanna All rights reserved
8 List ADTs List concepts. List applications.
Presentation transcript:

8 List and Iterator ADTs  List concepts  List applications  A list ADT: requirements, contract  Iterators  Implementations of lists: using arrays and linked-lists  Lists in the Java class library © 2008 David A Watt, University of Glasgow Algorithms & Data Structures (M)

8-2 List concepts (1)  A list is a sequence of elements, in a fixed order. Elements can added/removed at any position.  Elements are in positions 0 (leftmost), 1, 2, ….  The size (or length) of a list is the number of elements.  The concatenation of lists l 1 and l 2 is a list containing all elements of l 1 followed by all elements of l 2.  We can traverse a list (or iterate over the list), i.e., visit each of the list’s elements in turn.

8-3 List concepts (2)  Mathematical notation for lists: –«x 0, x 1, …, x n – 1 » is a list of length n, whose elements are x 0, x 1, …, x n – 1 (in that order). –« » is the empty list. –Note: We can use this notation in algorithms, but it is not supported by Java.

8-4 Example: lists and concatenation  A list of integers: fibonacci = «1, 1, 2, 3, 5, 8, 13, 21, 34»  A list of airports: tour = «GLA, LHR, CDG, GLA»  Lists of words: hamlet1 = «‘to’, ‘be’, ‘or’, ‘not’, ‘to’, ‘be’» hamlet2 = «‘that’, ‘is’, ‘the’, ‘question’»  Concatenation of those lists of words: «‘to’, ‘be’, ‘or’, ‘not’, ‘to’, ‘be’, ‘that’, ‘is’, ‘the’, ‘question’»

8-5 Lists vs linked-lists  Do not confuse the list abstract data type with linked-list data structures.  A list ADT can be implemented using different data structures (arrays, linked-lists).  Conversely, linked-list data structures can be used to implement many different ADTs (e.g., stacks, queues, lists, sets).

8-6 List applications  A sentence is a list of words. –The words are in the order they are read or spoken.  An itinerary is a list of places visited on a tour. –The places are in the order they are visited.  A log is a list of event records (e.g., equipment faults). –The event records are in time order.

8-7 Example: simple text editor (1)  Consider a very simple text editor that supports insertion and deletion of complete lines only.  The user can load text from a file, or save the text to a file.  The user can select any line of the text: –directly (by pointing at it, or giving a line number) –by searching for a line matching a given search string.  The user can delete the selected line.  The user can insert a new line, either above the selected line or below the selected line.

8-8 Example: simple text editor (2)  We can represent the text being edited by: –a list of lines, text –the position sel of the selected line  We can implement the user commands straightforwardly in terms of list operations, e.g.: –Delete: remove the line at position sel in text. –Insert above: add the new line at position sel in text, then increment sel. –Insert below: increment sel, then add the new line at position sel in text. –Save: traverse text, writing each line to the output file.

8-9 List ADT: requirements  Requirements: 1)It must be possible to make a list empty. 2)It must be possible to test whether a list is empty. 3)It must be possible to obtain the length of a list. 4)It must be possible to add an element at any position in a list. 5)It must be possible to remove the element at any position in a list. 6)It must be possible to inspect or update the element at any position in a list. 7)It must be possible to concatenate lists. 8)It must be possible to test lists for equality. 9)It must be possible to traverse a list.

8-10 List ADT: contract (1)  Possible contract for homogeneous lists: public interface List { // Each List object is a homogeneous list // whose elements are of type E. //////////// Accessors //////////// public boolean isEmpty (); // Return true if and only if this list is empty. public int size (); // Return this list’s length.

8-11 List ADT: contract (2)  Possible contract (continued): public E get (int p); // Return the element at position p in this list. public boolean equals (List that); // Return true if and only if this list and that have the // same length, and each element of this list equals // the corresponding element of that.

8-12 List ADT: contract (3)  Possible contract (continued): //////////// Transformers //////////// public void clear (); // Make this list empty. public void set (int p, E it); // Replace the element at position p in // this list by it. public void add (int p, E it); // Add it at position p in this list. public void addLast (E it); // Add it after the last element of this list. This changes the positions of succeeding elements.

8-13 List ADT: contract (4)  Possible contract (continued): public void addAll (List that); // Add all the elements of that after the // last element of this list. public E remove (int p); // Remove and return the element at // position p in this list. //////////// Iterator //////////// public Iterator iterator (); // Return an iterator that will visit all // elements of this list, in left-to-right order. } This changes the positions of succeeding elements.

8-14 Traversal (1)  To traverse array : for (int i = 0; i < array.length; i++) … array[i] … This traversal has time complexity O(n).  We could mimic this to traverse list : for (int p = 0; p < list.size(); p++) … list.get(p) … … list.set(p, x) …  But this traversal could have time complexity O(n 2 ), if get and set turn out to be O(n).

8-15 Traversal (2)  Better, use an iterator to traverse list : Iterator elements = list.iterator(); while (elements.hasNext()) { T elem = elements.next(); … elem … }  This traversal has time complexity O(n), since the hasNext() and next() operations are guaranteed to be O(1). visits the next element in that iterator tests whether that iterator still has more elements to visit constructs an iterator over the elements of list

8-16 Iterators (1)  View an iterator as a path along which we visit the elements one by one, in some desired order.  Examples of iterators over a list: ‘to’,‘be’,‘or’,‘not’,‘to’,‘be’ »« left-to-right iterator right-to-left iterator

8-17 Iterators (2)  The List interface’s iterator() operation constructs a left-to-right iterator over the list elements.  The iterator’s hasNext() operation tests whether there is a next element still to be visited.  The iterator’s next() operation returns the next element (if any).

8-18 Iterator ADT: contract  Java’s contract for iterators: public interface Iterator { // Each Iterator object represents an iterator // over some collection of elements of type E. public boolean hasNext (); // Return true if and only if this iterator has a next // element. Guaranteed O(1). public E next (); // Return the next element in this iterator. // Guaranteed O(1). … } omitted operation

8-19 Iterators: implementation  An iterator is represented by a position on the iterator’s path, typically: –an index (if the elements are held in an array) –a link (if the elements are held in a linked-list).  The hasNext() operation tests whether there is a next position on the iterator’s path.  The next() operation advances to the next position on the iterator’s path, and returns the element at that position. –It throws an exception if there is no next position.

8-20 Implementation of lists using arrays (1)  Represent a bounded list (size  cap) by: –a variable size –an array elems of length cap, containing the elements in elems[0…size–1]. Illustration (cap = 6): LHRCDGGLA 01235size=4 Empty list: cap–1size=0 element 01size–1cap–1 Invariant: last element unoccupied first element

8-21 Implementation of lists using arrays (2)  Java implementation: public class ArrayList implements List { private E[] elems; private int size; //////////// Constructor //////////// public ArrayList (int cap) { elems = (E[]) new Object[cap]; size = 0; }

8-22 Implementation of lists using arrays (3)  Java implementation (continued): //////////// Accessors //////////// public int size () { return size; } public E get (int p) { if (p = size) throw …; return elems[p]; } …

8-23 Implementation of lists using arrays (4)  Java implementation (continued): //////////// Transformers //////////// public void add (int p, E it) { if (p size) throw …; if (size == elems.length) … for (int j = size; j > p; j--) elems[j] = elems[j-1]; elems[p] = it; size++; } …

8-24 Implementation of lists using arrays (5)  Java implementation (continued): //////////// Iterator //////////// public Iterator iterator () { return new LRIterator(); } //////////// Inner class //////////// private class LRIterator implements Iterator { … } }

8-25 Implementation of lists using arrays (6)  Implementing iterators over ArrayList objects: //////////// Inner class //////////// private class LRIterator implements Iterator { // An LRIterator object is a left-to-right iterator // over an ArrayList object. private int position; // position is the index of the slot containing the // next element to be visited. private LRIterator () { position = 0; }

8-26 Implementation of lists using arrays (7)  Implementing iterators over ArrayList objects (continued): public boolean hasNext () { return (position < size); } public E next () { if (position >= size) throw …; return elems[position++]; } … }

8-27 Implementation of lists using arrays (8)  Since LRIterator is a non-static inner class of ArrayList, its instance methods can access ArrayList instance variables. E.g.: 0 position LRIterator class iter iterator constructed by iter = tour.iterator(); LHRCDGGLA Object[ ] classlength 4 size ArrayList classelems tour 5

8-28 Implementation of lists using SLLs (1)  Represent an (unbounded) list by: –a variable size –an SLL, with links to both first and last nodes. Invariant: element first elementlast element first last size Empty list: 0 first last size Illustration: GLALHRCDGGLA 4 first last size

8-29 Implementation of lists using SLLs (2)  Java implementation: public class LinkedList implements List { private Node first, last; private int size; //////////// Inner class //////////// private static class Node { … } //////////// Constructor //////////// public LinkedList () { first = last = null; size = 0; }

8-30 Implementation of lists using SLLs (3)  Java implementation (continued): //////////// Accessors //////////// public int size () { return size; } public E get (int p) { if (p = size) throw …; return locate(p).element; } …

8-31 Implementation of lists using SLLs (4)  Java implementation (continued): /////////// Auxiliary method /////////// private Node locate (int p) { // Return a link to the node at position p in this list. Node curr = first; for (int j = 0; j < p; j++) curr = curr.succ; return curr; }

8-32 Implementation of lists using SLLs (5)  Java implementation (continued): //////////// Transformers //////////// public void add (int p, E it) { if (p size) throw … ; Node newest = new Node(it, null); if (p == 0) { newest.succ = first; first = newest; } else { Node pred = locate(p-1); newest.succ = pred.succ; pred.succ = newest; } if (newest.succ == null) last = newest; size++; }

8-33 Implementation of lists using SLLs (6)  Java implementation (continued): //////////// Iterator //////////// public Iterator iterator () { return new LRIterator(); } //////////// Inner class //////////// private class LRIterator implements Iterator { … } }

8-34 Implementation of lists using SLLs (7)  Implementing iterators over LinkedList objects: private class LRIterator implements Iterator { // An LRIterator object is a left-to-right iterator over // a LinkedList object. private Node position; // position is a link to the node containing the next // element to be visited. private LRIterator () { position = first; }

8-35 Implementation of lists using SLLs (8)  Implementing iterators over LinkedList objects (continued): public boolean hasNext () { return (position != null); } public E next () { if (position == null) throw …; E nextElem = position.element; position = position.succ; return nextElem; } … }

8-36 Implementation of lists using SLLs (9)  Since LRIterator is a non-static inner class of LinkedList, its instance methods can access LinkedList instance variables: iterator constructed by iter = tour.iterator(); position LRIterator class iter last LinkedList classfirst tour 4 size GLA element Node class succ GLA element Node classsucc

8-37 Summary of list implementations  Time complexities of main operations: OperationArray representationSLL representation get O(1)O(p)O(p) set O(1)O(p)O(p) remove O(n)O(n)O(p)O(p) add O(n)O(n)O(p)O(p) addLast best worst O(1) O(n) O(1) equals O(n)O(n)O(n)O(n) addAll O(n') where n' = size of second list

8-38 Iterating over a list with a Java for-loop  The following code pattern is extremely common: List list; … Iterator elems = list.iterator(); while (elems.hasNext()) { T elem = elems.next(); … elem … }  So Java provides equivalent for-loop notation: List list; … for (T elem : list) { … elem … } Read this as “for each element elem in list, do the following”.

8-39 Lists in the Java class library (1)  The library interface java.util.List is similar to the above interface List.  The library class java.util.ArrayList implements java.util.List, representing each list by an array.  The library class java.util.LinkedList implements java.util.List, representing each list by a doubly-linked-list. (Why?)

8-40 Lists in the Java class library (2)  Time complexities of the principal list methods: Method ArrayListLinkedList get O(1)O(p)O(p) set O(1)O(p)O(p) remove O(n)O(n)O(p)O(p) add O(n)O(n)O(p)O(p) addLast best worst amortized O(1) O(n) O(1) O(1) ditto ditto

8-41 Aside: amortized complexity (1)  An operation’s amortized time complexity reflects its performance averaged over a large number of calls.  Consider the addLast method in ArrayList : –Normally, only 1 copy is needed. –When the array is full, n elements are copied into a new array with doubled length, so in total n+1 copies are needed.

8-42 Aside: amortized complexity (2)  Consider 30 consecutive additions to an empty list (with initial capacity 4).  Number of copies: 1, 1, 1, 1, 5, 1, 1, 1, 9, 1, 1, 1, 1, 1, 1, 1, 17, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 (total 58)  On average:  2 copies per call.  Amortized time complexity is O(1) total copies no. of calls 60

8-43 Example: simple text editor again (1)  Outline implementation of the simple text editor: public class TextEditor { private List text; private int sel; // position of the selected line public TextEditor () { // Make the text empty. text = new ArrayList (); sel = -1; } or: new LinkedList ()

8-44 Example: simple text editor again (2)  Outline implementation (continued): public void select (int p) { // Select the line at position p. if (p = text.size()) throw …; sel = p; } public void delete () { // Delete the selected line. if (sel < 0) throw …; text.remove(sel); if (sel == text.size()) sel--; }

8-45 Example: simple text editor again (3)  Outline implementation (continued): public void find (String str) { // Select the next line containing str as a substring. // Wrap round to line 0 if necessary. if (sel = 0) { // … str found sel = p; return; } if (++p == n) p = 0; } while (p != sel); throw …; // str not found }

8-46 Example: simple text editor again (4)  Outline implementation (continued): public void insertAbove (String line) { // Insert line immediately above the selected line. if (sel < 0) throw …; text.add(sel, line); sel++; } public void insertBelow (String line) { // Insert line immediately below the selected line. sel++; text.add(sel, line); }

8-47 Example: simple text editor again (5)  Outline implementation (continued): public void load (BufferedReader input) { // Load the entire contents of input into the text. for (;;) { String line = input.readLine(); if (line == null) break; text.addLast(line); } sel = text.size() - 1; // select last line }

8-48 Example: simple text editor again (6)  Outline implementation (continued): public void save (BufferedWriter output) { // Save the text to output. for (String line : text) output.write(line + "\n"); } }