Iterator for linkedlist traversal, Template - PowerPoint PPT Presentation

About This Presentation
Title:

Iterator for linkedlist traversal, Template

Description:

To let ListIterator access the private member head, we make ListIterator ... An iterator is a construct ... multimap _ Hash table, supports numerous values ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 28
Provided by: fuhb
Category:

less

Transcript and Presenter's Notes

Title: Iterator for linkedlist traversal, Template


1
Iterator for linked-list traversal,Template STL
COMP171 Fall 2005
2
Topics
  • Iterator for linked list traversal
  • Template
  • Standard Template Library

3
Node List Revisited
class Node public double data //
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) pri
vate Node head friend class ListIterator
To let ListIterator access the private member
head, we make ListIterator as a friend of List
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 ltlt currNode-gtdata ltlt endl currNode
currNode-gtnext
ListIterator listIter listIter.Reset(list) while
(!listIter) cout ltlt listIter ltlt "
" listIter
traverse using pointer
traverse using iterator
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
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
Operator
  • double operator()
  • returns data in the current node

double ListIteratoroperator() if
(!currNode) cout ltlt "Error the stack is
empty." ltlt endl return -1 return
currNode-gtdata
8
Operator !
  • bool operator!()
  • checks whether pointer is valid
  • bool ListIteratoroperator!()
  • return currNode ! NULL

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 ListIteratoroperator(int) //
first return value, then increment Node
tempNode currNode if (currNode) currNode
currNode-gtnext return tempNode Node
ListIteratoroperator() // first
increment, then return value if
(currNode) currNode currNode-gtnext return
currNode
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.InsertNo
de(1, 8.0) // print all the elements list.Disp
layList() ListIterator listIter // postfix
increment operator listIter.Reset(list)
Node node listIter cout ltlt "postfix " ltlt
node-gtdata ltlt endl // prefix increment
operator listIter.Reset(list) node
listIter cout ltlt "prefix " ltlt node-gtdata ltlt
endl // iteratively traverse the
list listIter.Reset(list) while (!listIter)
cout ltlt listIter ltlt " " listIter
cout ltlt endl return 0
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
Overview of Templates
  • template keyword tells the compiler that the
    class definition that follows will manipulate one
    or more unspecified types.

templateltclass Objectgt 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 Listltintgt list . . .
13
Linked List using template
  • Node with/without using template

class Node public double data Node next

templateltclass Objectgt class Node
public Object data Node next
  • templateltclass Objectgt
  • 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
Linked List using template
templateltclass Objectgt class List
public List(void) head NULL
List(void) bool IsEmpty() return head
NULL NodeltObjectgt InsertNode(int index,
Object x) int FindNode(Object x) int
DeleteNode(Object x) void DisplayList(void)
private NodeltObjectgt head
15
Linked List using template
templateltclass Objectgt ListltObjectgtList(void)
// desctructor NodeltObjectgt currNode
head, nextNode NULL while (currNode !
NULL) nextNode currNode-gtnext delete
currNode currNode nextNode
  • The first line indicates that Object is the
    template argument

16
templateltclass Objectgt NodeltObjectgt
ListltObjectgtInsertNode(int index, Object x)
if (index lt 0) return NULL int
currIndex 1 NodeltObjectgt currNode head w
hile (currNode index gt currIndex)
currNode currNode-gtnext currIndex
if (index gt 0 currNode NULL) return
NULL NodeltObjectgt newNode new NodeltObjectgt
newNode-gtdata x if (index 0)
newNode-gtnext head head newNode
else newNode-gtnext currNode-gtnext currNod
e-gtnext newNode return newNode
17
Linked List using template
templateltclass Objectgt int ListltObjectgtFindNode(
Object x) NodeltObjectgt currNode head int
currIndex 1 while (currNode
currNode-gtdata ! x) currNode currNode-gtnext
currIndex if (currNode) return
currIndex return 0
18
templateltclass Objectgt int ListltObjectgtDeleteNod
e(Object x) NodeltObjectgt prevNode NULL N
odeltObjectgt currNode head int
currIndex 1 while (currNode currNode-gtdata
! x) prevNode currNode currNode
currNode-gtnext currIndex if (currNode)
if (prevNode) prevNode-gtnext
currNode-gtnext delete currNode else
head currNode-gtnext delete
currNode return currIndex return 0
19
Linked List using template
templateltclass Objectgt void ListltObjectgtDisplayL
ist() int num 0 NodeltObjectgt
currNode head while (currNode !
NULL) cout ltlt currNode-gtdata ltlt
endl currNode currNode-gtnext num co
ut ltlt "Number of nodes in the list " ltlt num
ltlt endl
20
Using List
int main(void) // use integer type instead of
double Listltintgt 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) gt 0) cout ltlt "5
found" ltlt endl else cout ltlt "5 not found" ltlt
endl list.DeleteNode(x1) list.DisplayList()
return 0
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

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(...)

23
STL Algorithms Functions
  • Generic algorithms
  • All algorithms appear in the header file
    ltalgorithmgt
  • 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.

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 ltlistgt
  • using namespace std
  • list is a template container. In this example, we
    store integer data into the list.
  • typedef listltintgt LISTINT

25
STL Example
int rgTest1 5,6,7 int rgTest2
10,11,12 LISTINT listInt LISTINTiterator
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 ltlt
"listInt" for (i listInt.begin() i !
listInt.end() i) cout ltlt " " ltlt i cout ltlt
endl
26
STL Example
// Insert 3 fours listInt.insert (listInt.end(),
3, 4) // output 1 2 3 4 4 4 cout ltlt
"listInt" for (i listInt.begin() i !
listInt.end() i) cout ltlt " " ltlt i cout ltlt
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
ltlt "listInt" for (i listInt.begin() i !
listInt.end() i) cout ltlt " " ltlt i cout ltlt
endl
27
STL Example
// Insert another LISTINT LISTINT
listAnother listAnother.insert
( listAnother.begin(), rgTest2,
rgTest23) listInt.insert ( listInt.end(),
listAnother.begin(), listAnother.end()) //
output 1 2 3 4 4 4 5 6 7 10 11 12 cout ltlt
"listInt" for (i listInt.begin() i !
listInt.end() i) cout ltlt " " ltlt i cout ltlt
endl
Write a Comment
User Comments (0)
About PowerShow.com