I have been reading this tutorial on linked lists (http://www.cprogramming.com/tutorial/lesson15.html) and I got this code from the site, which I put into a compiler and ran it. It ran fine, I just don't understand the behavior of the code.
 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include using namespace std; struct node{ int x; node *next; }; int main(){ node *root; node *conductor; root = new node; root->next=0; root->x=12; conductor=root; if (conductor!=0){ while(conductor->next !=0){ cout<x<x<next = new node; conductor = conductor->next; conductor->next = 0; conductor->x = 42; //Any number return 0; }``````

I have tried commenting out parts of code, but I still don't understand how the code works, specifically how many nodes are created, why the x value isn't printed in the loop, and how the pointer next works.

Please explain if you can. Thanks!
At first only one node is created.

root = new node;

After the statement

conductor=root;

both pointers point the same node dynamically allocated in the statement demonstrated above.

The member of the node next was set to zero

root->next=0;

So the loop

while(conductor->next !=0){

will iterate never ( conductor is equal to root and root->next is equal to 0) because the condition is false.

In the statement following the loop member x (equal to 12) of the single node is printed

cout<<conductor->x<<endl;

Then a second node is created

conductor->next = new node;
and its members are set correspondingly to 0 and 42

conductor->next = 0;
conductor->x = 42; //Any number

Last edited on
How come I can't set next to 1 before the loop begins?
I have not understood your question.
 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include using namespace std; struct node{ int x; node *next; }; int main(){ node *root; node *conductor; root = new node; root->next=0; //When I try to set this to 1 I get an error saying invalid conversion from 'int' to 'node*' root->x=12; conductor=root; if (conductor!=0){ while(conductor->next !=0){ cout<x<x<next = new node; conductor = conductor->next; conductor->next = 0; conductor->x = 42; //Any number return 0; }``````
Why are you trying to set the pointer to integer value 1?! The pointer shall either contain a valid address of an object of type node or 0.
So I would have to find the memory address of an object of type node and set the pointer to that?
You should not find an address. You should use the address of newly created node that you are going to add to the linked list.
#include <iostream>
#include <conio.h>

using namespace std;

//******************
//The node class
//******************
template <typename T>
class nodeType {
public:
T info;
nodeType *forward;
nodeType *backward;
};

//******************
//This contains within it a class declaration for an iterator
//******************
template <typename T>
public:

//public members of the doublyLinkedList class

void insertFirst(const T&);
void insertLast(const T&);
void deleteNode(const T&);
void deleteAllOfOccurrence(const T&);
void deleteKthElement(const T&);
T& getKthElement(const T&);
void deleteSmallest();

//The iterator class
class iterator {
friend class doublyLinkedList; //friended so the linked list class can modify the iterator.
public:
iterator();
T& operator*() const;
iterator operator++();
iterator operator-(const int amount) const; //For your assignment
iterator operator+(const int amount) const; //For your assignment
T& operator[](const int index) const; //For your assignment

bool operator==(const iterator& right) const;
bool operator!=(const iterator& right) const;
private:
nodeType<T> *current; //Where the iterator is pointing to
//Note, to know when you are at either end of the node, just look for NULL
bool pastBoundary; //This helps us lets us know if we've gone past an end of the list, but still allows us to point to the end of the list.
};

iterator begin() const;
iterator end() const;

protected:
nodeType<T> *first;
nodeType<T> *last;
int count;
};

template <typename T>
first = NULL;
last = NULL;
count = 0;
}

template <typename T>
nodeType<T> *temp;
while (first != NULL) {
temp = first;
first = first->forward;
delete temp;
}
last = NULL;
count = 0;
}

 ``1234`` `````` node *next = &new node //Not exact code, but basically is this what I would do? ``````