Thread: Linked List

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    20

    Linked List

    Ok guys I plan to take an advanced c++ class this summer and I got some assignments from a friend of mine to try before I take the class! I got this one assignment and I really need help on it. I will post my current code that I have and what I need to do and if someone could please help me I would greatly appreciate it!

    TASK:
    1. Create a car class to hold information
    Attributes:
    make (you can also think of this as the Manufacturer)
    model
    color
    year
    mileage
    (Note: All attributes should be private.)
    behaviors:
    1) Create mutator (Set) and accessor (Get) functions for all attributes of this class.
    2) Create a default constructor that initializes all of the attributes to default values (blanks in the case of strings or 0 in the case of numbers.)
    3) Make sure to have validation to ensure that the mileage can’t be set less than 0.
    4) Create a constructor that takes the make, model, year, color, and mileage values and sets them for a newly created car.
    5) Create another function called car_details that prints all of the attributes of the car in an attractive format.
    (Example on an output line of a cars details:
    “The current car is a 2008 Red Ford Mustang with 5000 miles.”)
    2. Create a structure called ListNode that can hold an object of the car class as its value.
    3. Create a Linked List to hold the following cars in inventory. Then use a loop to print all of the information about them for the user.
    Make Model Color Year Mileage
    1) Porsche 911 Silver 2005 45000
    2) Ford Mustang Red 2007 12600
    3) Voltzwagon Jetta Black 2006 20218
    4) Jeep Cherokee White 2000 98322
    5) Nisson Sentra red 2002 76046
    6) Voltzwagon Beetle Black 2005 28031
    4. Change the Jeep Cherokee’s year to be 2001. Change the Sentra’s mileage to be 80000. Use the loop to print out the information for the user.
    5. You need to be able to see the average miles for all cars in inventory. Write the code to find the average miles and display it for the user by adding up the miles for all of the cars in inventory and dividing by the size of the Linked List holding inventory. Make sure to test if the Linked List holding the inventory has nothing in it yet to prevent a divide by zero problem!
    6. Add the following cars to inventory. Then use a loop to print all of the information about them for the user.
    Make Model Color Year Mileage
    1) Chevrolet Corvette Black 2003 11903
    6) Ford Explorer Grey 2004 73922
    7) Honda Civic White 2007 12045

    7. Delete the following car out of inventory. Then use a loop to print all of the information about them for the user.
    Voltzwagon Jetta Black 2006 20218
    (Hint: This is not the last car! You will have to delete this node and set the previous node’s pointer to reference the next object in the list.)

    Header File
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    
    //Car Class
    class Car
    {
    protected:
        string make;  //make
        string model; // model
        string color;  // color
        int year;  // year
        int mileage;  // miles on car
    
    public:
                    //Constructor that will set information for a new car
            void New_vehicle (string a, string b, string c, int d, int e) 
            {make = a; model = b; color = c; year = d; mileage = e;}
            
            Car(); //Default constructor
            Car(string, string, string, int, int);
            //mutator and accessor functions
            void setMake(string);
        void setModel(string);
        void setColor(string);
        void setYear(int);
        void setMileage(int);
    
        string getMake();
        string getModel();
        string getColor();
        int getYear();
        int getMileage();
    
            //Check mileage to see if valid
        void valid_mileage(int);
    	void car_details();
        string string_car_details();
    };
    
    //Sets to default values
    Car::Car() {
        make = " ";
        model = " ";
        color = " ";
        year = 0;
        mileage = 0;
    }
            // My Vehicle set up(Make, model, color, year, mileage)
    Car::Car(string make, string model, string color, int year, int mileage) {
        this->make =  make;
        this->model = model;
        this->color = color;
        this->year = year;
        valid_mileage(mileage);
    }
    
    
    void Car::setMake(string make) {
        Car::make = make;
    }
    
    void Car::setModel(string model) {
        Car::model = model;
    }
    
    void Car::setColor(string color) {
        Car::color = color;
    }
    
    void Car::setYear(int year) {
        Car::year = year;
    }
    
    void Car::setMileage(int mileage) {
        valid_mileage(mileage);
    }
    
    
    string Car::getMake() {
        return make;
    }
    string Car::getModel() {
        return model;
    }
    string Car::getColor() {
        return color;
    }
    int Car::getYear() {
        return year;
    }
    int Car::getMileage() {
        return mileage;
    }
    
    
    void Car::valid_mileage(int mileage) {
        if (mileage>=0)
            Car::mileage=mileage;
        else {
            Car::mileage=0;
            cout << "WARNING! You have entered invalid mileage!\n";
            }
        }
    
            void Car::car_details() {
                cout << "The current car is a " << year << ' ' << color << ' '
                            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            }
    
    
    
            string Car::string_car_details() {
                stringstream buf;
                buf << "The current car is a " << year << ' ' << color << ' '
                << make << ' ' << model << " with " << mileage << " miles.\n\n";
                return buf.str();
            }
    
    
    		struct ListNode
          {
          Car Car;
          ListNode *next;
          };   
    
          struct CarList
          {
    
          ListNode * root;
          CarList() : root(0) {};
          void insert(Car);
          void del(Car);
          void findCar(Car);
          void printList();
    	  void car_details();
    
    	 
          };
    
    	  //void CarList::car_details();
          //{
          //ListNode * current = firstNodeInList;
         // while(current != NULL)
          //cout << current->Car;
    	  //}
    CPP
    Code:
    #include "CarClass.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
        CarList inventory;
         Car carObject;
         bool addAnotherCar = true;
    
    	  	ListNode *start_ptr = NULL;
    		for (int i = 0; i<6; i++) {
    				cout << "Please enter the make of the vehicle: ";
    				&Car::getMake;
                    cout << "Please enter the model of the vehicle: ";
                    &Car::getModel;
                    cout << "Please enter the color of the vehicle: ";
                    &Car::getColor;
                    cout << "Please enter the year of the vehicle: ";
                    &Car::getYear;
                    cout << "Please enter the mileage of the vehicle: ";
                    &Car::getMileage;
    	 
    			inventory.insert(carObject);
    		
    		//Information needs to be changed
    	 	
    	 	inventory.car_details();
    
    		//Add more cars
    
    		//Information about a car to remove
    
    		//delete desidred car
    
    		inventory.del(carObject);
    
    		//Print information
    		inventory.car_details();
    		}
    	    return 0;
    	}
    And guys if I am doing this in the entirely wrong way and their is a much easier way then please tell me so that I will know! I want to be ahead of the game before this class starts this summer!

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    Now I know you don't need all of those headers. You shouldn't include headers that you don't need.

    Code:
    //Car Class
    class Car
    {
    protected:
        string make;  //make
        string model; // model
        string color;  // color
        int year;  // year
        int mileage;  // miles on car
    It's good to comment your code, but these kinds of comments are worthless. If what you wrote in the comment is obvious from the code, it shouldn't be in there.

    Code:
    public:
                    //Constructor that will set information for a new car
            void New_vehicle (string a, string b, string c, int d, int e) 
            {make = a; model = b; color = c; year = d; mileage = e;}
    You need to use better names than a b c d... On the same note,
    Code:
    Car(string, string, string, int, int);
    You should include the names of the variables in the function prototype, this way your class declaration file can also serve as documentation on how to use the class.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    A few small things quickly, because it's after 2am here, and I don't want to say anything stupid.

    • The class attributes should be private, not protected (as stated in the specs).
    • You accessor methods can be const, since they don't modify values.
    • You can split the class into definition (.h) and implementation (.cpp) files for readability, etc.
    • You can get rid of the New_vehicle() function, since you have a constructor with the same functionality.
    • Don't do this: Car(string, string, string, int, int). Put in variable names, like Car(string make, string model, string color, int year, int mileage). It's not technically incorrect, but it makes it hard to read and follow.
    • Use descriptive variable names.
    • Your class and object can't have the same name, like Car Car.


    It might also be a good idea to separate this project into two parts until you get the hang of each. Implement the Car class and forget about linked lists. Make sure you can create one Car, set the attributes, modify them, print them, etc. Once you have that working, save it, close it out, and start a new project. Implement a simple linked list, but of ints or strings, not cars. Nothing fancy. Once you can append, insert, and delete nodes, THEN combine the two. I always find that splitting a large project into smaller projects can make things a bit clearer.
    Last edited by Mostly Harmless; 04-15-2010 at 12:32 AM.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    20
    Thanks for the advice guys I will make some of these changes to clean my code...but can someone still help me with my linked list lol

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Sure but the only things you need to do with your linked list in this assignment are things that any linked list needs to do. Understand how the data structure works, then figure out which functions you need to write to manage it (list traversal , delete a node, add a node), then everything falls into place.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    20
    Here is my code
    Code:
    #include "CarClass.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
        CarList inventory;
         Car Car;
         bool addAnotherCar = true;
    
    	  	ListNode *start_ptr = NULL;
    		for (int i = 0; i<2; i++) {
    				cout << "Please enter the make of the vehicle: ";
    				&Car::getMake;
                    cout << "Please enter the model of the vehicle: ";
                    &Car::getModel;
                    cout << "Please enter the color of the vehicle: ";
                    &Car::getColor;
                    cout << "Please enter the year of the vehicle: ";
                    &Car::getYear;
                    cout << "Please enter the mileage of the vehicle: ";
                    &Car::getMileage;
    	 
    			inventory.insert(Car);
    		
    		//Information needs to be changed
    	 	
    	 	inventory.car_details();
    
    		//Add more cars
    
    		//Information about a car to remove
    
    		//delete desidred car
    
    		inventory.del(Car);
    
    		//Print information
    		inventory.car_details();
    		}
    	    return 0;
    	}
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    
    //Car Class
    class Car
    {
    protected:
        string make;  //make
        string model; // model
        string color;  // color
        int year;  // year
        int mileage;  // miles on car
    
    public:
                    //Constructor that will set information for a new car
            void New_vehicle (string a, string b, string c, int d, int e) 
            {make = a; model = b; color = c; year = d; mileage = e;}
            
            Car(); //Default constructor
            Car(string, string, string, int, int);
            //mutator and accessor functions
            void setMake(string);
        void setModel(string);
        void setColor(string);
        void setYear(int);
        void setMileage(int);
    
        string getMake();
        string getModel();
        string getColor();
        int getYear();
        int getMileage();
    
            //Check mileage to see if valid
        void valid_mileage(int);
    	void car_details();
        string string_car_details();
    };
    
    //Sets to default values
    Car::Car() {
        make = " ";
        model = " ";
        color = " ";
        year = 0;
        mileage = 0;
    }
            // My Vehicle set up(Make, model, color, year, mileage)
    Car::Car(string make, string model, string color, int year, int mileage) {
        this->make =  make;
        this->model = model;
        this->color = color;
        this->year = year;
        valid_mileage(mileage);
    }
    
    
    void Car::setMake(string make) {
        Car::make = make;
    }
    
    void Car::setModel(string model) {
        Car::model = model;
    }
    
    void Car::setColor(string color) {
        Car::color = color;
    }
    
    void Car::setYear(int year) {
        Car::year = year;
    }
    
    void Car::setMileage(int mileage) {
        valid_mileage(mileage);
    }
    
    
    string Car::getMake() {
        return make;
    }
    string Car::getModel() {
        return model;
    }
    string Car::getColor() {
        return color;
    }
    int Car::getYear() {
        return year;
    }
    int Car::getMileage() {
        return mileage;
    }
    
    
    void Car::valid_mileage(int mileage) {
        if (mileage>=0)
            Car::mileage=mileage;
        else {
            Car::mileage=0;
            cout << "WARNING! You have entered invalid mileage!\n";
            }
        }
    
            void Car::car_details() {
                cout << "The current car is a " << year << ' ' << color << ' '
                            << make << ' ' << model << " with " << mileage << " miles.\n\n";
            }
    
    
    
            string Car::string_car_details() {
                stringstream buf;
                buf << "The current car is a " << year << ' ' << color << ' '
                << make << ' ' << model << " with " << mileage << " miles.\n\n";
                return buf.str();
            }
    
    
    		struct ListNode
          {
          Car Car;
          ListNode *next;
          };   
    
          struct CarList
          {
    
          ListNode * root;
          CarList() : root(0) {};
          void insert(Car);
          void del(Car);
          void findCar(Car);
          void printList();
    	  void car_details();
    
    	 
          };
    
    	  //void CarList::car_details();
          //{
          //ListNode * current = firstNodeInList;
         // while(current != NULL)
          //cout << current->Car;
    	  //}
    I am getting the following errors when trying to run it!

    1>Car.obj : error LNK2019: unresolved external symbol "public: void __thiscall CarList::del(class Car)" (?del@CarList@@QAEXVCar@@@Z) referenced in function _main
    1>Car.obj : error LNK2019: unresolved external symbol "public: void __thiscall CarList::car_details(void)" (?car_details@CarList@@QAEXXZ) referenced in function _main
    1>Car.obj : error LNK2019: unresolved external symbol "public: void __thiscall CarList::insert(class Car)" (?insert@CarList@@QAEXVCar@@@Z) referenced in function _main

    And I have played with this thing for over a week and I can not get it to do the following:

    Change the Jeep Cherokee’s year to be 2001. Change the Sentra’s mileage to be 80000. Use the loop to print out the information for the user.

    Add Chevrolet Corvette Black 2003 11903
    6) Ford Explorer Grey 2004 73922
    7) Honda Civic White 2007 12045
    to the list

    And delete a car from the list

    Can someone please assist me??

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's not going to compile until you at least pretend to write all the functions. If you just want to run a check of what you have now, you can define those functions to do nothing, like:
    Code:
    void CarList::del(class Car car_to_be_deleted) {
        //doesn't do anything yet!
    }
    And then later you can write the code that actually removes a Car from the List.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    20
    The functions are blank because everything i tried blew up big time! So to save time i just took out my messed up code

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you had commented out that code, you wouldn't be getting those errors. If you want to leave the function calls in main(), then you have to have those functions defined (at least in a very minimal way) somewhere. If you don't even want to do that, then you cannot call the function and expect the thing to compile.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    43
    I think you're overcomplicating it by trying to implement two things at the same time without having a firm grasp on either. Write a Car class that can only handle one car at a time. Then implement a linked list. Something simple, like:
    Code:
    struct node
    {
        int data;
        node* next;
    };
    
    //etc.
    Once you can correctly program the two pieces separately, put them together.

  11. #11
    Registered User
    Join Date
    Apr 2010
    Posts
    20
    Code:
    #include "CarClass.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
        CarList inventory;
         Car carObject;
         bool addAnotherCar = true;
    
    	  	ListNode *start_ptr = NULL;
    		for (int i = 0; i<2; i++) {
    				cout << "Please enter the make of the vehicle: ";
    				&Car::getMake;
                    cout << "Please enter the model of the vehicle: ";
                    &Car::getModel;
                    cout << "Please enter the color of the vehicle: ";
                    &Car::getColor;
                    cout << "Please enter the year of the vehicle: ";
                    &Car::getYear;
                    cout << "Please enter the mileage of the vehicle: ";
                    &Car::getMileage;
    	 
    			inventory.insert(Car);
    		
    		//Information needs to be changed
    	 	
    	 	inventory.car_details();
    
    		//Add more cars
    
    		//delete desidred car
    
    		void CarList::del(Car "Voltzwagon")	{
    		}
    
    		//Print information
    		inventory.car_details();
    		}
    	    return 0;
    	}

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM