List Iterator

Jul 8, 2008 at 4:59am
Thanks a lot.
Last edited on Jul 9, 2008 at 1:17am
Jul 8, 2008 at 1:51pm
You have a design problem here.

ListDBL is not a linked list. It's a node element of a linked list.

Your ListIterator is the linked list since it has the info for top, last current.

You should rename ListIterator to ListDBL and rename ListDBL to ListNode.

Then the ListNode should be a private embedded class within the new ListDBL.

That way your user won't be creating nodes but will have to use ListDBL methods to do it indirectly.

Next, an iterator is also an embedded class in the new ListDBL. Now for the begin() method all you need do is return an iterator with its position set to top. It should use ListDBL methods to get at top and last so there is only one place where top and last are maintained.

Maybe something like this:

[code=cpp]
class ListNode
{
public:
int value;
ListNode* next;
ListNode* previous;


};

class ListDBL
{
private:
ListNode*top;
ListNode*last;
ListNode*current;

public:
class Iterator
{
private:
ListDBL* theList; //for top and last
ListNode* iteratorPosition;
public:
void SetPosition(ListNode* arg);
};
Iterator begin();
};

ListDBL::Iterator ListDBL::begin()
{
ListDBL::Iterator theIterator;
theIterator.SetPosition(this->top);
return theIterator;
}

void ListDBL::Iterator::SetPosition(ListNode* arg)
{
this->iteratorPosition = arg;
}
[/code]

As you work through this you will find you will need different iterator classes for const iterators, forward iterators, reverse iterators, const forward iterators, etc..

Check out the STL list template and see what they did.

I assume this is a scholastic exercise since all of this work is already done for you in the STL.

Once you get everything working with a data type of int in the node, then convert your code to a template.
Jul 9, 2008 at 9:27am
Thank you so much for your help.
Would you please show me the rest of portion?

Thanks for your time.

(jowel40 AT yahoo DOT com)

Jul 9, 2008 at 1:27pm
I am willing to provide assistance but not do the whole job.

I would start off and if you get stuck again, just make another posting and we'll go from there.
Jul 12, 2008 at 10:42am
Thanks for your cooperation. Could you please have a look at the following question (i.e.Italic comment line)?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class outer{
  private:
  class inner1{
   private:
  // private member variable 
  public:
   //....
  //variable,constructor and member functions 
 
};
  class inner2{
   private:
  // private member variable 
  public:
   inner2 function1();
   inner2 function2(); 
  //....
  //constructor and other member functions
 };

public:
  outer();
  ~outer();
  void outer_function1();
  //....
  //other member functions 
  
};             

//....in main function..

outer obj;
obj.function1();
// Is it possible? If not, how can the public function of inner2 be called using object of outer class?(i have tried making friend class )

I've placed operator overloaded code (++/--/*etc) at the public section of the Iterator class (on the post of 'weaknessforcats') its but not working.
Could you please tell me something on it?
Jul 12, 2008 at 2:47pm
function1() is a member function of outer::inner2, which is private. You cannot access private member functions outside the class.

Also, even if outer::inner2 were public, you would still need to create an outer::inner2 object inorder to call the method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class outer
{
  private:
	class inner1
	{
	 private:
	 // private member variable 
	 public:
	  //....
	  //variable,constructor and member functions 
 
	};
public:                                           //<-----------------------------------
	class inner2
	{
	  private:
	 // private member variable 
	 public:
	  inner2 function1();
	 inner2 function2(); 
	 //....
  //constructor and other member functions
	 };

public:
  outer();
  ~outer();
  void outer_function1();
  //....
  //other member functions 
  
};             

//....in main function..
int main()
{
outer::inner2 obj;
obj.inner2::function1();
}
Topic archived. No new replies allowed.