Linked List Class

Hey guys! I was supposed to make a linked list class that linked boxes. I submitted it and my professor said this - "When you insert into a linked list you should be using the overloaded < operator to find where the new node belongs.
When you traverse the list you should print using the classes toString method. This is why every class needs the have a toString method and the overloaded operators for comparing." I'm not really sure what I'm supposed to do. I know how to fix the toString issue, but I don't know what she means about the overloaded < operator. Here's all of my code. Any and all help you can offer is greatly appreciated!


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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
  #include <iostream>
#include <sstream>
using namespace std;

class numberList
{
    private:
    struct listNode
    {
        double value;
        struct listNode *next;
    };
    
    listNode *head;
    
    public:
    numberList()
    {
        head = nullptr;
    }
    ~numberList(){};
    
    void appendNode(double num)
    {
        listNode *newNode;
        listNode *nodePtr;
        
        newNode= new listNode;
        newNode->value = num;
        newNode->next = nullptr;
        
        if (!head)
        head=newNode;
        else
        {
            nodePtr=head;
            
            while (nodePtr->next)
            nodePtr = nodePtr->next;
            
            nodePtr->next = newNode;
        }
    }
    
    void insertNode(double num)
    {
        listNode *newNode;
        listNode *nodePtr;
        listNode *previousNode = nullptr;
        
        newNode = new listNode;
        newNode->value = num;
        
        if (!head)
        {
            head = newNode;
            newNode->next = nullptr;
        }
        
        else
        {
            nodePtr=head;
            previousNode = nullptr;
            
            while(nodePtr != nullptr && nodePtr->value<num)
            {
                previousNode=nodePtr;
                nodePtr = nodePtr->next;
            }
            

            if(previousNode == nullptr)
            {
                head = newNode;
                newNode->next=nodePtr;
            }
            else
            {
                previousNode->next = newNode;
                newNode->next = nodePtr;
            }
        }
    }
    
    void deleteNode(double num)
    {
        listNode *nodePtr;
        listNode *previousNode;
        
        if (!head)
            return;
        
        if (head->value == num)
        {
            nodePtr = head;
            delete head;
            head = nodePtr;
        }
        
        else
        {
            nodePtr = head;
            while (nodePtr !=nullptr && nodePtr->value !=num)
            {
                previousNode=nodePtr;
                nodePtr = nodePtr->next;
            }
            
            if (nodePtr)
            {
                previousNode->next = nodePtr->next;
                delete nodePtr;
            }
        }
    }
    
    
    void displayList() const
    {
        listNode *nodePtr;
        nodePtr = head;
        
        while (nodePtr)
        {
            cout << nodePtr->value << endl;
            nodePtr=nodePtr->next;
        }
    }
};


class Box
{
    private:
    double width;
    double length;
    double height;
    
    public:
    Box()
    {
        width=1;
        length=1;
        height=1;
    }; //default constructor;
    
    Box(double hei, double wid, double len)
    {
        if (hei>0)
        height=hei;
        
        if(wid>0)
        width=wid;
        
        if(len>0)
        length=len;
        
    }; //constructor
    
    void setWidth(double wid)
    {
        if(wid>0)
        width=wid;
    }
    
    void setHeight(double hei)
    {
        if(hei>0)
        height=hei;
    }
    
    void setLength(double len)
    {
        if(len>0)
        length=len;
    }
    
    double getWidth() const
    {
        return width;
    }
    
    double getHeight() const
    {
        return height;
    }
    
    double getLenght() const
    {
        return length;
    }
    
    double volume() const
    {
        return length*width*height;
    }
    
    string toString() const
    {
        string String=static_cast<ostringstream*>(&(ostringstream() << length << " x " << width << " x " << height) ) ->str();
        return String;
    }
};

int main()
{
    double len;
    double wid;
    double hei;
    double volume1;
    
    double len2;
    double wid2;
    double hei2;
    double volume2;
    
    double len3;
    double wid3;
    double hei3;
    double volume3;
    
    Box box1;
    cout << "Please enter the length, width, and height for your first box: " << endl;
    cin >> len;
    cin >> wid;
    cin >> hei;
    box1.setWidth(wid);
    box1.setLength(len);
    box1.setHeight(hei);
    volume1=box1.volume();
    cout << "Your first box is " << box1.toString() << endl;
    cout << "The volume of your first box is " << box1.volume() << endl;
    
    Box box2;
    cout << "Please enter the length, width, and height for your second box: " << endl;
    cin >> len2;
    cin >> wid2;
    cin >> hei2;
    box2.setWidth(wid2);
    box2.setLength(len2);
    box2.setHeight(hei2);
    volume2=box2.volume();
    cout << "Your second box is " << box2.toString() << endl;
    cout << "The volume of your second box is " << box2.volume() << endl;
    
    Box box3;
    cout << "Please enter the length, width, and height for your third box: " << endl;
    cin >> len3;
    cin >> wid3;
    cin >> hei3;
    box3.setWidth(wid3);
    box3.setLength(len3);
    box3.setHeight(hei3);
    volume3=box3.volume();
    cout << "Your third box is " << box3.toString() << endl;
    cout << "The volume of your third box is " << box3.volume() << endl;
    
    numberList list;
    cout << "Adding the boxes to the list now." << endl;
    list.insertNode(volume1);
    list.insertNode(volume2);
    list.insertNode(volume3);
    cout << "Your boxes are: " << endl;
    list.displayList();
    cout << "Appending your second box." << endl;
    list.appendNode(volume2);
    cout << "The list is now: " << endl;
    list.displayList();
    cout << "Deleting your first box from the list now." << endl;
    list.deleteNode(volume1);
    cout << "The list is now: " << endl;
    list.displayList();
    
    return 0;
}


Thank you in advance!
Last edited on
"When you insert into a linked list you should be using the overloaded < operator to find where the new node belongs.

Perhaps you're supposed to overload operator< for the listNode class rather than using it on the value member?
Topic archived. No new replies allowed.