Why doesnt "listt.addHead()->link->info" this section of code print out the "info" value of the Body Node of which is :2?

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// First_VB_Prog.cpp : Defines the entry point for the console application.
//

#include <iostream>

using namespace std;

//Simple Linked List composing of a "Body" , "Head" and a "Tail"

struct nodeType
{

    int info;
    nodeType *link;
};

class linkedList
{

public:
    linkedList()
    {
        Head = NULL;
    }

    nodeType* addHead()
    {

        Head = new nodeType;
        return Head;
    }
private:

    nodeType *Head;
};

int main()
{

//declaring Nodes:

    nodeType *Body;
    Body = new nodeType;

    nodeType *upperBody;
    upperBody = new nodeType;

    nodeType *Tail;
    Tail = new nodeType;

//Linking Nodes

    linkedList listt;

    Body->info = 2; //accessing info on Body Node
    listt.addHead()->link = Body;	//linking OR joing the head to the body Node

    Body->link = upperBody; //linking OR joing the body to the  TailNode
    lowerBody->info = 89;

    lowerBody->link = Tail; //linking OR joing the body to the  lowerBody
    Tail->link= NULL;  //accessing info on Tail Node

    cout<<listt.addHead()->link->info;
}
why doesnt "listt.addHead()->link->info" this section of code print out the "info" value of the Body Node of which is :2?


Because you're not printing out the "info" value of the Body node. You're printing out the "info" value of the newly allocated node returned by addHead, which, incidentally, leaks memory and makes no attempt to preserve any existing list structure.

addHead is not a method a linked list should have.
Last edited on
Thanks, after Removing the function the code works, but i heard its bad practise to declare the "Head as public" how true is this

/**modified code is:**/
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
49
50
51
52
// First_VB_Prog.cpp : Defines the entry point for the console application.
//

#include <iostream>

using namespace std;

//Simple Linked List composing of a "Body" , "Head" and a "Tail"

struct nodeType
{

    int info;
    nodeType *link;

};

int main()
{

//declaring Nodes:
    nodeType *Head;
    Head = new nodeType;
    Head->info = 0;


    nodeType *Body;
    Body = new nodeType;
    Head ->link = Body;


    nodeType *upperBody;
    upperBody = new nodeType;

    nodeType *Tail;
    Tail = new nodeType;

//Linking Nodes

    //linkedList listt;

    Body->info = 2; //accessing info on Body Node
    //listt.addHead()->link = Body;	//linking OR joing the head to the body Node

    Body->link = upperBody; //linking OR joing the body to the  TailNode
    upperBody->info = 89;

    upperBody->link = Tail; //linking OR joing the body to the  TailNode
    Tail->info = NULL;  //accessing info on Tail Node

    cout<<Head->link->info;
}
Topic archived. No new replies allowed.