Chapter 6: Vectors, Lists and Sequences Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004. 2011) http://parasol.tamu.edu Vectors: Outline and Reading • The Vector ADT (§6.1.1) • Array-based implementation (§6.1.2) Vectors 4/13/2015 11:53 2 AM The Vector ADT • Main vector operations: • The Vector ADT • elemAtRank(int r): returns the extends the notion of element at rank r without removing array by storing a it sequence of arbitrary • replaceAtRank(int r, Object o): objects replace the element at rank r with o • An element can be • insertAtRank(int r, Object o): insert accessed, inserted or a new element o to have rank r removed by specifying • removeAtRank(int r): removes the its rank (number of element at rank r elements preceding it) • Additional operations size() and isEmpty() • An exception is thrown if an incorrect rank is specified (e.g., a negative rank) Vectors 4/13/2015 11:53 3 AM Applications of Vectors • Direct applications • Sorted collection of objects (elementary database) • Indirect applications • Auxiliary data structure for algorithms • Component of other data structures Vectors 4/13/2015 11:53 4 AM Array-based Vector • Use an array V of size N • A variable n keeps track of the size of the vector (number of elements stored) • Operation elemAtRank(r) is implemented in O(1) time by returning V[r] N-1 0 V 0 1 2 r Vectors n 4/13/2015 11:53 5 AM Array based Vector: Insertion • In operation insertAtRank(r,o) we need to make room for the new element by shifting forward the n - r elements V[r], …, V[n - 1] • In the worst case (r = 0), this takes O(n) time V 0 1 2 r n 0 1 2 r n 0 1 2 o r V V Vectors n 4/13/2015 11:53 6 AM Deletion • In operation removeAtRank(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] • In the worst case (r = 0), this takes O(n) time V 0 1 2 o r n 0 1 2 r n 0 1 2 r V V Vectors n 4/13/2015 11:53 7 AM Performance • In the array based implementation of a Vector • The space used by the data structure is O(n) • Size(), isEmpty(), elemAtRank(r) and replaceAtRank(r,o) run in O(1) time • insertAtRank(r,o) and removeAtRank(r) run in O(n) time • If we use the array in a circular fashion, insertAtRank(0,o) and removeAtRank(0) run in O(1) time • In an insertAtRank(r,o) operation, when the array is full, instead of throwing an exception, we can replace the array with a larger one 4/13/2015 11:53 AM Vectors 8 Exercise: • Implement the Deque ADT using Vector functions • Deque functions: • first(), last(), insertFirst(e), insertLast(e), removeFirst(), removeLast(), size(), isEmpty() • Vector functions: • elemAtRank( r), replaceAtRank(r,e), insertAtRank(r,e), removeAtRank(r ), size(), isEmpty() Vectors 4/13/2015 11:53 9 AM Exercise Solution: • Implement the Deque ADT using Vector functions • Deque functions: first(), last(), insertFirst(e), insertLast(e), removeFirst(), removeLast(), size(), isEmpty() • Vector functions: elemAtRank( r), replaceAtRank(r,e), insertAtRank(r,e), removeAtRank(r ), size(), isEmpty() • • • • • • • • Deque function : Realization using Vector Functions size() and isEmpty() fcns can simply call Vector fcns directly first() => elemAtRank(0) last() => elemAtRank(size()-1) insertFirst(e) => insertAtRank(0,e) insertLast(e) => insertAtRank(size(), e) removeFirst() => removeAtRank(0) removeLast() => removeAtRank(size()-1) Vectors 4/13/2015 11:53 10 AM STL vector class • Functions in the STL vector class (incomplete) • • • • • • • • Size(), capacity() - return #elts in vector, #elts vector can hold empty() - boolean Operator[r] - returns reference to elt at rank r (no index check) At( r) - returns reference to elt at rank r (index checked) Front(), back() - return references to first/last elts push_back(e) - insert e at end of vector pop_back() - remove last elt vector(n) - creates a vector of size n • Similarities & Differences with book’s Vector ADT • STL assignment v[r]=e is equivalent to v.replaceAtRank(r,e) • No direct STL counerparts of insertAtRank( r) & removeAtRank( r) • STL also provides more generatl fcns for inserting & removing from arbitrary positions in the vector - these use iterators Vectors 4/13/2015 11:53 11 AM Iterators • An iterator abstracts the • An iterator is typically process of scanning through a associated with an another collection of elements data structure • Methods of the ObjectIterator • We can augment the Stack, ADT: Queue, Vector, and other container ADTs with method: • boolean hasNext() • ObjectIterator elements() • object next() • reset() • Two notions of iterator: • Extends the concept of position by adding a traversal capability • May be implemented with an array or singly linked list Vectors • snapshot: freezes the contents of the data structure at a given time • dynamic: follows changes to the data structure 4/13/2015 11:53 12 AM Iterators • Some functions supported by STL containers • Begin(), end() - return iterators to beginning or end of container • Insert(I,e) - insert e just before the position indicated by iterator I (analogous to our insertBefore(p)) • Erase(I) - removes the elt at the position indicated by I (analogous to our remove(p)) • The functions can be used to insert/remove elts from arbitrary positions in the STL vector and list Vectors 4/13/2015 11:53 13 AM Vector Summary • Vector Operation Complexity for Different Implementations Array Fixed-Size or Expandable List Singly or Doubly Linked RemoveAtRank(r), InsertAtRank(r,o) O(1) Best Case (r=0,n) O(n) Worst Case O(n) Average Case ? elemAtRank(r), ReplaceAtRank(r,o) O(1) ? Size(), isEmpty() O(1) ? Vectors 4/13/2015 11:53 AM 14 Lists and Sequences Vectors 4/13/2015 11:53 15 AM Outline and Reading • • • • • • Singly linked list Position ADT (§6.2.1) List ADT (§6.2.2) Doubly linked list (§ 6.2.3) Sequence ADT (§6.3.1) Implementations of the sequence ADT (§6.3.23) • Iterators (§6.2.5) Vectors 4/13/2015 11:53 16 AM Position ADT • The Position ADT models the notion of place within a data structure where a single object is stored • A special null position refers to no object. • Positions provide a unified view of diverse ways of storing data, such as • a cell of an array • a node of a linked list • Member functions: • Object& element(): returns the element stored at this position • bool isNull(): returns true if this is a null position Vectors 4/13/2015 11:53 17 AM List ADT (§5.2.2) • The List ADT models a sequence of positions storing arbitrary objects • establishes a before/after relation between positions • It allows for insertion and removal in the “middle” • Query methods: • isFirst(p), isLast(p) • Accessor methods: • first(), last() • before(p), after(p) • Update methods: • replaceElement(p, o), swapElements(p, q) • insertBefore(p, o), insertAfter(p, o), • insertFirst(o), insertLast(o) • remove(p) • Generic methods: • size(), isEmpty() 18 List ADT • Query methods: • isFirst(p), isLast(p) : • return boolean indicating if the given position is the first or last, resp. • Accessor methods • first(), last(): • return the position of the first or last, resp., element of S • an error occurs if S is empty • before(p), after(p): • return the position of the element of S preceding or following, resp, the one at position p • an error occurs if S is empty, or p is the first or last, resp., position Vectors 4/13/2015 11:53 19 AM List ADT • Update Methods • replaceElement(p, o) • Replace the element at position p with e • swapElements(p, q) • Swap the elements stored at positions p & q • insertBefore(p, o), insertAfter(p, o), • Insert a new element o into S before or after, resp., position p • Output: position of the newly inserted element • insertFirst(o), insertLast(o) • Insert a new element o into S as the first or last, resp., element • Output: position of the newly inserted element • remove(p) • Remove the element at position p from S Vectors 4/13/2015 11:53 20 AM Exercise: • Describe how to implement the following list ADT operations using a singly-linked list • list ADT operations: first(), last(), before(p), after(p) • For each operation, explain how it is implemented and provide the running time next • A singly linked list concrete data structure consists of a sequence of nodes • Each node stores elem • element • link to the next node node A Vectors B C D 4/13/2015 11:53 21 AM Exercise: • Describe how to implement the following list ADT operations using a doubly-linked list • list ADT operations: first(), last(), before(p), after(p) • For each operation, explain how it is implemented and provide the running time prev • Doubly-Linked List Nodes implement Position and store: • element • link to previous node • link to next node next node elem header trailer • Special trailer and header nodes Vectors elements 4/13/2015 11:53 22 AM Insertion • We visualize operation insertAfter(p, X) which returns position q p A B C p A q B C X p A q B Vectors X C 4/13/2015 11:53 23 AM Deletion • We visualize remove(p), where p = last() p A B C A B C D p D A B Vectors C 4/13/2015 11:53 24 AM Performance • In the implementation of the List ADT by means of a doubly linked list • • • • The space used by a list with n elements is O(n) The space used by each position of the list is O(1) All the operations of the List ADT run in O(1) time Operation element() of the Position ADT runs in O(1) time Vectors 4/13/2015 11:53 25 AM STL list class • Functions in the STL list class • • • • • Size() - return #elts in list, empty() - boolean Front(), back() - return references to first/last elts Push_front(e), push_back(e) - insert e at front/end Pop_front(), pop_back() - remove first/last elt List() - creates an empty list • Similarities & Differences with book’s List ADT • STL front() & back() correspond to first() & last() except the STL functions return the element & not its position • STL push() & pop() are equiv to List ADT insert and remove when applied to the beginning & end of the list • STL also provides fcns for inserting & removing from arbitrary positions in the list - these use iterators Vectors 4/13/2015 11:53 26 AM List Summary • List Operation Complexity for different implementations Vectors List Singly-Linked List DoublyLinked first(), last(), after(p) insertAfter(p,o), replaceElement(p,o), swapElements(p,q) O(1) O(1) before(p), insertBefore(p,o), remove(p) O(n) WC & AC O(1) BC O(1) Size(), isEmpty() O(1) O(1) 4/13/2015 11:53 AM 27 Sequence ADT • The Sequence ADT is the union • List-based methods: of the Vector and List ADTs • first(), last(), before(p), • Elements accessed by after(p), • Rank, or replaceElement(p, o), • Position swapElements(p, q), insertBefore(p, o), • Generic methods: insertAfter(p, o), • size(), isEmpty() insertFirst(o), • Vector-based methods: insertLast(o), • elemAtRank(r), replaceAtRank(r, remove(p) o), insertAtRank(r, o), removeAtRank(r) • Bridge methods: • atRank(r), rankOf(p) Vectors 4/13/2015 11:53 28 AM Applications of Sequences • The Sequence ADT is a basic, generalpurpose, data structure for storing an ordered collection of elements • Direct applications: • Generic replacement for stack, queue, vector, or list • small database (e.g., address book) • Indirect applications: • Building block of more complex data structures Vectors 4/13/2015 11:53 29 AM Array-based Implementation elements • We use a circular array storing positions • A position object stores: • Element • Rank • Indices f and l keep track of first and last positions 0 1 2 3 positions S f Vectors l 4/13/2015 11:53 30 AM Sequence Implementations Operation size, isEmpty atRank, rankOf, elemAtRank first, last, before, after replaceElement, swapElements replaceAtRank insertAtRank, removeAtRank insertFirst, insertLast insertAfter, insertBefore remove Vectors Array 1 1 1 1 1 n 1 n n List 1 n 1 1 n n 1 1 1 4/13/2015 11:53 31 AM