Search code examples
c++doubly-linked-list

Printing Doubly-Linked-List in main() behaves different than printing it with an outside function


If I write the while loop below (uncomment to compile it) that prints the nodes in this doubly-linked-list, I see that "all_gods" list becomes empty (because of "all_gods = all_gods->next();")

all_gods:
 0x560c837b1f30 Odin
 0x560c837b1ef0 Ares
 0x560c837b1eb0 Zeus

0x560c837b1f30 Odin
0x560c837b1ef0 Ares
0x560c837b1eb0 Zeus
all_gods:

But if I move the same while loop in a function outside main() then all_gods remains intact.

all_gods:
 0x55aedf48ef30 Odin
 0x55aedf48eef0 Ares
 0x55aedf48eeb0 Zeus

0x55aedf48ef30 Odin
0x55aedf48eef0 Ares
0x55aedf48eeb0 Zeus
all_gods:
 0x55aedf48ef30 Odin
 0x55aedf48eef0 Ares
 0x55aedf48eeb0 Zeus

Why is this happening? Thanks!

#include <iostream>
#include <string>
using namespace std;

class Link
{
public:
    Link(const string n, Link *p = nullptr, Link *s = nullptr)
        : name{n}, prev{p}, succ{s}
    {
    }
    Link *insert(Link *n); // insert n before this object

    Link *next() const { return succ; }
    Link *previous() const { return prev; }
    ~Link()
    {
        delete succ;
    }

    string name;

private:
    Link *prev;
    Link *succ;
};
Link *Link::insert(Link *n) // insert n before this object; return n
{
    if (n == nullptr)
        return this;
    if (this == nullptr)
        return n;
    n->succ = this;     // this object comes after n
    if (prev)           // if prev of this (object) is not zero
        prev->succ = n; 
    n->prev = prev;     // this object’s predecessor becomes n’s predecessor
    prev = n;           // n becomes this object’s predecessor
    return n;           // returns n (the new element) which is before the top node
}
void print_all(Link *p)
{
    while (p)
    {
        cout << " " << p << " " << p->name;
        if (p = p->next()) // moved to the next node
            cout << "\n";
    }
}
void loop(Link *p)
{
    while (p)
    {
        cout << p << ' ' << p->name << '\n';
        p = p->next();
    }
}
int main()
{
    Link *all_gods = new Link{"Zeus"}; 
    all_gods = all_gods->insert(new Link{"Ares"});
    all_gods = all_gods->insert(new Link{"Odin"});

    cout << "all_gods:\n";
    print_all(all_gods);
    cout << "\n\n";

    // while (all_gods)
    // {
    //     cout << all_gods << ' ' << all_gods->name << '\n';
    //     all_gods = all_gods->next();
    // }
    loop(all_gods);

    cout << "all_gods:\n";
    print_all(all_gods);
    cout << "\n";

    delete all_gods;
}

Solution

  • Function arguments in C++ (other than references) are copies of what are passed.

    When you use the loop function to print the list, the value of all_gods is copied to the argument p. Then, the argument p is used to print the list without giving any effect to the variable all_gods.