Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.

Slides:



Advertisements
Similar presentations
Chapter 3 Lists Dr Zeinab Eid.
Advertisements

© 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.
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.
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.
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.
© 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 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.
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.
General Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Mohammad Amin Kuhail M.Sc. (York, UK) University of Palestine Faculty of Engineering and Urban planning Software Engineering Department Computer Science.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
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,
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Priority Queues and Sorting CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science.
Linked Lists part 1 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
© 2004 Goodrich, Tamassia Vectors1 Vectors and Array Lists.
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.
Lecture 7 : Intro. to STL (Standard Template Library)
Merge Sort Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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,
Priority Queues and Heaps Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University.
Reading from a file, Sorting, and a little Searching Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics,
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 
Priority Queues CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
Chapter 2: Basic Data Structures. Spring 2003CS 3152 Basic Data Structures Stacks Queues Vectors, Linked Lists Trees Priority Queues and Heaps Dictionaries.
Iterators and Sequences
Sequences and Iterators
Queues Queues Queues.
Lists and Sequences 9/21/2018 7:21 PM Sequences Sequences
Prof. Michael Neary Lecture 7: The STL Prof. Michael Neary
Sequences and Iterators
More on Merge Sort CS 244 This presentation is not given in class
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
CS212D: Data Structures Week 5-6 Linked List.
Recursive Linked List Operations
Review and Prepare for Test 2
Vectors, Lists and Sequences
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:

Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin – Stout Based on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount) Some content from Data Structures Using C++ (D.S. Malik)

Points of Note Check assignment due dates HW08 due April 3 (today) HW09 due April 10

Last Time Last class we talked about Linked Lists Dynamic Arrays Book’s Vector ADT The Vector ADT (§6.1.1) Array-based implementation (§6.1.2) Stacks Queues Dequeues

Today Review: Iterators Iterators (§6.2.5)  mostly FYI Lists and Sequences as ADTs Position ADT (§6.2.1)  mostly FYI List ADT (§6.2.2) Sequence ADT (§6.3.1) Sorting

Iterators Some functions supported by STL containers such as: – 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 linked list operation: insertBefore(p) – erase(I) – removes the element at the position indicated by I – analogous to our linked list operation: remove(p) The functions can be used to insert/remove elements from arbitrary positions in the STL vector and list Review

Iterators Example In the previous class there was an exercise that was based on the code: Located at or near something like: Content  Unit2  InClass  Examples  Ex225_DequeWithIters.cpp Code example from this follows next slide

Iterator Example myDeque.push_back( 'H' ); myDeque.push_back( 'O' ); myDeque.push_back( 'W' ); myDeque.push_back( 'D' ); myDeque.push_back( 'Y' ); // // Change them back but this time use iterators // instead of reference pointers. // Note how we dereference the iterators with * when setting them. myDequeType::iterator it_begin = myDeque.begin(); myDequeType::iterator it_end = myDeque.end(); *it_begin = 'H'; // Change the first element from h back to H --it_end; *it_end = 'Y'; // Change the last element from y to back to Y // typedef deque myDequeType;

Iterator Example 2 void printContents( myDequeType deque ) { // // Using iterators, // which point to the beginning and ending of the vector, // loop through the vector and print out its contents myDequeType::iterator it_begin = deque.begin(); myDequeType::iterator it_end = deque.end(); cout << "Contents of myDeque: "; while (it_begin != it_end ) { cout << *it_begin << " " ; ++it_begin } cout << endl; } // typedef deque myDequeType;

Group Class Activity Create a program that instantiates a vector of integers using std::vector Then using a loop Based on iterators begin() and end() Pushes the Fibonacci numbers into the vector object … Starter Code can be found on D2L Circa: Unit2  InClass  Examples  GCA210_VectorIterators.tar.gz Submit the code to the correct D2L dropbox 1 submission per “group” Put all group member names in comments at top of main CPP file Forward and Backward

Lists and Sequences

FYI Background: Position ADT The Position ADT models the notion of place within a data structure where a single object is stored – often in the sense of relative position such as A is before B, or Z is after Y – also in the sense of first, second, third, … last rather than just at index i A special null position refers to no object. Positions provide a unified view of diverse ways of storing data – 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

Book’s 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) This is NOT a linked list description It is a LIST Abstract Data Type

List ADT Query methods: – isFirst(p), isLast(p) : return boolean indicating if the given position is the first or last, respectively. 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, respectively, the one at position p an error occurs if S is empty, or p is the first or last, resp., position S is the list

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 S is the list

Class Exercises Follow You are about to see 2 exercises Mostly a compare and contrast exercise set Think about run times too This is a group discussion activity Elect someone in your group to capture what is talked about in a MS-Word or text document Randomly selected groups will present their findings at conclusion of each exercise

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

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

Solution: Singly Linked List In the implementation of the List ADT by means of a singly 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) The before() operation runs in O(n) time All the other operations of the List ADT run in O(1) time Enhanced Version

Solution: Doubly Linked List 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 Enhanced Version

Variances of Implementation The details of implementing a list will vary So STL implementation does not always match the book’s described ADT This is common So always check the interface description wherever you may work

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 book’s first() & last() except the STL functions return the element & not its position STL push() & pop() are equiv to the book’s 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 This is important! It summarizes the differences between an Array and a List

Outline and Reading Where we are headed Bubble Sort (§6.4) Merge Sort (§11.1) Summary of sorting algorithms

The End of This Part Next Bubble Sort and Merge Sort Chapter 6 and 11.1