Presentation is loading. Please wait.

Presentation is loading. Please wait.

8 List ADTs List concepts. List applications.

Similar presentations


Presentation on theme: "8 List ADTs List concepts. List applications."— Presentation transcript:

1 8 List ADTs List concepts. List applications.
A list ADT: requirements, contract. Iterators. Implementations of lists: using arrays, linked lists. Lists in the Java class library. © 2001, D.A. Watt and D.F. Brown

2 List concepts (1) A list is a sequence of elements, whose order is significant. Elements can added and removed anywhere in the list. The length of a list is the number of elements it contains. An empty list has length zero. The concatenation of lists l1 and l2 is a list containing all the elements of l1 followed by all the elements of l2. Traversal of a list (or iterating over a list) means visiting each of the list’s elements in turn, in some desired order.

3 «‘to’, ‘be’, ‘or’, ‘not’, ‘to’, ‘be’, ‘that’, ‘is’, ‘the’, ‘question’»
List concepts (2) Notation for lists: «a, b, …, z». The empty list is « ». List notation is used here, but not supported by Java. Examples of lists: fibonacci = «1, 1, 2, 3, 5, 8, 13, 21, 44, 65» birthdays = « , , » tour = «GLA, LHR, CDG, GLA» hamlet1 = «‘to’, ‘be’, ‘or’, ‘not’, ‘to’, ‘be’» hamlet2 = «‘that’, ‘is’, ‘the’, ‘question’» Concatenation of hamlet1 and hamlet2: «‘to’, ‘be’, ‘or’, ‘not’, ‘to’, ‘be’, ‘that’, ‘is’, ‘the’, ‘question’»

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

5 List applications A sentence is a list of words.
The order of words is significant. The same word can occur more than once in a sentence. An itinerary is a list of places visited on a tour. The order of places is significant. The same place can occur more than once in an itinerary. A log is a list of event records (e.g., equipment faults). The event records are in time order.

6 Example 1: simple text editor (1)
Consider a 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 (e.g., by a mouse-click) by searching for the next line matching a given search string. The user can delete the selected line. The user can insert a new line, either before or after the selected line.

7 Example 1 (2) We can represent the text being edited by:
a list of lines, text the number of the selected line, sel (where 0  sel < length of text, or sel = –1 if text is empty) We can implement the user commands straightforwardly in terms of list operations, e.g.: Delete: remove line sel of text. Insert before: add the new line as line sel of text, then increment sel. Insert after: increment sel, then add the new line as line sel of text. Save: traverse text, writing each line to the output file.

8 List ADT: requirements
It must be possible to make a list empty. It must be possible to test whether a list is empty. It must be possible to obtain the length of a list. It must be possible to add an element anywhere in a list. It must be possible to remove an element anywhere in a list. It must be possible to inspect or update an element anywhere in a list. It must be possible to concatenate lists. It must be possible to test lists for equality. It must be possible to traverse a list.

9 List ADT: contract (1) Possible contract: public interface List {
// Each List object is an indexed list whose elements are // objects. //////////// Accessors //////////// public boolean isEmpty (); // Return true if and only if this list is empty. public int size (); // Return this list’s length.

10 List ADT: contract (2) Possible contract (continued):
public Object get (int i); // Return the element with index i 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.

11 List ADT: contract (3) Possible contract (continued):
//////////// Transformers //////////// public void clear (); // Make this list empty. public void set (int i, Object elem); // Replace by elem the element at index i in this list. public void add (int i, Object elem); // Add elem as the element with index i in this list. public void add (Object elem); // Add elem after the last element of this list.

12 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 Object remove (int i); // Remove and return the element with index i in this list. //////////// Iterator //////////// public Iterator iterator (); // Return an iterator that will visit all elements of this list, in // left-to-right order. }

13 List traversal (1) To traverse array a:
for (int i = 0; i < a.length; i++) … a[i] … We could mimic this to traverse list: for (int i = 0; i < list.size(); i++) … list.get(i) … … list.set(i, x) … But this traversal has time complexity O(n2), if get and set are O(n).

14 List traversal (2) Better, use an iterator to traverse list:
Iterator elems = list.iterator(); while (elems.hasNext()) { Object elem = elems.next(); … elem … } constructs an iterator over the elements of list tests whether that iterator has any more elements accesses the next element in that iterator This traversal has time complexity O(n).

15 Iterators (1) Visualize an iterator as a chain to which elements are attached in the required order. Examples of iterators over the list «‘to’, ‘be’, ‘or’, ‘not’, ‘to’, ‘be’»: left-to-right iterator ‘to’ ‘be’ ‘or’ ‘not’ right-to-left iterator ‘to’ ‘be’ ‘or’ ‘not’ left-to-right alternating iterator ‘to’ ‘be’ ‘or’ ‘not’

16 Iterators (2) The List interface’s iterator() operation constructs an iterator on which the list elements are ‘chained’ from left to right. The iterator’s next() operation returns the next element on the ‘chain’. The iterator’s hasNext() operation tests whether there is a next element on the ‘chain’.

17 Iterators: contract Java contract for iterators:
public interface Iterator { // Each Iterator object represents an iterator over some // collection. public boolean hasNext (); // Return true if and only if this iterator has a next element. public Object next (); // Return the next element in this iterator. } omitted operation

18 Implementation of lists using arrays (1)
Represent a bounded list (length  maxlen) by: a variable length, containing the current length an array elems of length maxlen, containing the listed elements in elems[0… length–1]: last element unoccupied first element 1 length–1 length maxlen–1 Invariant: element Empty list: 1 maxlen–1 length=0 Illustration (maxlen = 6): LHR CDG GLA 1 2 3 5 length=4

19 Implementation using arrays (2)
Java implementation: public class ArrayList implements List { private Object[] elems; private int length; //////////// Constructor //////////// public ArrayList (int maxlen) { elems = new Object[maxlen]; length = 0; }

20 Implementation using arrays (3)
Java implementation (continued): //////////// Accessors //////////// public int size () { return length; } public Object get (int i) { if (i < 0 || i >= length) throw …; return elems[i]; }

21 Implementation using arrays (4)
Java implementation (continued): //////////// Transformers //////////// public void add (int i, Object elem) { if (i < 0 || i > length) throw …; if (length == elems.length) …; for (int j = length; j > i; j--) elems[j] = elems[j-1]; elems[i] = elem; length++; }

22 Implementation using arrays (5)
Java implementation (continued): //////////// Iterator //////////// public Iterator iterator () { return new LRIterator(); } //////////// Inner class //////////// private class LRIterator implements Iterator { … } }

23 Implementation using arrays (6)
Implementing iterators over ArrayList objects: private class LRIterator implements Iterator { // An LRIterator object is a left-to-right iterator over an // ArrayList object. private int place; private LRIterator () { place = 0; }

24 Implementation using arrays (7)
Implementing iterators (continued): public boolean hasNext () { return (place < length); } public Object next () { if (place >= length) throw …; return elems[place++]; } ... }

25 Implementation using arrays (8)
Since LRIterator is an inner class of ArrayList, its instance methods can access ArrayList instance variables. E.g.: LHR CDG GLA 1 2 3 4 6 Object[] class tag length ArrayList elems tour 5 place LRIterator class tag iter Iterator constructed by: iter = tour.iterator();

26 Implementation of lists using SLLs (1)
Represent an (unbounded) list by: a variable length an SLL, with links first and last to both ends: first element last element Invariant: element element element Empty list: Illustration: GLA LHR CDG

27 Implementation using SLLs (2)
Java implementation: public class LinkedList implements List { private SLLNode first, last; private int length; //////////// Constructor //////////// public LinkedList () { first = last = null; length = 0; }

28 Implementation using SLLs (3)
Java implementation (continued): /////////// Auxiliary method /////////// private SLLNode node (int i) { // Return a link to the node containing the element with index i // in this list. SLLNode curr = first; for (int j = 0; j < i; j++) curr = curr.succ; return curr; }

29 Implementation using SLLs (4)
Java implementation (continued): //////////// Accessors //////////// public int size () { return length; } public Object get (int i) { if (i < 0 || i >= length) throw …; return node(i).element; }

30 Implementation using SLLs (5)
Java implementation (continued): //////////// Transformers //////////// public void add (int i, Object elem) { if (i < 0 || i > length) throw …; SLLNode newest = new SLLNode(elem, null); if (i == 0) { newest.succ = first; first = newest; } else { SLLNode pred = node(i-1); newest.succ = pred.succ; pred.succ = newest; } if (newest.succ == null) last = newest; length++; }

31 Implementation using SLLs (6)
Java implementation (continued): //////////// Iterator //////////// public Iterator iterator () { return new LRIterator(); } //////////// Inner class //////////// private class LRIterator implements Iterator { … } }

32 Implementation 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 SLLNode place; private LRIterator () { place = first; }

33 Implementation using SLLs (8)
Implementing iterators (continued): public boolean hasNext () { return (place != null); } public Object next () { if (place == null) throw …; Object nextElem = place.element; place = place.succ; return nextElem; } ... }

34 Implementation using SLLs (9)
Since LRIterator is an inner class of LinkedList, its instance methods can access LinkedList instance variables: last LinkedList class tag first tour 4 length GLA element SLLNode succ place LRIterator class tag iter Iterator constructed by: iter = tour.iterator();

35 Summary of list implementations
Time complexities of main operations: Operation Array representation SLL representation get O(1) O(n) set add(int,Object) add(Object) remove equals O(n2) addAll

36 Lists in the Java class library
Java provides the interface java.util.List, similar to the List interface above. Java provides the class java.util.ArrayList, which implements the java.util.List interface, representing each list by an array. Java provides the class java.util.LinkedList, which implements the java.util.List interface, representing each list by a DLL.

37 Example 2: simple text editor revisited (1)
Recall the simple text editor of Example 1. Outline implementation: public class TextEditor { private List text; private int sel; public TextEditor () { text = new ArrayList(); sel = -1; } or: new LinkedList()

38 Example 2 (2) Outline implementation (continued):
public void select (int ln) { if (ln < 0 || ln >= text.size()) throw …; sel = ln; } public void find (String s) { if (sel < 0) throw …; for (int ln = sel; ln < text.size(); ln++) { String line = text.get(ln); if (line.indexOf(s) >= 0) { // found s sel = ln; return; } } throw …; }

39 Example 2 (3) Outline implementation (continued):
public void insertBefore (String line) { if (sel < 0) throw …; text.add(sel++, line); } public void insertAfter (String line) { text.add(++sel, line); } public void delete () { if (sel < 0) throw …; text.remove(sel); if (sel == text.size()) sel--; }

40 Example 2 (4) Outline implementation (continued):
public void load (BufferedReader input) { for (;;) { String line = input.readLine(); if (line == null) break; text.add(line); } sel = text.size() - 1; }

41 Example 2 (5) Outline implementation (continued):
public void save (BufferedWriter output) { Iterator lines = text.iterator(); while (lines.hasNext()) { String line = (String) lines.next(); output.write(line + "\n"); } } }


Download ppt "8 List ADTs List concepts. List applications."

Similar presentations


Ads by Google