My C++ Problem (doublelinkList iterator)

Page 1 of 1

Forum Index -> General

Australia April 07 2011 04:20Posts: 393
Right so here we go (THIS IS HUGE)

I have an assignment at the moment to implement an iterator for a double linked list. I am having trouble just getting started with this one, that is initializing some variables in the constructor because they are const references.
Spoiler - The Code
#include <iostream>

template<class DataType>
class NodeIterator
{
private:
enum IteratorStates { BEFORE, DATA , END };

IteratorStates fState;

typedef DoubleLinkedNode<DataType> Node;

const Node& fLeftmost;//Start
const Node& fRightmost;//End
const Node& fCurrent;

public:
typedef NodeIterator<DataType> Iterator;

//The problem is in this constructor, fLeftmost needs to be set to the beginning of the list and
//Rightmost the end (not aList)
NodeIterator( const Node& aList ): fCurrent(aList), fLeftmost(aList), fRightmost(aList){}

const DataType& operator*() const{} // dereference

Iterator& operator++(){}
Iterator operator++(int){} // postfix increment
Iterator& operator--(){} // prefix decrement
Iterator operator--(int){} // postfix decrement

bool operator==( const Iterator& aOtherIter ) const{}
bool operator!=( const Iterator& aOtherIter ) const{}

Iterator begin() const{ }
Iterator end() const{}
};
Things i have tried so far, Calling methods from the member constructor and inserting code into the member constructor.

Some more notes which might help:
In order to get fRightmost i need to go through the list getting the next node in the chain until i get to the end
Similar to fRightmost for fLeftmost i need to go through backwards towards the start
NEWSWRITER
Australia April 07 2011 05:07Posts: 99
As you have found, you cannot call instance methods from the initialiser. This is because it would be invalid to call methods that belong to the class instance before the class instance is even initialised properly.

To get around this, you must create static methods and call them instead. Static methods do not reference "this", so they are legal to call from within the initialiser.

Add these lines above your constructor.
static const Node& FindFirstNode(const Node& aList)
{
return aList; //actually do the find here
}

static const Node& FindLastNode(const Node& aList)
{
return aList; //actually do the find here
}
You then do your constructor + initialiser like this:
NodeIterator(const Node& aList)
: fCurrent(aList)
, fLeftmost(FindFirstNode(aList))
, fRightmost(FindLastNode(aList))
{}
Also, you should not provide those method implementations in your .h file. Do it in your .cpp file. Your .h file should be reserved for the class definition, the class implementation should be in the .cpp file.

Also, I personally recommend staying away from typedefs for as long as possible. In my opinion they obscure the code by making it more difficult to determine what type is actually being used. I want to know that I'm using a DoubleLinkedNode<DataType> when I read the code. I don't want to have to remember that "Node" means "DoubleLinkedNode<DataType>". If you want to be a lazy typist, get an IDE that has good Intellisense.
Charity, if you have the means, is a personal choice, but charity which is expected or compelled is simply a polite word for slavery. ADMIN
Australia Rostered Member for Proxiteam April 07 2011 06:45Posts: 427
Ratfinks a boss.
Timmy - "Making carriers really is a useful talent toi have!"
United States Rostered Member for Proxiteam April 07 2011 10:48Posts: 619

FighT wrote:

Ratfinks a boss.

Fuck yea he is. Aint nothin to a boss.
http://sc2sig.com/s/us/1614629-1.png NEWSWRITER
Australia April 07 2011 14:35Posts: 393
Thanks ratfink, heres the kicker. The header file has been provided for us and we cannot modify any of the methods or variables in the class we can only use what is provided. SO adding static methods and such i don't think is allowed. The reason it is all implemented in the header file is because it is a template you need to implement templates in the header because of a C++ "feature".

So is there any alternative you can think of other than creating two new methods? if not i'll just do it how u've suggested thanks so much for the help! =D
NEWSWRITER
Australia April 07 2011 18:58Posts: 99

Veom wrote:

Thanks ratfink, heres the kicker. The header file has been provided for us and we cannot modify any of the methods or variables in the class we can only use what is provided. SO adding static methods and such i don't think is allowed. The reason it is all implemented in the header file is because it is a template you need to implement templates in the header because of a C++ "feature".

Ah good point, I forgot about it being a template. Stupid C++ compilers make things so hard (C# (or any modern language) ftw).

I'm not sure how you're supposed to be able to initialise fLeftmost and fRightmost without calling another method to find them from aList. Does DoubleLinkedNode<T> have a method that does the search operation for you (I hope not; that responsibility should be the iterator's)? Is there another method on the iterator that iterates through a node forward (or back) to the start (or end) that I'm not seeing?

Frankly, I don't really understand the iterator structure you've got going here. The iterators I'm used to in C# are not immutable like this one seems to be (the fCurrent is const, and begin and end return ostensibly a new NodeIterator<DataType> instance, since they're const methods). But okay, let's say you're doing iterators in an immutable fashion and moving next with ++. Why do you need to store the fLeftmost and fRightmost... surely whether you're on the first or last node is self evident by looking at fCurrent.pPreviousNode (or whatever you've called it) and fCurrent.pNextNode and seeing if they're NULL. I guess it'd give you the ability to jump to the start or end of the list in an O(1) fashion, but the first and last nodes of a linked list should be stored by the LinkedList class (assuming you have one; I hope you do) and if you want to jump to the start or end, I think you should ask for a new iterator at those positions from the linked list class not from another iterator.

What I'm getting at is this is either an odd way of doing a LinkedList that I'm not familiar with, so I might be missing some esoteric way of solving this, or there's a bug in your assignment. Can you go ask your tutor or something?

I also just tried in my compiler (VS2010) not using static methods, just to make sure I'm not talking shit (it's been a long time since I wrote C++), turns out you can actually do it, but from what I'm reading you're not supposed to because its evil. Just an FYI. Typical horrible C++ letting you shoot yourself in the foot, I guess.
Charity, if you have the means, is a personal choice, but charity which is expected or compelled is simply a polite word for slavery. ADMIN
Australia April 07 2011 23:34Posts: 393
Yeah Ratfink thanks for the reply, i think something is wrong with the assignment becuase if fCurrent is a constant how can you iterate to the next node? seems a bit silly to me
NEWSWRITER
Australia Rostered Member for Proxiteam April 25 2011 08:53Posts: 30
o.o

What uni are you studying at Veom? I was doing an exercise very similar to this a couple of weeks ago!

However, I got nowhere with it because I suck at it. Curse required courses
scv ready

Page 1 of 1

Reply You must be logged in to comment. (Sign In)