Linked Lists

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#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;
conductor=root;

if (conductor!=0){
while(conductor->next !=0){
cout<<conductor->x<<endl;
    }
  }

cout<<conductor->x<<endl;

conductor->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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

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<<conductor->x<<endl;
    }
  }

cout<<conductor->x<<endl;

conductor->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;
};



//******************
//The linked list base class
//This contains within it a class declaration for an iterator
//******************
template <typename T>
class doublyLinkedList {
public:

//public members of the doublyLinkedList class
doublyLinkedList();
~doublyLinkedList();

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--(); //For your assignment
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>
doublyLinkedList<T>::doublyLinkedList() {
first = NULL;
last = NULL;
count = 0;
}

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

@Vlad from Moscow:

So
I would have to declare a variable that equals the address of the new node?

Such as
:
1
2
3
4

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

Topic archived. No new replies allowed.