CH 6 : VECTORS, LISTS AND SEQUENCES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM JORY DENNY AND MUKULIKA GHOSH 1
ITERATORS AND POSITION (CH 6.2.1) 2
LISTS List ADT (Ch ) Doubly linked list (Ch ) 3
LIST ADT (CH 6.2.2) 4
INSERT(P,E) 5 ABXC ABC p ABC p X q p q
ERASE(P) 6 ABCD p ABC D p ABC
EXERCISE: LIST PERFORMANCE AND SPACE (BEST, WORST, AVERAGE) Singly-linked ListDoubly-linked List Worst case Average case Best case 7 Singly-linked ListDoubly-linked List Space usage for list of n elements
LIST PERFORMANCE AND SPACE SUMMARY Singly-linked ListDoubly-linked List Worst case Average case Best case 8 Singly-linked ListDoubly-linked List Space usage for list of n elementsO(n)
VECTORS (CH 6.1) The Vector ADT (Ch ) Array-based implementation (Ch ) 9
VECTOR ADT (CH 6.1.1) The Vector ADT extends the notion of 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., a negative rank) 10
APPLICATIONS OF VECTOR Direct applications Sorted collection of objects (elementary database) Indirect applications Auxiliary data structure for algorithms Component of other data structures 11
EXERCISE 12
ARRAY-BASED VECTOR (CH 6.1.2) STORAGE 012n i 0 13
ARRAY-BASED VECTOR (CH 6.1.2) INSERTION 14 V 012n i V 012n i V 012n e i
ARRAY-BASED VECTOR (CH 6.1.2) DELETION 15 V 012n r V 012n e i V 012n i
EXERCISE: ARRAY-BASED VECTOR PERFORMANCE (BEST, WORST, AVG) 16 Array based Vector (Fixed) Array-based Vector (Growable) Space used by Vector at(i) Set(i,e) insert(i,e) erase(i) size() empty()
EXERCISE: ARRAY-BASED VECTOR PERFORMANCE 17 Array based Vector (Fixed) Array-based Vector (Growable) Doulbly-linked List Vector Space used by Vector ? at(i), set(i,e) ? insert(i,e) ? erase(i) ? size(), empty() ?
SEQUENCES Sequence ADT (Ch ) Implementations of the sequence ADT (Ch ) 18
SEQUENCE ADT (CH 6.3.1) 19
APPLICATION OF SEQUENCES The Sequence ADT is a basic, general-purpose, 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 20
ARRAY BASED IMPLEMENTATION OF SEQUENCE (CH 6.3.3) positions elements S lf
EXERCISE: SEQUENCE PERFORMANCE (BEST,WORST, AVG) Circular Array based SequenceDoubly-linked List based Sequence 22 Circular Array based SequenceDoubly-linked List based Sequence Space usage
SEQUENCE SUMMARY Circular Array based SequenceDoubly-linked List based Sequence 23 Circular Array based SequenceDoubly-linked List based Sequence Space usage O(N)O(n)