Please explain this

This's just a part of the code. (Linked list)

1
2
3
4
5
6
7
8
9
10
11
12
  chainNode* sourceNode = theList.firstNode; //node in theList to copy from
  firstNode = new chainNode (sourceNode->data); //copy first data of theList
  sourceNode = sourceNode->next;
  chainNode* targetNode = firstNode; //current last node in *this

while(sourceNode != NULL)
{
targetNode->next = new chainNode(sourceNode->element);
targetNode = targetNode->next;
sourceNode = sourceNode->next;
}
targetNode->next = NULL; //end the chain 


Edit: Corrected targetNOde to targetNode
Last edited on
This is just part of the answer...

Although we can infer a few things about what we are looking at, you don't even provide the classes prototype or even a specific question for that matter. So how are we supposed to know for certain what the end result is meant to be? What you have posted already has twice as many comments as I am used to seeing on any ones code so you'll have to be specific about the part that you don't get.

Also, based on that typo on Line 9 I don't trust that some other part of this wasn't copied wrong. What is so hard about highlight -> copy -> paste? Or a direct link to the source?
It's simple..I'd to copy the snippet from a book which not many are familiar with
(http://www.amazon.in/Data-Structures-Algorithms-And-Applications/dp/0929306325)

I was learning data structures very recently and presumed that this code and the terms regarding linked lists are familiar to most of them. My bad..

chainNode = name of the node class of a linked list whose data members are data(data-field) and next(link field);
theList = name of the list;

I'm confused with what the code is trying to do and what the variables firstNode, targetNode, and sourceNode are actually "doing"?If you can plz explain line-by-line!

@Computergeek, if it's still "very" unclear that you can't infer out..please let me know! (:
All these answers and more on the next exciting episode of "Knightrider29"

Computergeek is right on this one, we need more specific questions.

However, the three variables you asked about are not "doing" much of anything useful. Especially this syntax error targetNOde->next;

However if you move through it line by line, which you should do, you'll see target list is making a copy of source list, exactly like the comments say. You may want to look more into linked lists in general to get a better feel for how they work- and if you're just starting with data structures there are things that would serve you better to spend your time learning (such as standard containers like vectors and maps)
Topic archived. No new replies allowed.