Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.

Slides:



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

Chapter 3 Lists Dr Zeinab Eid.
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Lists1 © 2010 Goodrich, Tamassia. Position ADT The Position ADT models the notion of place within a data structure where a single object is stored It.
6/7/2014 8:24 AMSequences1 Lists and Sequences. 6/7/2014 8:24 AMSequences2 Outline and Reading Singly linked list Position ADT and List ADT (§5.2.1) Doubly.
M180: Data Structures & Algorithms in Java
CSC 212 – Data Structures Lecture 22: PositionList.
Review Learn about linked lists
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
Iterators and Sequences1 © 2010 Goodrich, Tamassia.
© 2004 Goodrich, Tamassia Vectors1. © 2004 Goodrich, Tamassia Vectors2 The Vector ADT (“Vector” = “Array List” in §6.1) The Vector ADT extends the notion.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Data Structures Lecture 4 Fang Yu Department of Management Information Systems National Chengchi University Fall 2010.
1 Chapter 24 Lists Stacks and Queues. 2 Objectives F To design list with interface and abstract class (§24.2). F To design and implement a dynamic list.
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Linked Lists1 Part-B3 Linked Lists. Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data structure consisting of a sequence.
CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic data structures, including –Stacks –Queues –Vectors –Lists.
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.
Vectors, Lists, and Sequences - Ed. 2 and 3.: Chapter 5 - Ed. 4: Chapter 6.
© 2004 Goodrich, Tamassia Vectors1 Lecture 03 Vectors, Lists and Sequences Topics Vectors Lists Sequences.
Chapter 6: Sequences : vectors and lists COL 106 Shweta Agrawal.
Doubly Linked Lists1 © 2014 Goodrich, Tamassia, Goldwasser Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition,
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
© 2004 Goodrich, Tamassia Linked Lists1. © 2004 Goodrich, Tamassia Linked Lists2 Singly Linked List (§ 4.4.1) A singly linked list is a concrete data.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Arrays & Linked Lists Last Update: Aug 21, 2014EECS2011: Arrays & Linked Lists1.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Lists Last Update: Oct 29, 2014 EECS2011: Lists1.
CS 221 Analysis of Algorithms Data Structures Vectors, Lists.
CH 6. VECTORS, LISTS, AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Lecture4: Arrays Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Lecture 3 Queues Queues1. queue: – Retrieves elements in the order they were added. – First-In, First-Out ("FIFO") – Elements are stored in order of insertion.
Lecture5: Linked Lists Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
Array Lists1 © 2010 Goodrich, Tamassia. Array Lists2 The Array List ADT  The Array List ADT extends the notion of array by storing a sequence of arbitrary.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
1 Linked Lists (Lec 6). 2  Introduction  Singly Linked Lists  Circularly Linked Lists  Doubly Linked Lists  Multiply Linked Lists  Applications.
© 2004 Goodrich, Tamassia Lists1. © 2004 Goodrich, Tamassia Lists2 Position ADT (§ 5.2.2) The Position ADT models the notion of place within a data structure.
Introduction Dynamic Data Structures Grow and shrink at execution time Linked lists are dynamic structures where data items are “linked up in a chain”
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH,
Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
Linked List, Stacks Queues
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,
Vectors 5/31/2018 9:25 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Ch7. List and Iterator ADTs
Sequences and Iterators
COMP9024: Data Structures and Algorithms
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Sequences and Iterators
Array Lists, Node Lists & Sequences
Ch7. List and Iterator ADTs
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,
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
" A list is only as strong as its weakest link. " - Donald Knuth
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Recall What is a Data Structure Very Fundamental Data Structures
Vectors, Lists and Sequences
Linked Lists & Iterators
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Vectors and Array Lists
Presentation transcript:

Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007

The Array List ADT (aka Vector or Index List) The ArrayList ADT extends the notion of an array by storing a sequence of arbitrary objects An element can be accessed, inserted or removed by specifying its rank (number of elements preceding it) An exception is thrown if an incorrect rank is specified; e.g.: – negative rank – rank larger than current size of the array list

The Array List ADT Main array list operations: – object get(integer r): returns the element at rank r without removing it – object set(integer r, object o): replace the element at rank r with o and return the old element – void insert(integer r, object o): insert a new element o to have rank r – object remove(integer r): removes and returns the element at rank r All these operations need to check that r is a valid rank. Additional operations size() and isEmpty()

In the Java Vector class these methods are renamed: Java Vector: size() and isEmpty() elementAt(int index) object setElementAt(object o, int index) insertElementAt(object o, int index) object remove(int index) Array List ADT: size() and isEmpty() get(integer r) object set(integer r, object o) add(integer r, object o) object remove(integer r)

Array-Based Implementation Use an array V of capacity N A variable n keeps track of the size of the array (number of elements stored) Operation get(r) is implemented in O(1) time by returning V[r] V 012n r

Insertion In operation set(r,o), we need to make room for the new element by shifting forward elements V[r], …, V[n  1] In the worst case ( r  0 ), this requires n operations ( O(n) time) V 012n r V 012n r V 012n o r

Deletion For remove(r), we need to fill the hole left by the removed element by shifting backward the ( n – r – 1) elements V[r  1], …, V[n  1] Also O(n) time V 012n r V 012n o r V 012n r

Performance The array-based implementation of an Array List: – The space used is O(n) – size,isEmpty,get and set run in O(1) time – add and remove run in O(n) time In an add operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one

Extendable Arrays A simple solution to the capacity limitation of arrays is to replace a full array with a longer array, and copy elements from the original array to the new array. This can be done in O(n) time. (Does not increase the asymptotic efficiency of the add operation.)

Extendable Arrays size, capacity and A[] are fields of the ArrayIndexList class, if (size == capacity) { // increase capacity capacity *= 2; // create a new array E[] new_array = (E[]) new Object[capacity]; // copy references from old array for (int i=0; i<size; i++) new_array[i] = A[i]; // replace old array with new array A = new_array; }

UML for IndexList

UML for ArrayIndexList

Position List ADT (aka Node List) The Position List ADT specifies positions of elements relative to positions of other elements We’ll use a new kind of object, Position, as an abstraction of the location of some existing element Just one method: – object element(): returns the element stored at the position

Position List ADT (aka Node List) The Node List ADT models a sequence of positions storing arbitrary objects It establishes a before/after relation between positions Generic methods: – size() – isEmpty() Accessor methods (all return type Position): – first(), last() – prev(p), next(p) Update methods: – set(p, e) – remove(p) – addBefore(p, e) – addAfter(p, e) – addFirst(e) – addLast(e)

Doubly Linked List A doubly-linked list provides a natural implementation of the Node List ADT Nodes implement Position and store: – element – link to the previous node – link to the next node Sentinel header and trailer nodes trailer header nodes/positions elements prevnext elem node

Insertion Operation addAfter(p, X), which returns position q ABC p ABC p X q ABXC pq

Insertion Algorithm Algorithm addAfter(p,e): Create a new node v v.setElement(e) v.setPrev(p){link v to its predecessor} v.setNext(p.getNext()){link v to its successor} (p.getNext()).setPrev(v){link p’s old successor to v} p.setNext(v){link p to its new successor, v} size ++ return v{the position for the element e}

ABCD p Deletion Operation remove(p) ABC D p ABC

Deletion Algorithm Algorithm remove(p): t = p.element {temporary variable to hold the return value} (p.getPrev()).setNext(p.getNext()){fix links around p} (p.getNext()).setPrev(p.getPrev()) p.setPrev(null){invalidating the position p} p.setNext(null) size -- return t

Performance List ADT using a doubly-linked list: – The space used by a list with n elements is O(n) – All the operations of the List ADT run in O(1) time – Operation element() of the Position ADT runs in O(1) time

Iterators An iterator abstracts the process of scanning through a collection of elements Methods of the Iterator ADT: – element element() – boolean hasNext() – element nextElement() – reset() Extends the concept of Position by adding a traversal capability In other contexts, iterators may be called cursors – example: SQL databases

Iterators An iterator is typically associated with an another data structure Data structures that support iterators add a method to create and iterator: – ElementIterator iterator() Two notions of iterator: – snapshot: freezes the contents of the data structure at a given time – dynamic: follows changes to the data structure

Using Iterators Iterator iter = mylist.iterator(); while (iter.hasNext()) { Object obj = iter.next(); // do something with this object } mylist is a reference to some data structure that supports iterators

What’s next? Designing and implementing iterators Java Collections Framework