Sunday, July 12, 2009

Is there a C++ expert around?? How do I write an iterator for my linked list class in C++??

so i want to write an iterator by writing a function/method like this





typedef Node%26lt;Type%26gt;* iterator





void operator++(Node%26lt;Type%26gt;* rhs){


rhs = rhs-%26gt;next;


};








but where would i put this?? and how would i template it?? would it be a member of the class, friend function, or do i have to make node a class??? also how would i do post increment??





i know post increment works like operator++(int)...but doesn't that have to be a class in order to call





list%26lt;int.%26gt; z;





list%26lt;int%26gt;::iterator x = z.begin();


++x;





here is my code.... thanks!





template %26lt;typename Type%26gt;


struct Node{





Node* next;


Node* previous;


Type data;





};





template %26lt;typename Type%26gt;


class list{





public:


//constructor


list();





//destructor


~list();





void push_back(Type);





void push_front(Type);





Type%26amp; get_front();





Type%26amp; get_back();





int size();








private:





// pointer to the front of the list


Node%26lt;Type%26gt;* front;





//pointer to the back of the list


Node%26lt;Type%26gt;* back;


};











template class list%26lt;int%26gt;;


template class list%26lt;double%26gt;;

Is there a C++ expert around?? How do I write an iterator for my linked list class in C++??
The iterator itself is a templated class.





template %26lt;class Type%26gt;


class Iterator {


...


}





The methods defined for the iterator are used to traverse any type that conforms to specific rules you define in the iterator. This can be linked nodes, a contigous array, or any other referencing method.





A good way to implement this is to create an abstract inherited class (e.g. Iterable), that contains the methods or attributes that the inheriting storage class must implement to provide iteration capabilities.





The iterator class itself will utilize the methods or attributes defined in the Iterable base class, ensuring that all instances of your iterator type will work with any storage type which implements the Iterable class definition.





e.g.





template %26lt;class Type%26gt;


class Iterator {


// Define iterator methods (e.g. operator++) which may


// use methods defined in 'class Iterable'


};





template %26lt;class Type%26gt;


class Iterable {


public:


...





// Resets iterator to begining of list


Type* begin(Iterator%26lt;Type%26gt;%26amp; itr) = 0;





// Resets iterator to end of list


Type* end(Iterator%26lt;Type%26gt;%26amp; itr) = 0;





// Returns the pointer to the first element in storage


// Used to determine iterator position through comparison


Type* begin() = 0;





// Returns the pointer to the last element in storage


// Used to determine iterator position through comparison


Type* end() = 0;





};








template %26lt;class Type%26gt;


class List : public Iterable%26lt;Type%26gt; {


...


};
Reply:u answered one off my c++ questions. i understand what you said some what. can you show me how i would apply that to this:


http://answers.yahoo.com/question/index;...





you can contact me through email if u like.

florist delivery

No comments:

Post a Comment