Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

COMP171 Fall 2005 Lists.
DATA STRUCTURES USING C++ Chapter 5
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Data Structures Using C++ 2E
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Linked List
Object Oriented Programming Elhanan Borenstein Lecture #12 copyrights © Elhanan Borenstein.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Stack and Queue COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
COMP103 - Linked Lists (Part B)1 Chapter 17 Linked List as Objects.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Data Structures Using C++ 2E
Templates and the STL.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
Data Structures Using C++ 2E
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Standard Template Library (STL)
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
Friends & Standard Template Library CSCI3110 Advanced Data Structures Lecturer: Dr. Carroll and Nan Chen.
Chapter 5 – Dynamic Data Structure Part 2: Linked List DATA STRUCTURES & ALGORITHMS Teacher: Nguyen Do Thai Nguyen
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Templates Class Templates Used to specify generic class types where class members data types can be specified as parameters, e.g. here is a generic List.
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
A Doubly Linked List prevnextdata There’s the need to access a list in reverse order header dnode.
Copyright © 2009 – Curt Hill Standard Template Library An Introduction.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Data Structure & Algorithms
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 17: Linked Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 18: Linked Lists.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
CMSC 202 Computer Science II for Majors. CMSC 202UMBC Topics Templates Linked Lists.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
C++ Programming:. Program Design Including
Lists CS 3358.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Standard Template Library (STL)
Chapter 4 Linked Lists.
Data Structures and Algorithms IT12112
Collections Intro What is the STL? Templates, collections, & iterators
Chapter 16-2 Linked Structures
Linked Lists.
Chapter 18: Linked Lists.
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Linked List.
Standard Template Library
Collections Intro What is the STL? Templates, collections, & iterators
Standard Template Library
Some Definitions vector, string, deque, and list are standard sequence containers. set, multiset, map, multimap, unordered_set, unordered_multiset, unordered_map.
Sequences 08/30/17 08/30/17 Unit III. Linked Lists 1.
Presentation transcript:

Iterator for linked-list traversal, Template & STL COMP171 Fall 2005

Template & STL/ Slide 2 Topics 1. Iterator for linked list traversal 2. Template 3. Standard Template Library

Template & STL/ Slide 3 Node & List Revisited class Node { public: doubledata;// data Node*next;// pointer to next node }; class List { public: List(void) { head = NULL; }// constructor ~List(void);// destructor bool IsEmpty() { return head == NULL; } Node* InsertNode(int index, double x); int FindNode(double x); int DeleteNode(double x); void DisplayList(void); private: Node* head; friend class ListIterator; }; To let ListIterator access the private member head, we make ListIterator as a friend of List

Template & STL/ Slide 4 List Traverse * An iterator is a construct that allows you to cycle through the data items in a data structure and perform an action on each item  Through overloading operators (e.g. !, *, ++ ), iterators hide the underlying implementation of ADT Node* currNode= head; while (currNode != NULL) { cout data << endl; currNode = currNode->next; } ListIterator listIter; listIter.Reset(list); while (!listIter) { cout << *listIter << " "; listIter++; } traverse using pointertraverse using iterator

Template & STL/ Slide 5 Class ListIterator We can use ListIterator to perform the following functions: Set the manipulation to start at the first item of the list: function reset Advance to the next node in the list: function operator++ Determine whether the current node is valid or not: function operator! Access the content of the current node: function operator*

Template & STL/ Slide 6 Class ListIterator class ListIterator { public: // constructor ListIterator() { currNode = NULL; } // set currNode to the first node of the list // note: friend feature; head is private in List void Reset(List &pList) { currNode = pList.head; } // return data in the current node double operator*(); // check whether currNode points to a valid node bool operator!(); // advance to next node (postfix operator) Node* operator++(int); // advance to next node (prefix operator) Node* operator++(); private: Node* currNode; };

Template & STL/ Slide 7 Operator * * double operator*() n returns data in the current node double ListIterator::operator*() { if (!currNode) { cout << "Error: the stack is empty." << endl; return -1; } return currNode->data; }

Template & STL/ Slide 8 Operator ! * bool operator!() n checks whether pointer is valid bool ListIterator::operator!() { return currNode != NULL; }

Template & STL/ Slide 9 Operator ++  The syntax operator++(int) is used to denote the postfix increment operator: listIter++  If you use operator++(void) instead, you declare a prefix increment operator: ++listIter Node* ListIterator::operator++(int) { // first return value, then increment Node* tempNode= currNode; if (currNode) currNode= currNode->next; return tempNode; } Node* ListIterator::operator++() { // first increment, then return value if (currNode) currNode= currNode->next; return currNode; }

Template & STL/ Slide 10 Using ListIter int main(void) { List list; // add items to the list list.InsertNode(0, 7.0); list.InsertNode(0, 5.0); list.InsertNode(0, 6.0); list.InsertNode(1, 8.0); // print all the elements list.DisplayList(); ListIterator listIter; // postfix increment operator listIter.Reset(list); Node* node =listIter++; cout data << endl; // prefix increment operator listIter.Reset(list); node = ++listIter; cout data << endl; // iteratively traverse the list listIter.Reset(list); while (!listIter) { cout << *listIter << " "; listIter++; } cout << endl; return 0; }

Template & STL/ Slide 11 Overview of Templates  In our previous implementation of Node/List, the data stored is double type  If you want a list that holds data of int type or other type, how do you do it?  Solution: use template  template makes algorithms and data structures type-independent

Template & STL/ Slide 12 Overview of Templates  template keyword tells the compiler that the class definition that follows will manipulate one or more unspecified types. template class List * When a list is declared, a specific type must be specified. The compiler then generates the actual class code from the template. int main(void) { // use integer type instead of double List list;... }

Template & STL/ Slide 13 Linked List using template  Node with/without using template class Node { public: doubledata; Node*next; }; template class Node { public: Objectdata; Node*next; }; * template Object is the substitution parameter, representing a type name. Object is used everywhere in the class where you would normally see the specific type (e.g. double ) the container holds.

Template & STL/ Slide 14 Linked List using template template class List { public: List(void) { head = NULL; } ~List(void); bool IsEmpty() { return head == NULL; } Node * InsertNode(int index, Object & x); int FindNode(Object & x); int DeleteNode(Object & x); void DisplayList(void); private: Node * head; };

Template & STL/ Slide 15 Linked List using template The first line indicates that Object is the template argument template List ::~List(void) { // desctructor Node * currNode = head, *nextNode = NULL; while (currNode != NULL) { nextNode = currNode->next; delete currNode; currNode = nextNode; }

Template & STL/ Slide 16 template Node * List ::InsertNode(int index, Object & x) { if (index < 0) return NULL; int currIndex=1; Node * currNode=head; while (currNode && index > currIndex) { currNode=currNode->next; currIndex++; } if (index > 0 && currNode == NULL) return NULL; Node * newNode=newNode ; newNode->data=x; if (index == 0) { newNode->next=head; head=newNode; } else { newNode->next=currNode->next; currNode->next=newNode; } return newNode; }

Template & STL/ Slide 17 Linked List using template template int List ::FindNode(Object & x) { Node * currNode=head; int currIndex=1; while (currNode && currNode->data != x) { currNode=currNode->next; currIndex++; } if (currNode) return currIndex; return 0; }

Template & STL/ Slide 18 template int List ::DeleteNode(Object & x) { Node * prevNode=NULL; Node * currNode=head; int currIndex=1; while (currNode && currNode->data != x) { prevNode= currNode; currNode= currNode->next; currIndex++; } if (currNode) { if (prevNode) { prevNode->next= currNode->next; delete currNode; } else { head= currNode->next; delete currNode; } return currIndex; } return 0; }

Template & STL/ Slide 19 Linked List using template template void List ::DisplayList() { int num=0; Node * currNode=head; while (currNode != NULL) { cout data << endl; currNode=currNode->next; num++; } cout << "Number of nodes in the list: " << num << endl; }

Template & STL/ Slide 20 Using List int main(void) { // use integer type instead of double List list; int x1 = 7, x2 = 5, x3 = 6, x4 = 8; // add items to the list list.InsertNode(0, x1); list.InsertNode(0, x2); list.InsertNode(0, x3); list.InsertNode(1, x4); // print all the elements list.DisplayList(); if(list.FindNode(x2) > 0)cout << "5 found" << endl; elsecout << "5 not found" << endl; list.DeleteNode(x1); list.DisplayList(); return 0; }

Template & STL/ Slide 21 Overview of STL * STL: Standard Template Library n A generic library that provides solutions to manage collections of data with modern and efficient algorithms. n The heart of the C++ standard library * STL includes the following components: n Data Structures – (vector, list, set, …) n Generic Algorithms – (for each DS: find, sort, …) n Object Functions – (e.g. math function, logic function,…) n Allocators

Template & STL/ Slide 22 STL Data Structures * Supported data structures n vector – dynamic array (supports resizing) n deque – a queue or a stack n list – doubly linked list n map – Hash table (key, value pairs) n multimap – Hash table, supports numerous values stored with each key n set – Hash table, storing keys only. Each key can only be stored once n multiset – Hash table, storing keys only. Each key can be stored several times * All DS supports: begin(), end(), insert(…), erase(…), clear(...)

Template & STL/ Slide 23 STL Algorithms & Functions * Generic algorithms n All algorithms appear in the header file n Include: find, find_if, count, replace, copy, sort, reverse and many more. * Object functions n Commonly used with STL. n Include: pow, sqrt, sin, other math functions, complex numbers functions, logic functions, comparison functions and many more.

Template & STL/ Slide 24 STL Example  A simple example using STL list list is defined under namespace std in list file n Add the following two lines in your.h/.cpp files #include using namespace std; list is a template container. In this example, we store integer data into the list. typedef list LISTINT;

Template & STL/ Slide 25 STL Example int rgTest1[] = {5,6,7}; int rgTest2[] = {10,11,12}; LISTINT listInt; LISTINT::iterator i; // Insert one at a time listInt.insert (listInt.begin(), 2); listInt.insert (listInt.begin(), 1); listInt.insert (listInt.end(), 3); // output: cout << "listInt:"; for (i = listInt.begin(); i != listInt.end(); i++) cout << " " << *i; cout << endl;

Template & STL/ Slide 26 STL Example // Insert 3 fours listInt.insert (listInt.end(), 3, 4); // output: cout << "listInt:"; for (i = listInt.begin(); i != listInt.end(); ++i) cout << " " << *i; cout << endl; // Insert an array at the end listInt.insert (listInt.end(), rgTest1, rgTest1 + 3); // output: cout << "listInt:"; for (i = listInt.begin(); i != listInt.end(); ++i) cout << " " << *i; cout << endl;

Template & STL/ Slide 27 STL Example // Insert another LISTINT LISTINT listAnother; listAnother.insert ( listAnother.begin(), rgTest2, rgTest2+3); listInt.insert ( listInt.end(), listAnother.begin(), listAnother.end()); // output: cout << "listInt:"; for (i = listInt.begin(); i != listInt.end(); ++i) cout << " " << *i; cout << endl;