Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hey guys! I was supposed to make a linked list class that linked boxes. I wrote

ID: 3773204 • Letter: H

Question

 Hey guys! I was supposed to make a linked list class that linked boxes. I wrote the program and it compiles and works correctly. However, my professor doesn't like the way I did a few things. When I submitted it to my professor, she 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 don't know what she means about the overloaded < operator. I understand how to write a toString function, but not how to use it within the class or how to write it in this particular case. The functions she has problems with are in bold in the my code. Any and all help you can offer is greatly appreciated! Here's all of my code:    #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) //she says for this part I should be using the overloaded < operator?     {         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 //she said for this part I needed to use a toString method?     {         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; } 

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

class numberList{
    private:
        struct listNode{
            double value;
            struct listNode *next;
            string toString() const{
                string String = static_cast<ostringstream*>(&(ostringstream() << value )) ->str();
                return String;
            }
            bool operator <(const listNode &b) const{
                if (b.value > value) return true;
                return false;         
            };

        };
        listNode *head;
    public:
        numberList(){
            head = NULL;
        }
        ~numberList(){};
  
        void appendNode(double num){
            listNode *newNode;
            listNode *nodePtr;

            newNode= new listNode;
            newNode->value = num;
            newNode->next = NULL;

            if (!head)
            head=newNode;
            else{
                nodePtr=head;

                while (nodePtr->next)
                nodePtr = nodePtr->next;

                nodePtr->next = newNode;
            }
        }
  
        void insertNode(double num){ //she says for this part I should be using the overloaded < operator
            listNode *newNode;
            listNode *nodePtr;
            listNode *previousNode = NULL;
          
            newNode = new listNode;
            newNode->value = num;
            if (!head){
                head = newNode;
                newNode->next = NULL;
            }
          
            else{
                nodePtr=head;
                previousNode = NULL;
              
                while(nodePtr != NULL && newNode < nodePtr){
                    previousNode=nodePtr;
                    nodePtr = nodePtr->next;
                }
              

                if(previousNode == NULL){
                    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 !=NULL && nodePtr->value !=num){
                    previousNode=nodePtr;
                    nodePtr = nodePtr->next;
                }
              
                if (nodePtr){
                    previousNode->next = nodePtr->next;
                    delete nodePtr;
                }
            }
        }
  
  
        void displayList() const{ //she said for this part I needed to use a toString method?
            listNode *nodePtr;
            nodePtr = head;
            while (nodePtr){
                cout << nodePtr->toString() << 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;
}