I'm getting really stressed out, creating a sorted linked list is very difficult and I'm trying the best I can, I'm just writing this code as I go and I don't even think I got anything right. Dealing with pointers would just put you into a lost path. Can someone give me an idea of what is wrong? I'm trying to sort the data, area code for example.
Here is what I have so far:
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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include <iostream>
using namespace std;
struct itemType
{
string name;
string title;
float price;
int area;
};
struct node
{
itemType data;
node *next;
};
enum compType{lesser, greater, equal};
compType compare(itemType x, itemType y);
class sortedList
{
private:
node *head;
public:
sortedList();
void add(itemType x);
itemType remove();
compType compare(itemType x, itemType y)
{
if(x.area > y.area) return greater;
else if(x.area < y.area) return lesser;
else return equal;
}
};
int main()
{
typedef itemType house;
sortedList myList;
house a, b, output;
a.name = "Evan";
a.title = "Programmer";
a.price = 122000;
a.area = 313;
b.name = "Mike";
b.title = "Writer";
b.price = 144500;
b.area = 248;
myList.add(a);
myList.add(b);
return 0;
}
sortedList::sortedList()
{
head = NULL;
}
void sortedList::add(itemType x)
{
node* newNode;
node* prevNode;
node* location;
bool moreToSearch;
location = data;
prevNode = NULL;
moreToSearch = (location != NULL);
while (moreToSearch)
{
switch(x.compare(location->data))
{
case greater:
prevNode = location;
location = location->next;
moreToSearch = (location != NULL);
break;
case lesser:
moreToSearch = false;
break;
}
}
newNode = new node;
newNode->data = x;
if (prevNode == NULL)
{
newNode->next = data;
data = newNode;
}
else
{
newNode->next = location;
prevNode->next = newNode;
}
length++;
}
|
Error 3 error C2039: 'compare' : is not a member of 'itemType' 77
Error 2 error C2065: 'data' : undeclared identifier 71
Error 4 error C2065: 'data' : undeclared identifier 95
Error 5 error C2065: 'data' : undeclared identifier 96
Error 6 error C2065: 'length' : undeclared identifier 103
Error 1 error C2872: 'equal' : ambiguous symbol 34
"equal" is ambiguous 34
class "itemType" has no member "compare" 77
identifier "data" is undefined 71
identifier "length" is undefined 103