Thread: linked list iterator class inheritience

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    linked list iterator class inheritience

    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.

    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
    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.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I believe I have it all setup, expect that I have no idea how to let the linked list class use it's child class. no matter which class i put first in the file, I can't use one without the other defined.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I once did something similar. I had both the Node class and the Iterator class as child classes of the LList class. I made the LList class a friend class to both the Node class and the Iterator class and the Node class and Iterator classes were each friends to each other. Leaving out all the template<> fallderah it looked something like this:

    Code:
    class LList
    {
       struct Node
       {
    	  friend class LList;
    	  friend class Iterator;  
    	  //etc.
       };
     
      class Iterator
    		  {
    	friend class LList;
    	friend struct Node;
    	//etc
      };
      
      //etc;
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  4. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM