Link list nodes, inserting a node.

hello, i am trying to insert a node at the end of the list.
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
using namespace std;

class node
{
    private:
        int key;
        node * next;
    public:
    node(int);
    int getkey();
    node * getnext();
    void setnext(node  n);
    void print();

};




node * node::getnext()
{
    return (next);
}
void node::setnext(node n)
{
    next = &n;
}
void node::print()
{
    cout<<key<<endl;
}

node::node(int a)
{
    key =a;
}

int node::getkey()
{
    return (key);
}


class head
{
    private:
            node * head;

    public:
        bool find(int);
        void p();
        void sethead(node);
        void insert(node* nptr);


};
void head::insert(node* nptr)
{
node* tptr = head;
while (tptr->getnext() != NULL)
{
    tptr= tptr->getnext();
}

tptr->getnext() = nptr;          //causing error :: In member function `void head::insert(node*)':|
                                //|66|error: non-lvalue in assignment|
                                //|67|error: non-lvalue in assignment|
                                //||=== Build finished: 2 errors, 0 warnings ===|

nptr->getnext() = NULL;
}

void head::sethead(node p)
{
    head =&p;
}
bool head::find(int k)
{
       node * lct = head;
    while (lct != NULL)
    {
        if(lct->getkey() == 9)
        {
            cout <<"found"<<endl;
            break;

        }
        else cout <<"not found"<<endl;
        lct =lct->getnext();
    }


}

int main ()
{
    node a(9);
    node c(10);
    head b;
    node *h=&c;
    b.sethead(a);
    int o =10,l =90;
    int *i = &l;
    int *p = &o;


    b.insert(h);
    b.find(10);




}



i am not sure why i am getting an error, sorry for asking this but i am kinda lost. but assignment in line 66 and 67 are valid, i am assigning pointer to a pointer then, a pointer to NULL.
You can't assign to a value returner by a function. Function just creates a copy. It is useless to assign anything to it.
However, if the function returns a pointer, you can assign to the object it is pointing to. Here you need to assign a node*, so the function will have to return node**. Then (*tptr->getnext()) = 0;//I'm not sure if you need the parenthesis here will be fine.
References are a way to simplify the problem. It works like a pointer, but looks like a normal variable. If your function returned node*&, you could simply do tptr->getnext() = 0;.
Topic archived. No new replies allowed.