How to print a linked list located inside another linked list struct?

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.

1
2
3
4
5
6
7
8
struct Node
{
   int data;
   Node * listA;
   Node * listB;
   Node * next;
   Node (int);
};


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!
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Node * firstlist;

..... // some code filling up *firstlist here.

for (const Node* temp(firstlist); temp != NULL; temp = temp->next)
{
   cout << temp->data << endl;

   for (const Node* p = temp->listA; p!= NULL; p = p->next)
      cout << p->data << endl;

   for (const Node* p = temp->listB; p!= NULL; p = p->next)
      cout << p->data << endl;
}
Thanks kbw, I'll test this out.

Is there a way to assign a Node * temp to listA? After filling a Node * temp linked list can I just do temp = listA and print out contents of listA?
Last edited on
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 
Last edited on
This looks right but my brain is no longer working, haven't slept all night. Will come back again tonight and test this out! Thanks!
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.
Last edited on
Is it possible to simply do listA = somestuff?
Yes.
Topic archived. No new replies allowed.