Download presentation
Presentation is loading. Please wait.
Published byScott Gaines Modified over 9 years ago
1
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005
2
Template & STL/ Slide 2 Topics 1. Iterator for linked list traversal 2. Template 3. Standard Template Library
3
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
4
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
5
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*
6
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; };
7
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; }
8
Template & STL/ Slide 8 Operator ! * bool operator!() n checks whether pointer is valid bool ListIterator::operator!() { return currNode != NULL; }
9
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; }
10
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; }
11
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
12
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;... }
13
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.
14
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; };
15
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; }
16
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; }
17
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; }
18
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; }
19
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; }
20
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; }
21
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
22
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(...)
23
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.
24
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;
25
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: 1 2 3 cout << "listInt:"; for (i = listInt.begin(); i != listInt.end(); i++) cout << " " << *i; cout << endl;
26
Template & STL/ Slide 26 STL Example // Insert 3 fours listInt.insert (listInt.end(), 3, 4); // output: 1 2 3 4 4 4 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: 1 2 3 4 4 4 5 6 7 cout << "listInt:"; for (i = listInt.begin(); i != listInt.end(); ++i) cout << " " << *i; cout << endl;
27
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: 1 2 3 4 4 4 5 6 7 10 11 12 cout << "listInt:"; for (i = listInt.begin(); i != listInt.end(); ++i) cout << " " << *i; cout << endl;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.