Iterators. node UML diagram implementing a double linked list the need for a deep copy. nested classes for iterator function objects

Similar documents
Lists. linking nodes. constructors. chasing pointers. MCS 360 Lecture 11 Introduction to Data Structures Jan Verschelde, 17 September 2010.

Queue Implementations

Templates and Vectors

B-Trees. nodes with many children a type node a class for B-trees. an elaborate example the insertion algorithm removing elements

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

Doubly-Linked Lists

Dynamic Data Structures

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

Hashing. mapping data to tables hashing integers and strings. aclasshash_table inserting and locating strings. inserting and locating strings

Intermediate Programming, Spring 2017*

Hashing. inserting and locating strings. MCS 360 Lecture 28 Introduction to Data Structures Jan Verschelde, 27 October 2010.

A linear structure is an ordered (e.g., sequenced) arrangement of elements.

Chapter 17: Linked Lists

CPSC 260 Data Structures and Algorithms for Computer Engineers Linked Lists!

Linked List using a Sentinel

the Stack stack ADT using the STL stack are parentheses balanced? algorithm uses a stack adapting the STL vector class adapting the STL list class

Jordan University of Science & Technology Department of Computer Science CS 211 Exam #1 (23/10/2010) -- Form A

CSCE 110 PROGRAMMING FUNDAMENTALS

Inheritance and Polymorphism

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

More on Templates. Shahram Rahatlou. Corso di Programmazione++

Simulations. buffer of fixed capacity using the STL deque. modeling arrival times generating and processing jobs

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

List, Stack, and Queues

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Structuur van Computerprogramma s 2

Expression Trees and the Heap

STL: C++ Standard Library

Priority Queues and Huffman Trees

CSCE 110 PROGRAMMING FUNDAMENTALS

DataStruct 9. Hash Tables, Maps, Skip Lists, and Dictionaries

W3101: Programming Languages C++ Ramana Isukapalli

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

lecture09: Linked Lists

Linked Lists. Gaddis Ch. 17. CS 2308 :: Spring 2016 Molly O'Neil

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

the Queue queue ADT using the STL queue designing the simulation simulation with STL queue using STL list as queue using STL vector as queue

Unified Modeling Language a case study

Object oriented programming

CSI33 Data Structures

CSCI-1200 Data Structures Fall 2009 Lecture 20 Hash Tables, Part II

Faculty of Information and Communication Technologies

CE221 Programming in C++ Part 1 Introduction

Outline. A C++ Linked Structure Class A C++ Linked List C++ Linked Dynamic Memory Errors In-class work. 1 Chapter 11: C++ Linked Structures

Motivation for Templates

The Standard Template Library. EECS 211 Winter 2018

Comp151. Generic Programming: Container Classes

CSCI 104 Templates. Mark Redekopp David Kempe

III. Classes (Chap. 3)

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

CSCI-1200 Data Structures Fall 2018 Lecture 21 Hash Tables, part 1

5 Phases of Software Life Cycle

Object Oriented Programming COP3330 / CGS5409

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

Quadratic Sorting Algorithms

This examination has 11 pages. Check that you have a complete paper.

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Chapter 17: Linked Lists

Lab 2: ADT Design & Implementation

Lecture Notes CPSC 122 (Fall 2014) Today Quiz 7 Doubly Linked Lists (Unsorted) List ADT Assignments Program 8 and Reading 6 out S.

Software Engineering Concepts: Invariants Silently Written & Called Functions Simple Class Example

CSCI-1200 Data Structures Fall 2017 Lecture 17 Trees, Part I

Containers: Queue and List. Jordi Cortadella and Jordi Petit Department of Computer Science

G52CPP C++ Programming Lecture 18

! A data structure representing a list. ! A series of dynamically allocated nodes. ! A separate pointer (the head) points to the first

Program template-smart-pointers-again.cc

Advanced C++ 4/13/2017. The user. Types of users. Const correctness. Const declaration. This pointer and const.

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Implementing Algorithms

CS

CS197c: Programming in C++

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

Object oriented programming

Unit 4 Basic Collections

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

Introducing C++ to Java Programmers

LECTURE 11 TREE TRAVERSALS

Lecture 23: Pointer Arithmetic

Today. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

EECE.3220: Data Structures Spring 2017

Absolute C++ Walter Savitch

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

1 Short Answer (5 Points Each)

COEN244: Class & function templates

CSCI 104 Templates. Mark Redekopp David Kempe

CSE100. Advanced Data Structures. Lecture 4. (Based on Paul Kube course materials)

Midterm Practice TA: Brian Choi Section Webpage:

Review Questions for Final Exam

CSE 333 Lecture smart pointers

CSCI-1200 Data Structures Spring 2015 Lecture 18 Trees, Part I

CSE 100: C++ TEMPLATES AND ITERATORS

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

CSCI-1200 Data Structures Fall 2017 Lecture 23 Functors & Hash Tables, part II

Sol. Sol. a. void remove_items_less_than(int arr[], int size, int value) #include <iostream> #include <ctime> using namespace std;

Spring 2008 Data Structures (CS301) LAB

Chapter 12: Searching: Binary Trees and Hash Tables. Exercises 12.1

Transcription:

Iterators 1 Double Linked and Circular Lists node UML diagram implementing a double linked list the need for a deep copy 2 Iterators on List nested classes for iterator function objects MCS 360 Lecture 12 Introduction to Data Structures Jan Verschelde, 25 September 2017 Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 1 / 31

Iterators 1 Double Linked and Circular Lists node UML diagram implementing a double linked list the need for a deep copy 2 Iterators on List nested classes for iterator function objects Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 2 / 31

Linked Lists A linked list is a sequence of nodes connected by pointers. We distinguish four types: 1 single linked list: one pointer to next node 2 double linked list: pointer to next and previous 3 circular list: last next points to first node 4 ordered list: if data type admits order The list in the Standard Template Library (STL) is double linked. The slist in the STL is a single linked list. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 3 / 31

Unified Modeling Language The node UML diagram of a double linked list: Node Item_Type data prev next The type of the data is templated, next points to the next node in the list, prev points to the previous node in the list. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 4 / 31

doubly linking nodes A linked list to store 3, 5, 4 looks like: Node Node Node NULL next: * prev: * data: 3 next: * prev: * data: 5 next: * prev: * data: 4 NULL At the expense of one extra pointer per node, reverse transversal becomes efficient. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 5 / 31

a circular list A circular list to store 3, 5, 4 looks like: Node Node Node next: * data: 3 next: * data: 5 next: * data: 4 Traversing a circular list is like modular addition, e.g.: n times nd = nd->next on a node nd in a list of length n leads to the same node nd. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 6 / 31

Iterators 1 Double Linked and Circular Lists node UML diagram implementing a double linked list the need for a deep copy 2 Iterators on List nested classes for iterator function objects Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 7 / 31

file mcs360_double_list.h #ifndef MCS360_DOUBLE_LIST_H #define MCS360_DOUBLE_LIST_H #define NULL 0 namespace mcs360_double_list template <typename T> class List private: #include "mcs360_double_node.h" Node *first; // points to first node Node *last; // points to last node public: // operations omitted ; #include "mcs360_double_list.tc" #endif Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 8 / 31

file mcs360_double_node.h #ifndef DNODE_H #define DNODE_H struct Node T data; // T is template parameter Node *next; // pointer to next node Node *prev; // pointer to previous node Node(const T& item, Node* next_ptr = NULL, Node* prev_ptr = NULL) : data(item), next(next_ptr), prev(prev_ptr) ; #endif The struct of C is a class where all is public. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 9 / 31

public methods In file mcs360_double_list.h: public: List(); List(T item); void append(t item); void write_forward(); void write_backward(); These methods form a very basic, first implementation. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 10 / 31

constructors in the file mcs360_double_list.tc namespace mcs360_double_list template <typename T> List<T>::List() first = NULL; last = NULL; template <typename T> List<T>::List(T item) first = new Node(item); last = first; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 11 / 31

a double linked list Appending to a list that stores 3, 5, 4: first last Node Node Node NULL next: * prev: * data: 3 next: * prev: * data: 5 next: * prev: * data: 4 NULL Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 12 / 31

the append() method template <typename T> void List<T>::append(T item) if(first == NULL) first = new Node(item); last = first; else if(first == last) first->next = new Node(item); first->next->prev = first; last = first->next; else last->next = new Node(item); last->next->prev = last; last = last->next; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 13 / 31

traversing forward and backward template <typename T> void List<T>::write_forward() Node *ptr = first; while(ptr!= NULL) std::cout << "->" << ptr->data; ptr = ptr->next; template <typename T> void List<T>::write_backward() Node *ptr = last; while(ptr!= NULL) std::cout << "->" << ptr->data; ptr = ptr->prev; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 14 / 31

Iterators 1 Double Linked and Circular Lists node UML diagram implementing a double linked list the need for a deep copy 2 Iterators on List nested classes for iterator function objects Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 15 / 31

testing the class #include "mcs360_double_list.h" #include <iostream> using namespace std; using namespace mcs360_double_list; int main() List<int> L(3); L.append(5); L.append(4); cout << " forward write :"; L.write_forward(); cout << endl; cout << "backward write :"; L.write_backward(); cout << endl; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 16 / 31

the need for a deep copy List<int> K = L; L.append(1); cout << " writing K after K = L :"; K.write_forward(); cout << endl; cout << " writing L after K = L :"; L.write_forward(); cout << endl; The assignment K = L is the shallow copy of pointers first and last as L changes, so does K. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 17 / 31

Iterators 1 Double Linked and Circular Lists node UML diagram implementing a double linked list the need for a deep copy 2 Iterators on List nested classes for iterator function objects Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 18 / 31

iterators on list Including write_forward and write_backward in definition of our class is not proper. In our test program we would like to use void write_with_iterator ( List<int> L ) for(list<int>::iterator item = L.begin(); item!= L.end(); ++item) cout << "->" << *item; cout << endl; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 19 / 31

nested classes With templates everything must be included in the header file, here mcs360_double_list.h. #include <stdexcept> namespace mcs360_double_list // omitted private part public: // omitted previous public #include "mcs360_list_iterator.h" friend class Iterator; The class Iterator must have access to the private part of List, therefore: friend. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 20 / 31

the class Iterator #ifndef MCS360_LIST_ITERATOR_H #define MCS360_LIST_ITERATOR_H class Iterator friend class List<T>; private: List<T> *parent; /* refers to the list */ typename List<T>::Node *current; /* pointer to the current node in parent */ Iterator( List<T> *L, Node *n ) : parent(l), current(n) Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 21 / 31

the public part T& operator*() const // dereferencing operator if(current == NULL) throw std::invalid_argument ("cannot dereference past end()"); return current->data; Iterator& operator++() // prefix increment operator if(current == NULL) throw std::invalid_argument ("cannot go past end()"); current = current->next; bool operator!=(const Iterator &other) return current!= other.current; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 22 / 31

begin() and end() At the end of mcs360_list_iterator.h, we put: Iterator begin() Iterator r(this,this->first); return r; Iterator end() Iterator r(this,null); return r; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 23 / 31

postfix increment operator The postfix increment operator first makes a copy of the current value of the iterator: Iterator operator++(int) // postfix increment operator Iterator return_value = *this; ++(*this); return return_value; Observe the prefix increment operator: Iterator& operator++() Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 24 / 31

Iterators 1 Double Linked and Circular Lists node UML diagram implementing a double linked list the need for a deep copy 2 Iterators on List nested classes for iterator function objects Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 25 / 31

function objects A frequent recurring application on sequences is to select items that match a criterion. Example: list all even numbers in a list. To define the criterion we overload the function call operator, the operator(). A class that overloads () is called a function class and an object of this class is a function object. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 26 / 31

class Divisible_By class Divisible_By private: int divisor; public: ; Divisible_By(int d) : divisor(d) bool operator()(int x) return x % divisor == 0; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 27 / 31

the find_if method The find_if method of the STL list class finds the first occurrence of an item that satisfies a criterion. template < typename T, typename P > T find_if( T first, T last, P pred ); If there is no item for which pred returns true, then last is returned. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 28 / 31

listing the even numbers int main() list<int> L; const int n = 20; srand(time(0)); for(int i=0; i<n; i++) L.push_back(rand() % 100); cout << "L : "; write(l); cout << "the even numbers of L : " << endl; even_numbers(l); cout << endl; return 0; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 29 / 31

applying find_if void even_numbers ( list<int> L ) list<int>::iterator i = L.begin(); while(i!= L.end()) list<int>::iterator j; j = find_if(i,l.end(),divisible_by(2)); if(j == L.end()) break; cout << " " << *j; while(i!= j) i++; i++; Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 30 / 31

Summary + Exercises Ended Chapter 4 on Sequential Containers. Exercises: 1 Define a copy operation on a double linked list. Show with a test program that the copy is deep: the original list does not change if the copy changes. 2 Write code for to insert item1 in a double linked list before item2. Ifitem2 does not occur in the list, then item1 is appended to the list. 3 Define a prefix decrement operator on the iterator. Write a program to write a list in reverse order. 4 Use the STL list and vector classes and give code to convert a list into a vector and vice versa. Introduction to Data Structures (MCS 360) Iterators L-12 25 September 2017 31 / 31