Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
-
Upload
scott-gaines -
Category
Documents
-
view
223 -
download
0
Transcript of 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 Revisitedclass Node {public:
double data; // dataNode* 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 << currNode->data << endl;currNode = currNode->next;
}
ListIterator listIter;listIter.Reset(list);while (!listIter) {
cout << *listIter << " ";listIter++;
}
traverse using pointer traverse 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 ListIteratorclass ListIterator {public:
// constructorListIterator() { currNode = NULL; }// set currNode to the first node of the list// note: friend feature; head is private in Listvoid Reset(List &pList) { currNode = pList.head; }// return data in the current nodedouble operator*();// check whether currNode points to a valid nodebool 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*() 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!() 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: ++listIterNode* 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 ListIterint main(void) {List list;// add items to the listlist.InsertNode(0, 7.0);list.InsertNode(0, 5.0);list.InsertNode(0, 6.0);list.InsertNode(1, 8.0);// print all the elementslist.DisplayList();ListIterator listIter;// postfix increment operator
listIter.Reset(list);Node* node = listIter++;cout << "postfix: " << node->data << endl;// prefix increment operatorlistIter.Reset(list);node = ++listIter;cout << "prefix: " << node->data << endl;// iteratively traverse the listlistIter.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 Object> 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 doubleList<int> list;
. . .}
Template & STL/ Slide 13
Linked List using template Node with/without using template
class Node {public:
double data;Node* next;
};
template<class Object>class Node {public:
Object data;Node* next;
};
template<class Object> 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 templatetemplate<class Object>class List {public:
List(void) { head = NULL; }~List(void);bool IsEmpty() { return head == NULL; }Node<Object>* InsertNode(int index, Object & x);
int FindNode(Object & x);int DeleteNode(Object & x);void DisplayList(void);
private:Node<Object>* head;
};
Template & STL/ Slide 15
Linked List using template
The first line indicates that Object is the template argument
template<class Object>List<Object>::~List(void) { // desctructor
Node<Object>* currNode = head, *nextNode = NULL;while (currNode != NULL){
nextNode = currNode->next;delete currNode;currNode = nextNode;
}}
Template & STL/ Slide 16
template<class Object>Node<Object>* List<Object>::InsertNode(int index, Object & x) {
if (index < 0) return NULL;
int currIndex = 1;Node<Object> * currNode = head;while (currNode && index > currIndex) {
currNode = currNode->next;currIndex++;
}if (index > 0 && currNode == NULL) return NULL;
Node<Object> * newNode = new Node<Object>;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<class Object>int List<Object>::FindNode(Object & x) {
Node<Object>* 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<class Object>int List<Object>::DeleteNode(Object & x) {
Node<Object>* prevNode = NULL;Node<Object>* 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<class Object>void List<Object>::DisplayList(){
int num = 0;Node<Object>* currNode = head;while (currNode != NULL){
cout << currNode->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 doubleList<int> list;int x1 = 7, x2 = 5, x3 = 6, x4 = 8;// add items to the listlist.InsertNode(0, x1);list.InsertNode(0, x2);list.InsertNode(0, x3);list.InsertNode(1, x4);// print all the elementslist.DisplayList();if(list.FindNode(x2) > 0) cout << "5 found" << endl;else cout << "5 not found" << endl;list.DeleteNode(x1);list.DisplayList();return 0;
}
Template & STL/ Slide 21
Overview of STL
STL: Standard Template Library A generic library that provides solutions to manage
collections of data with modern and efficient algorithms. The heart of the C++ standard library
STL includes the following components: Data Structures – (vector, list, set, …) Generic Algorithms – (for each DS: find, sort, …) Object Functions – (e.g. math function, logic function,…) Allocators
Template & STL/ Slide 22
STL Data Structures Supported data structures
vector – dynamic array (supports resizing) deque – a queue or a stack list – doubly linked list map – Hash table (key, value pairs) multimap – Hash table, supports numerous values stored
with each key set – Hash table, storing keys only. Each key can only be
stored once 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 All algorithms appear in the header file <algorithm> Include: find, find_if, count, replace, copy, sort,
reverse and many more.
Object functions Commonly used with STL. 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 Add the following two lines in your .h/.cpp files
#include <list>
using namespace std;
list is a template container. In this example, we store integer data into the list.typedef list<int> 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 timelistInt.insert (listInt.begin(), 2);listInt.insert (listInt.begin(), 1);listInt.insert (listInt.end(), 3);// output: 1 2 3cout << "listInt:";for (i = listInt.begin(); i != listInt.end(); i++)
cout << " " << *i;cout << endl;
Template & STL/ Slide 26
STL Example
// Insert 3 fourslistInt.insert (listInt.end(), 3, 4);// output: 1 2 3 4 4 4cout << "listInt:";for (i = listInt.begin(); i != listInt.end(); ++i)
cout << " " << *i;cout << endl;// Insert an array at the endlistInt.insert (listInt.end(), rgTest1, rgTest1 + 3);// output: 1 2 3 4 4 4 5 6 7cout << "listInt:";for (i = listInt.begin(); i != listInt.end(); ++i)
cout << " " << *i;cout << endl;
Template & STL/ Slide 27
STL Example
// Insert another LISTINTLISTINT 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 12cout << "listInt:";for (i = listInt.begin(); i != listInt.end(); ++i)
cout << " " << *i;cout << endl;