Linked list help

I have this program that sorts an added list but something is wrong in the add function, I've been using breakpoints, something is wrong with the whole function. Somewhere, I have the pointers incorrectly used, can someone help me out here?

P.S.- I know templates are being used, I just learned to use them today, so I hope they are okay as well. The main problem is that the program is not working properly.

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

struct houses
{
    string title;
    int price;
};

struct node
{
    houses data;
    node *next;
};

template <class T>
class sortedList
{
private:
    node *head;
public:
    sortedList();
    void add(houses& x);
};

template <class T>
sortedList<T>::sortedList()
{
    head = NULL;
}

enum compareType
{
    lesser,
    greater,
    similar
};

compareType compare(houses& x, houses& y)
{
 if (x.price > y.price)
    return greater;
 else if (x.price < y.price)
    return lesser;
 else
    return similar;
}

int main()
{
    sortedList<houses> myList;
    houses a, b, c, d;

    a.title = "House A";
    a.price = 191;
    b.title = "House B";
    b.price = 152;
    c.title = "House C";
    c.price = 135;
    d.title = "House D";
    d.price = 164;

    myList.add(a);
    myList.add(b);
    myList.add(c);
    myList.add(d);

    system("pause");
    return 0;
}

template <class T>
void sortedList<T>::add(houses& x)
{
    node *temp;
    temp = head;
	temp = new node;
   
    while(temp != NULL && greater == compare(x, temp->data))
    {
        temp = temp->next;
    }

    temp->data.title = x.title;
    temp->data.price = x.price;

    temp->next = new node;
}
Could you be more specific as to what is wrong? And on line 77 and 87, it seems you are newing nodes without ever editing them, basically adding extra "empty" nodes to your tree. I would just remove line 77, as it seems to just create an error anyway; since your while loop operates on temp, and you have just created a new node for it to point to, anytime you add to the tree you are working on a new tree, so to speak, and end up with memory leaks all over the place.

Does your textbook go over how to create a linked list? Start with that and compare the explanations/code with your own.
In addition to the memory leak Zhuge points out, it looks like you are trying to add nodes to the list so they are in order. Make sure you change both the new node's next pointer, and the temp's pointer, like the code below...of course you'll have to test it out, but I think it will work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class T>
void sortedList<T>::add(houses& x)
{
    node *temp;
    temp = head;
   
    while(temp != NULL && greater == compare(x, temp->data))
    {
        temp = temp->next;
    }
  node *someNode = new node;       //create the new node
   someNode->data.title = x.title;
    someNode->data.price = x.price;
someNode->next = temp->next;     //set the new node's next pointer to the next in the list
temp->next = someNode;              //set the temp's next pointer to the new node
}
and more

1. in add()
you should use template into the paremeter of add()

template <class T>
void sortedList<T>::add(T & x)

2. in add()
node *temp;
temp = head; //here is no memmory when the first call from add(a);
Topic archived. No new replies allowed.