Why this list print only one element?

Hi Community,

i make a disjointset and when i make union, i push back in an list for after print all elements, qhy no print all element, but only last insert?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class TreeNode
{
private:
    int key;
    string value;
    int rango;
    TreeNode* parent;
    list<TreeNode*>* childs;
public:
    TreeNode(int key, string value)
    {
        this->key=key;
        this->value=value;
        this->rango=0;
        this->parent=this;
        this->childs= new list<TreeNode*>;
    }

    list <TreeNode*>* getChilds() { return childs; }
}



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
  bool disjointset::link(TreeNode* x, TreeNode* y)
{
    if ( x == y )
        return false;
    if ( x->getRango() > y->getRango() )
    {
        y->setParent(x);
        x->getChilds()->push_back(y);

        list<TreeNode*>* l = x->getChilds();
        for(auto it = l->begin(); it != l->end(); ++it)
          cout << "Disjoint with: " << (*it)->getKey() << " : "<< (*it)->getValue();
    }
    else
    {
        x->setParent(y);
        y->getChilds()->push_back(x);

        list<TreeNode*>* l = y->getChilds();
        for(auto it = l->begin(); it != l->end(); ++it)
          cout << "Disjoint with: " << (*it)->getKey() << " : "<<(*it)->getValue();
    }
    if ( x->getRango() == y->getRango() )
        y->setRango(1);
    return true;
}


if i make union between: 10 and 100 after union 100 -> 10,
now make union from 12 and 100 after union 100 -> 10 -> 12,
but in first step print only 10 and ok, in second step print only 12 and not 10 and 12.

can help me pls?
Last edited on
Can you show us a working program that demonstrates your problem? Please provide the input, the output that you get, and the output that you expect.
this is the output after i make union with elements: (10,100) (12,100)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
key: 21  position: 23
value: sempre

key: 35  position: 35
value: eqwewr

key: 34  position: 38
value: ciao

key: 50  position: 52
value: simpaticone

key: 75  position: 80
value: scopi

key: 100  position: 103
value: mur
disjoint with: 12 : qwecc


the output that i expect is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
key: 21  position: 23
value: sempre

key: 35  position: 35
value: eqwewr

key: 34  position: 38
value: ciao

key: 50  position: 52
value: simpaticone

key: 75  position: 80
value: scopi

key: 100  position: 103
value: mur
disjoint with: 10 : reset
disjoint with: 12 : qwecc


is this ok or do you need anything else?
up pls
Please show your full, compileable program. Preferably as a single file, so that we can run it in cpp.sh and not have to download lots of individual bits.
the complete program has several lines, can i upload files somewhere?
Reduce your program to the minimum necessary to show the problem. i.e. remove anything unrelated to it.

If something designed to produce your sample output is too large to upload here then your program is too big.
my problem is small, i can't print all the elements of the list, despite using an iterator to scroll the list.
I solved, we were missing endl; at the end of the cout
Last edited on
The code you posted won't even compile. Since you're pretty new here, I put in the work to get it there:
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
#include <iostream>
#include <string>
#include <list>

using std::string;
using std::cin;
using std::cout;
using std::list;

class TreeNode
{
private:
    int key;
    string value;
    int rango;
    TreeNode* parent;
    list<TreeNode*>* childs;
public:
    TreeNode(int key, string value)
    {
        this->key=key;
        this->value=value;
        this->rango=0;
        this->parent=this;
        this->childs= new list<TreeNode*>;
    }

    list <TreeNode*>* getChilds() { return childs; }
    int getRango() { return rango; }
    void setRango(int r) { rango = r; }
    void setParent(TreeNode *p) { parent = p; }
    int getKey() const { return key;} 
    const string &getValue() const { return value; };
    
};

bool disjointset(TreeNode* x, TreeNode* y)
{
    if ( x == y )
        return false;
    if ( x->getRango() > y->getRango() )
    {
        y->setParent(x);
        x->getChilds()->push_back(y);

        list<TreeNode*>* l = x->getChilds();
        for(auto it = l->begin(); it != l->end(); ++it)
          cout << "Disjoint with: " << (*it)->getKey() << " : "<< (*it)->getValue();
    }
    else
    {
        x->setParent(y);
        y->getChilds()->push_back(x);

        list<TreeNode*>* l = y->getChilds();
        for(auto it = l->begin(); it != l->end(); ++it)
          cout << "Disjoint with: " << (*it)->getKey() << " : "<<(*it)->getValue();
    }
    if ( x->getRango() == y->getRango() )
        y->setRango(1);
    return true;
}

int main()
{
    int key;
    string value;

    cin >> key >> value;
    TreeNode n1(key, value);
    cin >> key >> value;
    TreeNode n2(key, value);
    disjointset(&n1, &n2);
}


this is the output after i make union with elements: (10,100) (12,100)


I assume "elements" means TreeNode's. I assumed "10,100" and "12,100" means those are the parameters to the constructor. You can see all this in the main program above. When I run that, I get this output:
Disjoint with: 10 : 100


That isn't even in the same format as the output you posted.

I hope this helps you see things from our perspective. We don't know what the code is supposed to do. We can't compile it as you've presented it and even after attempting to fix it, it doesn't generate the output that you posted. So it's very difficult to help with your problem.
I solved, we were missing endl; at the end of the cout
Topic archived. No new replies allowed.