Vectors, Lists, and Sequences. Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)

Slides:



Advertisements
Similar presentations
Chapter 3 Lists Dr Zeinab Eid.
Advertisements

Queues and Linked Lists
© 2004 Goodrich, Tamassia Node-Lists1 6.2 Node Lists.
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.
Queues1 Part-B2 Queues. Queues2 The Queue ADT (§4.3) The Queue ADT stores arbitrary objects Insertions and deletions follow the first-in first-out scheme.
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.
Queues 4/14/2017 5:24 PM 5.2 Queues Queues Dr Zeinab Eid.
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.
Queues. What is a queue? First-in first-out data structure (FIFO) New objects are placed at rear Removal restricted to front Examples?
Lists and Iterators CSC311: Data Structures 1 Chapter 6 Lists and Iterators Objectives Array Lists and Vectors: ADT and Implementation Node Lists: ADT.
Lists and Iterators (Chapter 6) COMP53 Oct 22, 2007.
Elementary Data Structures Stacks, Queues, & Lists Amortized analysis Trees.
CSC 212 Vectors, Lists, & Sequences. Announcement Daily quizzes accepted electronically only  Submit via one or other Dropbox  also accepted,
© 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.
© 2004 Goodrich, Tamassia Stacks1 Abstract Data Types (ADTs) An abstract data type (ADT) is a contract between the user of a data structure and its implementor.
CSC401 – Analysis of Algorithms Lecture Notes 3 Basic Data Structures Objectives: Introduce basic data structures, including –Stacks –Queues –Vectors –Lists.
Sequences. The Sequence Abstract Data Type Implementing a Sequence.
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.
© 2004 Goodrich, Tamassia Sequences and Iterators1.
Stacks and Linked Lists. Abstract Data Types (ADTs) An ADT is an abstraction of a data structure that specifies – Data stored – Operations on the data.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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.
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
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,
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.
© 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.
Vectors, Lists, Sequences. Vectors Linear sequence s of n elements e rank – number of elements before e in s Vector supports access to elements via their.
Sequences1 Vectors Positions Lists General Sequences Bubble Sort Algorithm.
© 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.
Parasol Lab, Dept. CSE, Texas A&M University
Welcome to CSCE 221 – Data Structures and Algorithms
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,
CH 5 : STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Lists1 © 2010 Goodrich, Tamassia. Position ADT  The Position ADT models the notion of place within a data structure where a single object is stored 
LINKED LISTS.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
© 2004 Goodrich, Tamassia Queues. © 2004 Goodrich, Tamassia Stacks2 The Queue ADT The Queue ADT stores arbitrary objects Insertions and deletions follow.
Iterators and Sequences
Queues 5/11/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H.
Ch7. List and Iterator ADTs
Sequences and Iterators
Queues Queues Queues.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Sequences and Iterators
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Ch7. List and Iterator ADTs
Queues 11/22/2018 6:47 AM 5.2 Queues Queues Dr Zeinab Eid.
Vectors 11/29/2018 6:45 PM Vectors 11/29/2018 6:45 PM Vectors.
Lists and Sequences 12/8/2018 2:26 PM Sequences Sequences
Vectors, Lists and Sequences
Copyright © Aiman Hanna All rights reserved
Linked Lists & Iterators
Vectors, Lists, and Sequences
Vectors 4/26/2019 8:32 AM Vectors 4/26/2019 8:32 AM Vectors.
CS210- Lecture 6 Jun 13, 2005 Announcements
CS210- Lecture 7 Jun 14, 2005 Agenda Practice Session Vector
Vectors and Array Lists
Stacks and Linked Lists
Presentation transcript:

Vectors, Lists, and Sequences

Vectors: Outline and Reading The Vector ADT (§6.1.1) Array-based implementation (§6.1.2)

The Vector ADT 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) Main vector operations: – elemAtRank(int r): returns the element at rank r without removing it – replaceAtRank(int r, Object o): replace the element at rank r with o – insertAtRank(int r, Object o): insert a new element o to have rank r – removeAtRank(int r): removes the element at rank r Additional operations size() and isEmpty()

Applications of Vectors Direct applications – Sorted collection of objects (simple database) Indirect applications – Auxiliary data structure for algorithms – Component of other data structures

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] V 012n r N-1 0

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 012n r V 012n r V 012n o r

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 012n r V 012n o r V 012n r

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

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

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)

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 counterparts of insertAtRank( r) & removeAtRank( r) – STL also provides more general fcns for inserting & removing from arbitrary positions in the vector - these use iterators

Iterators An iterator abstracts the process of scanning through a collection of elements Methods of the ObjectIterator ADT: – boolean hasNext() – object next() – reset() Extends the concept of position by adding a traversal capability An iterator is typically associated with an another data structure We can augment the Stack, Queue, Vector, and other container ADTs with method: – ObjectIterator elements() Two notions of iterator: – snapshot: freezes the contents of the data structure at a given time – dynamic: follows changes to the data structure

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 element at the position indicated by I (analogous to our remove(p)) The functions can be used to insert/remove elements from arbitrary positions in the STL vector and list

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

Lists and Sequences

Outline and Reading Singly linked list Position ADT (§6.2.1) List ADT (§6.2.2) Sequence ADT (§6.3.1) Implementations of the sequence ADT (§ ) Iterators (§6.2.5)

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

List ADT (§6.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) Generic methods: – size(), isEmpty() 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)

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

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

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 A singly linked list consists of a sequence of nodes Each node stores element link to the next node next elem node LeonardSheldonHoward headtail  Raj

Exercise: Doubly-Linked List Nodes implement Position and store: element link to previous node link to next node Special head/tail nodes next elem node prev head tail LeonardSheldon HowardRaj 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

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

STL list class Functions in the STL list class – size() - return #elements in list, empty() - boolean – front(), back() - return references to first/last elements – push_front(e), push_back(e) - insert e at front/end – pop_front(), pop_back() - remove first/last element – 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 functions for inserting & removing from arbitrary positions in the list - these use iterators

List Summary List Operation Complexity for different implementations List Singly-LinkedList Doubly- Linked first(), last(), after(p) insertAfter(p,o), replaceElement(p,o), swapElements(p,q) O(1) before(p), insertBefore(p,o), remove(p) O(n)O(1) Size(), isEmpty()O(1)

Sequence ADT The Sequence ADT is the union of the Vector and List ADTs Elements accessed by – Rank, or – Position Generic methods: – size(), isEmpty() Vector-based methods: – elemAtRank(r), replaceAtRank(r, o), insertAtRank(r, o), removeAtRank(r) List-based methods: – first(), last(), before(p), after(p), replaceElement(p, o), swapElements(p, q), insertBefore(p, o), insertAfter(p, o), insertFirst(o), insertLast(o), remove(p) Bridge methods: – atRank(r), rankOf(p)

Applications 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

Sequence Implementations OperationArrayList size, isEmpty 11 atRank, rankOf, elemAtRank 1n first, last, before, after 11 replaceElement, swapElements 11 replaceAtRank 1n insertAtRank, removeAtRank nn insertFirst, insertLast 11 insertAfter, insertBefore n1 remove n1