Hi, I've been staring at the screen for 30 hours it seems and haven't gotten a single "A-ha!" light bulb, so I'm reaching out to the pro's. I have a couple of linked lists inside a node struct and can't figure out how to print this list.
How do I print out the contents inside *listA and *listB? I can print out the contents of my main list this way:
1 2 3 4 5 6 7 8 9 10 11 12
Node * firstlist;
..... // some code filling up *firstlist here.
const Node * temp (firstlist);
while (temp != NULL)
{
cout << temp -> data << endl;
temp = temp -> next;
}
I tried doing this to print the contents of listA:
cout << temp -> listA -> data << endl;
But apparently the compiler doesn't understand this. I've also searched all over the web and documentation for this is almost non-existent (or difficult to find). Any thoughts much appreciated!
I thought you had a linked list (connected by next), and each node was the head of two distinct linked lists (listA and listB).
I think you're asking how to print a node. Well, you'd make a generic print function:
1 2 3 4 5 6 7 8 9 10 11 12 13
void print_node(std::ostream &os, const Node* node)
{
for (const Node* temp = node; temp != NULL; temp = temp->next)
{
os << temp->data << endl;
for (const Node* p = temp->listA; p!= NULL; p = p->next)
os << p->data << endl;
for (const Node* p = temp->listB; p!= NULL; p = p->next)
os << p->data << endl;
}
}
Then you could call it like:
1 2 3 4 5
print_node(std::cout, firstlist); // print entire list
if (firstlist)
print_node(std::cout, firstlist->listA); // print listA of first node
if (firstlist)
print_node(std::cout, firstlist->listB); // print listB of first node
Actually just realized--I need to set listA equal to another linked list. The other linked list was created elsewhere in the program, for example Node * somestuff. How can I set listA equal to *somestuff? Is it possible to simply do listA = somestuff? Basically, I'd like to copy the contents of Node * somestuff into listA.