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)
26
Embed
Iterators, Lists, and Sequences Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
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:
//--------------------------------------------------------------------- // Change them back but this time use iterators // instead of reference pointers. // Note how we dereference the iterators with * when setting them.
*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;
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
• 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
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
Reca
ll
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
Reca
ll
head tail
Leonard Sheldon Howard Raj
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
List Summary
• List Operation Complexity for different implementations