8 List ADTs List concepts. List applications.

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
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)
Linked Lists Ping Zhang 2010/09/29. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link.
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.
Linked Lists.
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.
Linked Lists [AJ 15] 1. 2 Anatomy of a Linked List  a linked list consists of:  a sequence of nodes abcd each node contains a value and a link (pointer.
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.
8 List and Iterator ADTs  List concepts  List applications  A list ADT: requirements, contract  Iterators  Implementations of lists: using arrays.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Linked Lists. 2 Anatomy of a linked list A linked list consists of: –A sequence of nodes abcd Each node contains a value and a link (pointer or reference)
Iterators Chapter 7. Chapter Contents What is an Iterator? A Basic Iterator Visits every item in a collection Knows if it has visited all items Doesn’t.
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
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.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
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,
9-1 9 Set ADTs Set concepts. Set applications. A set ADT: requirements, contract. Implementations of sets: using arrays, linked lists, boolean arrays.
4-1 4 Linked List Data Structures Linked lists: singly-linked and doubly-linked. Insertion. Deletion. Searching. © 2001, D.A. Watt and D.F. Brown.
Course: Object Oriented Programming - Abstract Data Types Unit2: ADT ListsSlide Number 1 Principles for implementing ADTs ADT operations as “walls” between.
Copyright © 0 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
List Interface and Linked List Mrs. Furman March 25, 2010.
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,
CSE 501N Fall ‘09 10: Introduction to Collections and Linked Lists 29 September 2009 Nick Leidenfrost.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 20 Ordered.
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.
Linked Lists.
Stacks.
Java collections library
Top Ten Words that Almost Rhyme with “Peas”
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM 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,
Data Structures ADT List
Stacks.
List Implementations Chapter 9.
Dynamic Data Structures and Generics
ADT list.
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
CSC 143 Java Linked Lists.
Introduction to Stacks
Building Java Programs
TCSS 143, Autumn 2004 Lecture Notes
Presentation transcript:

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

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.

«‘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 = «2001-02-23, 2001-05-05, 2001-11-05» 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’»

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

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.

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.

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.

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.

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.

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.

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.

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

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

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

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’

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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.

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

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

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

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

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