linked list basic problem mistake-i am pulling out my hair

someone please tell me, why is this not working. why is the L1 not being updated by addbegin() function. literally have been stuck in 2 programs for last 4 hours due to this reason.

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
  // Example program
#include <iostream>
using namespace std;

typedef
struct node
{
    int coef;
    int power;
    struct node* next;
}* Lptr;

void addbegin(node * L,int c, int p)
{
    Lptr T=new struct node;
    T->coef=c;
    T->power=p;
    T->next=L;
    L=T;
}

void printL(Lptr L)
{
    Lptr T;
    T=L;
    while(T!=NULL)
    {
        cout<<T->coef<<" "<<T->power<<endl;
        T=T->next;
    }
}


int main()
{
    Lptr L1;
    L1=NULL;
    addbegin(L1,5,27);addbegin(L1,3,12);addbegin(L1,7,2);addbegin(L1,15,0);
    printL(L1);
    cout<<"done";
}
Last edited on
addBegin takes a copy of a pointer. Any changes made to that pointer are lost when the function returns.

Either return the new list head by value (and do something appropriate with the return value) or take the old list head by reference.

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
// Example program
#include <iostream>
using namespace std;

typedef
struct node
{
    int coef;
    int power;
    struct node* next;
};

void addbegin(node*& L, int c, int p)
{
    L = new node{ c, p, L };
}

void printL(node* L)
{
    while (L)
    {
        cout << L->coef << " " << L->power << endl;
        L = L->next;
    }
}

void destroy(node* l) 
{
    while (l) {
        node* t = l;
        l = l->next;
        delete t;
    }
}

int main()
{
    node* L1 = nullptr;
    addbegin(L1, 5, 27); 
    addbegin(L1, 3, 12); 
    addbegin(L1, 7, 2); 
    addbegin(L1, 15, 0);

    printL(L1);
    destroy(L1);
    cout << "done";
}

actually i also thought the same. but if so, why does this work then?

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
#include <iostream>
using namespace std;

typedef
struct node
{
    int data;
    struct node* next;
}* Lptr;

void addend(node * L,int d)
{
    node* T;
    T=L;
    while(T->next!=NULL)
    T=T->next;
    T->next=new (struct node);
    T=T->next;
    T->data=d;
    T->next=NULL;
}

void printL(Lptr L)
{
    Lptr T;
    T=L;
    while(T!=NULL)
    {
        cout<<T->data<<endl;
        T=T->next;
    }
}

int main()
{
    Lptr L=new node;
    L->data=25;
    L->next=NULL;
    addend(L,33);
    addend(L,25);
    addend(L,27);
    addend(L,33);
    addend(L,33);
    printL(L);
}
actually i also thought the same. but if so, why does this work then?

In addend you are not modifying L and expecting that to be reflected in main.
@cire sorry. i didn't get that. In "addend()", according to your logic, I am taking a copy of 'L' and and by the help of 'T' pointer making all the modification of adding new nodes at end to that copy of L. but then, why is the same being reflected in main() while using "printL(L)" where argument 'L' is the original 'L'.
i didn't get that. In "addend()", according to your logic, I am taking a copy of 'L'

Yes. What you are not doing is modifying that copy and expecting the original to be affected. In other words, you don't assign to L as you did in addstart.


and by the help of 'T' pointer making all the modification of adding new nodes at end to that copy of L.
T is not needed.

// Equivalent to what you've written:
1
2
3
4
5
6
7
8
9
void addend(node * L,int d)
{
    while ( L->next ) L = L-> next;

    L->next = new node;
    L = L->next;
    L->data = d;
    L->next = nullptr;   
}


And note that the original in main is not somehow magically pointing to the last node in the list. It still points to the first.


while using "printL(L)" where argument 'L' is the original 'L'.
printL accepts a pointer by value, therefore it operates on a copy of the original, and you'll notice that my version of printL takes advantage of that.

[edit: formatting]
Last edited on
Topic archived. No new replies allowed.