Need help with a linked list problem

Hello, I'm having trouble with this class that is not to allow duplicates. It appends the items from one object into another. The method worked fine when I didn't check for duplicates, but since I added code to check for duplicates the program just hangs when it gets to the last number for a few minutes and then crashes. Any advice would be appreciated. I am just getting into using this type of problem so I'm not sure if I'm making a stupid mistake with the node. Here is the method:
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
template<class Type>
void set<Type>::append(const set<Type>& otherSet)
{
    nodeType<Type> * newNode;
    nodeType<Type> * current;

    if (otherSet.first == NULL)
    {
        cout << "The Set is empty.  Cannot copy set" << endl;
    }
    else
    {
        current = otherSet.first;
        count = count + otherSet.count;

        while (current != NULL)
        {
            int duplicate;

            duplicate = search(current->info); //returns 0 if not found

            if (duplicate == 0)
            {

                newNode = new nodeType<Type>;
                assert(newNode != NULL);

                newNode->info = current->info;
                newNode->link = current->link;

                if (first == NULL)
                {
                    first = newNode;
                    last = newNode;
                }
                else
                {
                    last->link = newNode;
                    last = newNode;
                }

                current = current->link;

            }
            else
            {
                cout << "duplicate detected, item not added" << endl;

                current = current->link;
            }
        }

    }
}
pase your code without checking for the duplicate, so we can compare it, and we can exatly see where the error happens!
Thanks for the reply. This is the code for the one that does accept duplicates, it works with no hang or no errors.

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
template<class Type>
void bag<Type>::append(const bag<Type>& otherbag)
{
    nodeType<Type> * newNode;
    nodeType<Type> * current;

    if (otherbag.first == NULL)
    {
        cout << "The bag is empty.  Cannot copy bag" << endl;
    }
    else
    {
        current = otherbag.first;
        count = count + otherbag.count;

        while (current != NULL)
        {
            newNode = new nodeType<Type>;
            assert(newNode != NULL);

            newNode->info = current->info;
            newNode->link = current->link;

            if (first == NULL)
            {
                first = newNode;
                last = newNode;
            }
            else
            {
                last->link = newNode;
                last = newNode;
            }

            current = current->link;
        }
    }
}
newNode->link = current->link;
newNode->link will point to a node inside otherSet. That can't be right. Shouldn't newNode->link be set to null or something instead?
Thanks Peter. I looked further into this and found that in fact neither of my methods were working correctly, it only appeared that way. If I would append the objects and then delete the 2nd object it would crash when printing the first b/c the result was only keeping a copy of that other object's item in the list b/c of the newNode->link = current->link... not really making it anew in the original object. I have adjusted this and it works now. Thanks again.
Last edited on
Topic archived. No new replies allowed.