"joshd" <jo***@joshd.cawrote in message
news:11**********************@e3g2000cwe.googlegro ups.com
>>
Is it simply that you have a parent linked list and that each node
in the parent linked list contains a linked list?
Yes this is exactley what Im looking for. I just am unable to figure
out how to do it, maybe Im missing something simple or havent read the
correct articles. Could you please provide some information on doing
it this way.
>>
>>Can this be done without iterators? or should I study more to wrap
my mind around STL and iterators?
If you are writing your own linked list, then you can avoid
iterators. If you are using the standard library linked list, then
iterators are fundamental. Writing your own linked list can be a
useful learning exercise, but using the standard library containers
is almost always preferable for any program that isn't a learning
exercise. You should definitely learn the standard library
containers, along with iterators.
This is a learning exercise, I would really like to fully grasp the
concepts before moving onto something else. I dont like to
'partially' know what Im doing.
If the learning exercise includes writing the code for a linked list, then I
suggest you do that first, since it is fairly complicated, requiring a good
grasp of pointer manipulation.
Once you have a linked list class, then the rest of it isn't that hard, as I
will illustrate using std::list. The code uses structs with everything
public in order to keep it simple.
#include <list>
#include <iostream>
#include <string>
using namespace std;
// give the child some members for use in testing
struct ChildCell
{
size_t index;
string name;
ChildCell *pmatching;
ChildCell(size_t i, string nme) : index(i), name(nme), pmatching(0)
{}
ChildCell() : index(0), pmatching(0)
{}
};
struct ParentCell
{
list<ChildCellchildList;
int a, b;
};
int main()
{
list<ParentCellparentList;
// add two nodes to parentList
parentList.push_back(ParentCell());
parentList.push_back(ParentCell());
// Add four nodes to the child list in each parent node
// Do it for first parent node
list<ParentCell>::iterator it = parentList.begin();
for(size_t i = 0; i<4; ++i)
it->childList.push_back(ChildCell(i, "First_Child_List_Cell"));
// for later use, store a reference to the first childList
list<ChildCell& firstChildList = it->childList;
// Increment iterator to move to second parent node
// then add four nodes to its child list
++it;
for(size_t i = 0; i<4; ++i)
it->childList.push_back(ChildCell(i, "Second_Child_List_Cell"));
// for later use, store a reference to the second childList
list<ChildCell& secondChildList = it->childList;
// Now we make each cell in the child list store a pointer to its
// corresponding cell in the other list
list<ChildCell>::iterator it1, it2;
for(it1 = firstChildList.begin(),
it2 = secondChildList.begin();
it1!= firstChildList.end()
&& it2!= secondChildList.end();
++it1, ++it2)
{
it1->pmatching = &(*it2);
it2->pmatching = &(*it1);
}
// test that it worked
cout << "Iterating over first child list\n";
for(it1 = firstChildList.begin();
it1!= firstChildList.end(); ++it1)
{
cout << "Name is " << it1->name;
cout << " and index is " << it1->index << endl;
cout << "Name of corresponding cell is ";
cout << it1->pmatching->name;
cout << " and index is ";
cout << it1->pmatching->index << endl << endl;
}
cout << "\n\nIterating over second child list\n";
for(it2 = secondChildList.begin();
it2!= secondChildList.end(); ++it2)
{
cout << "Name is " << it2->name;
cout << " and index is " << it2->index << endl;
cout << "Name of corresponding cell is ";
cout << it2->pmatching->name;
cout << " and index is ";
cout << it2->pmatching->index << endl << endl;
}
return 0;
}
--
John Carson