Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "8 List and Iterator ADTs  List concepts  List applications  A list ADT: requirements, contract  Iterators  Implementations of lists: using arrays."— Presentation transcript:

1 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)

2 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.

3 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.

4 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’»

5 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).

6 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.

7 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-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.

9 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.

10 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.

11 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.

12 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.

13 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.

14 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).

15 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

16 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

17 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).

18 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

19 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.

20 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

21 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; }

22 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]; } …

23 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++; } …

24 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 { … } }

25 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; }

26 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++]; } … }

27 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 01234 6Object[ ] classlength 4 size ArrayList classelems tour 5

28 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

29 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; }

30 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; } …

31 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; }

32 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++; }

33 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 { … } }

34 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; }

35 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; } … }

36 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

37 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

38 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”.

39 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?)

40 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

41 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.

42 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). 102030 total copies 10 20 30 40 50 0 0 no. of calls 60

43 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 ()

44 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--; }

45 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 }

46 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); }

47 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 }

48 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"); } }


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

Similar presentations


Ads by Google