Question about class constructors

So here is the question about this linked list. I understand linked list more then ever ,but the problem I am having now is with this. Listhenodes::Listthenodes() head = NULL. Here is what throws me off the functions say if (head != NULL) .... But in Listthenodes::Listthenodes head is set to NULL what is going on someone explain it for me.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Listthenodes::Listthenodes()
{
    head = NULL;  //Head = NULL?
    temp = NULL;
    curr = NULL;
}
void Listthenodes::Addnode(int addData)
{
    Nodeptr n = new nodes;
    n->next = NULL;
    n->Numbers = addData;

    if (head != NULL) // Head != NULL? But this if statement still runs
    {
        curr = head;
        while (curr->next != NULL){
            curr = curr->next;
        }
        curr->next = n;
    }
Your abstractions are wrong. A linked list is a list of linked nodes. So Listthenodes is not a class, it's a member on a linked list or a function that takes a linked list as a parameter.

It makes no sense for a class called Listthenodes to have a member called Addnode.
Hey @kbw I changed the name and showed more of the code. Can you help me and answer my question please.

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
37
38
39
40
41
42
43
44
45
46
47
48
class thenodes
{
private:
    typedef struct nodes
    {
        int Numbers;
        nodes* next;

    }*Nodeptr;

Nodeptr head;
Nodeptr curr;
Nodeptr temp;

public:
    thenodes();
void Addnode(int addData);
void DeleteNode(int delnode);
void DisplayData ();

};
thenodes::thenodes()
{
    head = NULL;
    temp = NULL;
    curr = NULL;
}
void thenodes::Addnode(int addData)
{
    Nodeptr n = new nodes;
    n->next = NULL;
    n->Numbers = addData;

    if (head != NULL)
    {
        curr = head;
        while (curr->next != NULL){
            curr = curr->next;
        }
        curr->next = n;
    }

    else
        {

    head = n;
        }
}
Listthenodes::Listthenodes() initializes head = NULL, but that doesn't mean it stays NULL forever. I think if you look further down in the Addnode() method, you'll see that head gets set.

By the way, I think you'll find that temp and curr don't need to be members of Listthenodes. They can probably be local variables in the methods that need them.
I gave an example of the algorithm here: http://www.cplusplus.com/forum/general/166404/#msg837802
Topic archived. No new replies allowed.