I'm reading the tutorials from cprogramming.com and I'm a bit stuck with their linked list example. The code is below:
#include <iostream>
using namespace std;
struct node {
int x;
node *next;
};
int main() {
node *root;
node *conductor;
root = new node;
root->next = 0;
(*root).x = 12; // I was testing alt. syntax.
conductor = root;
if(conductor != 0) {
while(conductor->next != 0) {
cout << conductor->x;
conductor = conductor->next;
}
}
conductor->next = new node;
conductor = conductor->next;
conductor->next = 0;
(*conductor).x = 42;
cout << conductor->x;
return 0;
}
In the example root->next
is being set to 0
. conductor
is then being set to the address of root
which means the while loop will never be reached right?
I don't understand the purpose of the example if it's not demonstrating the use of a linked list (i.e. adding more nodes, and traversing through them)开发者_开发知识库.
Am I analyzing the code correctly?
You're correct, in this example the while loop should be skipped as conductor->next will be 0 from the beginning.
This is probably a simple mistake and the author wants to show the way that a list is generally traversed through - and there his code is actually completely correct. My best guess is that he copy & pasted this code out of a (probably his own) LinkedList class and tried to assemble a quick example without testing it or thinking it through thoroughly. The way it works now, the while loop is basically meaningless since it doesn't do anything - but that probably wasn't the intention of the author.
This doesn't make sense at all
root = new node;
//then later
conductor = root;
if(conductor != 0) {
here conductor
can't be null - the check is meaningless, then
while(conductor->next != 0) {
here conductor
equals root
, so conductor->next
is null
conductor->next = new node;
And add to this that all created nodes are just leaked when the program ends. This sample doesn't demonstrate anything.
Conductor is then being set to the address of root which means the while loop will never be reached right?
If root
is non0
then conductor
is also non0
, So while()
loop will be definitely reached.
The code in your example, although it's probably describing something, doesn't do anything productive in my eyes :). Since before the while loop root and conductor are pointing at the same thing and the next of that object contains 0. So the while loop will never fire. After that we create a new node and let next point to it. Then we instead point conductor to the node and set it's next to 0. This would result in a memory leak.
root = new node;
root->next = 0;
Creates a new node
& initializes the next
pointer to 0
.
conductor = root;
sets conductor
pointer to address of root
if(conductor != 0)
{
while(conductor->next != 0)
{
cout << conductor->x;
conductor = conductor->next;
}
}
In order to add a new node one needs to navigate to the end of the link list, the above while loop does that.
Probably, this sample code was quickly put together by assembling pieces from an existing link list code.
This while loop is not really necessary if you know you have just one node in the link list. Probably, it was a part of the some function like addNodeToList()
in original code, which would need to navigate to end of list before adding new node to the list.
next = 0
marks the end of the list, similar to a zero-terminated string. It is a convention with linked lists in general. As you populate the list, you move the zero-terminated next
pointer forward (across the list). All other nodes have a valid next
value pointing to the next node in the list.
精彩评论