Click to See Complete Forum and Search --> : Need help with LinkList


RJabbar
June 27th, 2002, 05:03 PM
Got a class in which I need a double LinkList (X). This DLList X has another DoubleLinkList (Y) within its nodes.

Now I'm using STL List.h template. The issue I'm facing maybe very simple for you but a little confusing for me..... Hoping that someone can help me with it.

class Paul
{

public: Paul(); //Construction
~Paul(); //Destructor
CreateList();

private:
typedef struct XNode
{
long country;
list <CString> csCities;
};
list <XNode> XList,
}

Question:
a) how will I initialise this XList?
b) If I want to pass this xList as a ref variable in a funtion, how will that be done?
c) how will I create a new XNode? Basically, anything especial I have to do for csCities list creation?
c) How will the destructor work?

Thanks for your kind perusal.

jfaust
June 27th, 2002, 05:47 PM
Basically, it all just works. My guess is that your main confusion has to do with notation. What I always do is typedef the templated items as follows:

typedef std::list<CString> MyCStringList;
typedef MyCStringList::iterator MyCStringListIter;
typedef MyCStringList::const_iterator MyCStringListConstIter;

This way, you don't need to use the template parameter when accessing the class.

a) how will I initialise this XList?
It's initialized as soon as the class is initialized.

b) If I want to pass this xList as a ref variable in a funtion, how will that be done?
Simply pass it. The argument type would be CXNodeList& if you typedef as I described above.

c) how will I create a new XNode? Basically, anything especial I have to do for csCities list creation?
Nothing special needed.

c) How will the destructor work?
It will work just fine.

Jeff

RJabbar
June 28th, 2002, 04:21 PM
Thanks Jeff,
Ur typedef has made it simpler to see the structure and has helped me. You are also right in mentioning that I'm getting confused with the notations.

Couple of questions though....
a) when I'm creating a new XNode for the XList, how would I create the csCities linkedlist?

typedef std::list<XNode> MyNodeList
MyNodeList nList;

XNode xTemp;
xTemp.country = 12;

xTemp.csCities->list(); //error for creating list here

nList.push_front(xTemp);

b) How does iteration work? How would I traverse the node and check for country?

I know it would be simple but I'm lost... Is there any place from where I can reference examples for using this STD::list?

thanks for everyones time for coming here and seeing the possibilty of helping. Especial thanks to Jeff for taking the time to actually answering my questions.

jfaust
June 28th, 2002, 04:35 PM
The csCities linked list is created when you createteh XNode, although it will of course be empty.

You add items as follows:

xTemp.csCities.push_back("Chicago");

to iterate through the list, you would do:



for( MyCStringListConstIter iter = xTemp.csCities.begin(); iter != xTemp.csCities.end(); ++iter )
{
CString cityName = *iter;
...
}



I understand your confusion. It is somewhat difficult to learn. MSDN documentation is difficult and the source code is worse. Somebody else would be better at recommending a book--I started using STL on an SGI computer when there were no decent books available, at least none that I found. It was rather frustrating.

Stroustrup's book is good as a reference, but not necessarily to learn from.

Anybody have a book recommendation for an STL book?

Jeff

arunkumar_gona
June 28th, 2002, 04:37 PM
The C++ Standard Library -- Josuttis

Graham
June 28th, 2002, 04:38 PM
Matthew Austern's book is good as a reference, and Paul MacKenzie recommends Josuttis's book (I've not read it). Definitely read "Effective STL" by Scott Meyers.

RJabbar
June 28th, 2002, 05:05 PM
Thanks everyone......