/
Iterators, Lists, and Sequences Iterators, Lists, and Sequences

Iterators, Lists, and Sequences - PowerPoint Presentation

karlyn-bohler
karlyn-bohler . @karlyn-bohler
Follow
377 views
Uploaded On 2016-05-17

Iterators, Lists, and Sequences - PPT Presentation

Data Structures and Algorithms CS 244 Brent M Dingle PhD Department of Mathematics Statistics and Computer Science University of Wisconsin Stout Based on the book Data Structures and Algorithms in C Goodrich ID: 323593

adt list element position list adt position element linked iterators vector amp methods stl data remove push mydeque sequence return iterator elements

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Iterators, Lists, and Sequences" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Iterators, Lists, and Sequences

Data Structures and AlgorithmsCS 244

Brent M. Dingle, Ph.D.Department of Mathematics, Statistics, and Computer ScienceUniversity of Wisconsin – StoutBased on the book: Data Structures and Algorithms in C++ (Goodrich, Tamassia, Mount)Some content from Data Structures Using C++ (D.S. Malik)Slide2

Points of NoteCheck assignment due dates

HW08 due April 3 (today)HW09 due April 10Slide3

Last TimeLast class we talked about Linked Lists

Dynamic ArraysBook’s Vector ADTThe Vector ADT (§6.1.1)Array-based implementation (§6.1.2)StacksQueuesDequeuesSlide4

TodayReview: IteratorsIterators (§6.2.5

)  mostly FYILists and Sequences as ADTsPosition ADT (§6.2.1)  mostly FYI

List ADT (§6.2.2)Sequence ADT (§6.3.1)SortingSlide5

Iterators

Some functions supported by STL containers such as:begin(), end() return iterators to beginning or end of containerinsert(

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 listReviewSlide6

Iterators ExampleIn 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.cppCode example from this follows next slideSlide7

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

< char >

myDequeType

;Slide8

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

< char >

myDequeType

;Slide9

Group Class Activity

Create a program that instantiates a vector of integers using std::vectorThen using a loop Based on iterators begin() and end()Pushes the Fibonacci numbers into the vector object1 1 2 3 5…Starter Code can be found on D2L Circa:

Unit2InClassExamples GCA210_VectorIterators.tar.gzSubmit the code to the correctD2L dropbox1 submission per “group”Put all group member names in comments at top of main CPP fileForward and BackwardSlide10

Lists and SequencesSlide11

FYI Background: Position ADT

The Position ADT models the notion of place within a data structure where a single object is storedoften in the sense of relative positionsuch as A is before B, or Z is after Y

also in the sense of first, second, third, … lastrather than just at index iA special null position refers to no object.Positions provide a unified view of diverse ways of storing dataa cell of an arraya node of a linked listMember functions:Object& element(): returns the element stored at this positionbool isNull(): returns true if this is a null positionSlide12

Book’s List ADT (§6.2.2)

The List ADT models a sequence of positions storing arbitrary objectsestablishes 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 TypeSlide13

List ADT

Query methods:isFirst(p), isLast(p) :

return boolean indicating if the given position is the first or last, respectively.Accessor methodsfirst(), last(): return the position of the first or last, resp., element of San error occurs if S is emptybefore(p), after(p): return the position of the element of S preceding or following, respectively, the one at position pan error occurs if S is empty, or p is the first or last, resp., position

S is the listSlide14

List ADT

Update MethodsreplaceElement(p, o)Replace the element at position p with e

swapElements(p, q) Swap the elements stored at positions p & qinsertBefore(p, o), insertAfter(p, o),Insert a new element o into S before or after, resp., position pOutput: position of the newly inserted elementinsertFirst(o), insertLast(o)Insert a new element o into S as the first or last, resp., elementOutput: position of the newly inserted elementremove(p)Remove the element at position p from S

S is the listSlide15

Class Exercises Follow

You are about to see 2 exercisesMostly a compare and contrast exercise setThink about run times tooThis is a group discussion activityElect someone in your group to capture what is talked about in a MS-Word or text documentRandomly selected groups will present their findings at conclusion of each exerciseSlide16

Exercise 1 of 2

Describe how to implement the following list ADT operations using a singly-linked listlist ADT operations:

first(), last(), before(p), after(p)For each operation, explain how it is implemented and provide the running time

tail

Leonard

Sheldon

Howard

Raj

A

singly linked list

consists of a sequence of nodes

Each node stores

element

link to the next node

next

elem

node

Recall

headSlide17

Exercise 2 of 2

Describe how to implement the following list ADT operations

using a doubly-linked listlist 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

Leonard

Sheldon

Howard

Raj

Slide18

Solution: Singly Linked List

In the implementation of the List ADT by means of a singly linked listThe 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) timeAll the other operations of the List ADT run in O(1) timeEnhanced VersionSlide19

Solution: Doubly Linked List

In the implementation of the List ADT by means of a doubly linked listThe 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) timeEnhanced VersionSlide20

Variances of ImplementationThe details of implementing a list will varySo STL implementation does not always match the book’s described ADT

This is commonSo always check the interface description wherever you may workSlide21

STL list class

Functions in the STL list classsize() - return #elements in list, empty() - booleanfront(), back() - return references to first/last elementspush_front(e), push_back

(e) - insert e at front/end pop_front(), pop_back() - remove first/last elementlist() - creates an empty listSimilarities & Differences with book’s List ADTSTL front() & back() correspond to book’s first() & last() except the STL functions return the element & not its positionSTL push() & pop() are equiv to the book’s List ADT insert and remove when applied to the beginning & end of the listSTL also provides functions for inserting & removing from arbitrary positions in the list - these use iteratorsSlide22

List SummaryList Operation Complexity for different implementations

List Singly-Linked

List Doubly- Linked

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)

O(1)

Size(), isEmpty()

O(1)

O(1)Slide23

Sequence ADT

The Sequence ADT is the union of the Vector and List ADTsElements accessed byRank, orPositionGeneric 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)Slide24

Applications of Sequences

The Sequence ADT is a basic, general-purpose, data structure for storing an ordered collection of elementsDirect applications:Generic replacement for stack, queue, vector, or listsmall database (e.g., address book)

Indirect applications:Building block of more complex data structuresSlide25

Sequence Implementations

Operation

ArrayListsize,

isEmpty

1

1

atRank

,

rankOf

,

elemAtRank

1

n

first, last, before, after

1

1

replaceElement

,

swapElements

1

1

replaceAtRank

1

n

insertAtRank

,

removeAtRank

n

n

insertFirst, insertLast

1

1

insertAfter, insertBefore

n

1

remove

n

1

This is important!

It summarizes the differences

between an Array and a ListSlide26

Outline and Reading

Where we are headedBubble Sort (§6.4)Merge Sort (§11.1)Summary of sorting algorithmsSlide27

The End of This Part

NextBubble Sort and Merge

SortChapter 6 and 11.1