I wrote a linked list class (templated) and I need to write an iterator class for using it of which I'm not very informed on syntax. How am I supposed to use the iterator to access the list? I found myself rewriting a couple of the linked list functions, but that's because the linked list class changed the current and head nodes, but the iterator doesn't. Do I need to write functions for retrieving the data from the iterator if the code is already written for the linked list class?
Here's the code. I'm not posting node.h, hopefully some things should be explanatory, but if you need, I can post it.
I started writing the iterator while working on overloading the = operator, reason being the parameter in the overloaded operator should be const, but as it is, I wouldn't be able to use it correctly.Code:#ifndef _LLIST_H #define _LLIST_H #include "node.h" #include <iostream> using namespace std; template <class T> class LList { private: Node<T> *head; Node<T> *curr; public: class Iterator { public: Node<T> *Pos; Iterator() { Node<T> Pos = new Node<T>; Pos = curr; } ~Iterator() { delete Pos; } bool next() { if ((Pos == NULL) || (Pos->getNext() == NULL)) return false; Pos = Pos->getNext(); return true; } bool prev() { if ((Pos == NULL) || (Pos == head)) return false; Node<T> *temp = Pos; Pos = head; while (Pos->getNext() != temp) Pos->next(); return true; } }; LList() { head = new Node<T>; curr = head; } ~LList() { seekStart(); while (remove()); } bool insert(const T &d) { if (curr == NULL) return false; Node<T> *temp = new Node<T>(d, curr->getNext()); curr->setNext(temp); return true; } bool remove() { if (curr->getNext() == NULL) return false; Node<T> *temp = curr->getNext(); curr->setNext(temp->getNext()); delete temp; return true; } const T& getData() { return curr->getNext()->getItem(); } bool isEmpty() { return head->getNext() == NULL; } bool next() { if ((curr == NULL) || (curr->getNext() == NULL)) return false; curr = curr->getNext(); return true; } bool prev() { if ((curr == NULL) || (curr == head)) return false; Node<T> *temp = curr; seekStart(); while (curr->getNext != temp) next(); return true; } void seekStart() { curr = head; } void seekEnd() { while (next()); } LList<T> operator = (LList<T> &rhs) { *this.head = rhs.head; *this.curr = rhs.curr; rhs.seekStart(); do { *this.insert(rhs.getData(); } while (rhs.next()); rhs.curr = *this.curr; } }; #endif