1. Welcome to Tacoma World!

    You are currently viewing as a guest! To get full-access, you need to register for a FREE account.

    As a registered member, you’ll be able to:
    • Participate in all Tacoma discussion topics
    • Communicate privately with other Tacoma owners from around the world
    • Post your own photos in our Members Gallery
    • Access all special features of the site

Linked List C++

Discussion in 'Technology' started by tarheelfan_08, Apr 11, 2010.

  1. Apr 11, 2010 at 9:54 AM
    #1
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Hey guys I am trying to work on some linked list stuff and I have my code put together but I wanted to post it on here and just check to make sure I am going in the right direction. Here is what I am trying to do:

    Create a structure called ListNode that can hold an object of the car class as its value.
    Then create a linked list to hold the following cars in inventory(6 cars that are in the array). Then I have to print the information for the user.

    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();
            }
    
    CPP
    Code:
    #include "CarClass.h"
    using namespace std;
    
    struct ListNode
        {
            Car Car_array[6];
            ListNode *Next;
        };    
    
    int main() {
            
            //Array of 6 cars
            ListNode = { Car("Porsche", "911", "Silver", 2005, 45000), 
                                    Car("Ford", "Mustang", "Red", 2007, 12600),
                                    Car("Voltzwagon", "Jetta", "Black", 2006, 20218),
                                    Car("Jeep", "Cherokee", "White", 2000, 98322),
                                    Car("Nissan", "Sentra", "Red", 2002, 76046),
                                    Car("Voltzwagon", "Beetle", "Black", 2005, 28031)};
    
     
  2. Apr 11, 2010 at 7:27 PM
    #2
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Here is what I have

    Code:
    #include "CarClass.h"
    using namespace std;
    
    struct ListNode
        {
            string make;  //make
            string model; // model
            string color;  // color
            int year;  // year
            int mileage;  // miles on car
            ListNode *next;   //Pointer to next node
    
            ListNode(void)
                    :next(NULL)
            {}
    };      
    
    ListNode *start_ptr = NULL;
    int main()
    {
            start_ptr = new ListNode;
            ListNode *temp = start_ptr;
            cout << "Please enter the make of the vehicle: ";
            cin >> temp->make;
    
            start_ptr = new ListNode;
            ListNode *temp = start_ptr;
            cout << "Please enter the model of the vehicle: ";
            cin >> temp->model;
    
            start_ptr = new ListNode;
            ListNode *temp = start_ptr;
            cout << "Please enter the color of the vehicle: ";
            cin >> temp->color;
    
            start_ptr = new ListNode;
            ListNode *temp = start_ptr;
            cout << "Please enter the year of the vehicle: ";
            cin >> temp->year;
    
            start_ptr = new ListNode;
            ListNode *temp = start_ptr;
            cout << "Please enter the mileage of the vehicle: ";
            cin >> temp->mileage;
    
    
            return 0;
        }
    
    
    I now have 2 questions. I have to enter the following 6 vehicles,

    Code:
    Car("Porsche", "911", "Silver", 2005, 45000), 
                                    Car("Ford", "Mustang", "Red", 2007, 12600),
                                    Car("Voltzwagon", "Jetta", "Black", 2006, 20218),
                                    Car("Jeep", "Cherokee", "White", 2000, 98322),
                                    Car("Nissan", "Sentra", "Red", 2002, 76046),
                                    Car("Voltzwagon", "Beetle", "Black", 2005, 28031)};
    
    
    How does this code that I currently have know that I have 6 to enter and how can i print them all when I am done using the cardetails function set up in the header file??
     
  3. Apr 11, 2010 at 8:20 PM
    #3
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Got my code working....but i have one problem! I need the information in the Linked list to see how many cars were keyed in and then print out using the car_details function in the header file. Can someone show me or explain to me how to do this??

    Code:
    #include "CarClass.h"
    using namespace std;
    
    struct ListNode
        {
            string make;  //make
            string model; // model
            string color;  // color
            int year;  // year
            int mileage;  // miles on car
            ListNode *next;   //Pointer to next node
    
            ListNode(void)
                    :next(NULL)
            {}
    };      
    
    ListNode *start_ptr = NULL;
    int main()
    {
            start_ptr = new ListNode;
            ListNode *temp = start_ptr;
    
            char again;
    
            while(true)
            {
                    cout << "Please enter the make of the vehicle: ";
                    cin >> temp->make;
                    cout << "Please enter the model of the vehicle: ";
                    cin >> temp->model;
                    cout << "Please enter the color of the vehicle: ";
                    cin >> temp->color;
                    cout << "Please enter the year of the vehicle: ";
                    cin >> temp->year;
                    cout << "Please enter the mileage of the vehicle: ";
                    cin >> temp->mileage;
                    
                    cout<<"Would You like To Enter Another Car (Y/N) :";
                    cin>>again;
    
                    if(toupper(again) != 'Y')
                            break;
                    temp->next = new ListNode;
                    temp = temp->next;
            }
    
            for (int x=0; x<6; x++) 
            {
            Car[x].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();
            }
    
     
  4. Apr 11, 2010 at 8:40 PM
    #4
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    I got it!!! I just need to figure out now how to set a variable to recognize how many vehicles were intered in

    Code:
    #include "CarClass.h"
    using namespace std;
    
    
    
    ListNode *start_ptr = NULL;
    int main()
    {
            start_ptr = new ListNode;
            ListNode *temp = start_ptr;
    
            char again;
    
            while(true)
            {
                    cout << "Please enter the make of the vehicle: ";
                    cin >> temp->make;
                    cout << "Please enter the model of the vehicle: ";
                    cin >> temp->model;
                    cout << "Please enter the color of the vehicle: ";
                    cin >> temp->color;
                    cout << "Please enter the year of the vehicle: ";
                    cin >> temp->year;
                    cout << "Please enter the mileage of the vehicle: ";
                    cin >> temp->mileage;
                    
                    cout<<"Would You like To Enter Another Car (Y/N) :";
                    cin>>again;
    
                    if(toupper(again) != 'Y')
                            break;
                    temp->next = new ListNode;
                    temp = temp->next;
            }
    
            for (int x=0; x<6; x++) 
            {
            start_ptr[x].car_details();
            }
    
            return 0;
        }
    
    Code:
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    
    struct ListNode
        {
            string make;  //make
            string model; // model
            string color;  // color
            int year;  // year
            int mileage;  // miles on car
            ListNode *next;   //Pointer to next node
            
            void car_details();
            ListNode(void)
                    :next(NULL)
            {}
    };      
    //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 ListNode::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();
            }
    
     
  5. Apr 11, 2010 at 9:23 PM
    #5
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Ok I got it but I have one final question I am curious about! If I wanted to add 3 more cars how would I do this! And if I wanted to delete a specific one for instance the second car...how would i do this??

    Code:
    #include "CarClass.h"
    using namespace std;
    
    
    
    ListNode *start_ptr = NULL;
    int main()
    {
            start_ptr = new ListNode;
            ListNode *temp = start_ptr;
            ListNode Car[6];
    
            for (int i = 0; i<6; i++)
                {
                    cout << "Please enter the make of the vehicle: ";
                    cin >> temp->make;
                    Car[i].make;
                    cout << "Please enter the model of the vehicle: ";
                    cin >> temp->model;
                    Car[i].model;
                    cout << "Please enter the color of the vehicle: ";
                    cin >> temp->color;
                    Car[i].color;
                    cout << "Please enter the year of the vehicle: ";
                    cin >> temp->year;
                    Car[i].year;
                    cout << "Please enter the mileage of the vehicle: ";
                    cin >> temp->mileage;
                    Car[i].mileage;
                    
                    cout << "__________________________________________________________\n"<< endl;
    
            }
    
            for (int i=0; i<6; i++) 
            {
            Car[i].car_details();
            }
    
    
            // Change and print Information
            Car[3].year(2004);
    
            return 0;
        }
    
     
  6. Apr 11, 2010 at 9:30 PM
    #6
    snowgod06

    snowgod06 UG legend wannabe

    Joined:
    Apr 26, 2009
    Member:
    #16454
    Messages:
    5,385
    Gender:
    Male
    First Name:
    Rob
    A farm field, OREGON
    Vehicle:
    06 Access Cab CLONE
    crap bolted and welded together....
    to add more cars from my old days doing java, you should be able to change the limit in your "for loop" parameters. Instead of it being 1<6 you could change that variable, or put perhaps a verification of input loop in after it all and put one of the options as enter MORE cars. I just cant remember if you can put the verification of input in the "for loop" or after it. try doing that if that makes any sense lol.

    EDIT, actually i take almost all that back after i re-read your code. Do you want to be able to have just an unlimited entery for cars or do you want to set the list to 9?
     
  7. Apr 11, 2010 at 9:36 PM
    #7
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    And guys I am unsure but by running this program I do not feel that I may be doing this right...I am going to post what I am trying to do an someone please inform me if I have it all wrong if you can!

    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.)
     
  8. Apr 12, 2010 at 6:44 PM
    #8
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Anyone able to help me yet??
     
  9. Apr 13, 2010 at 8:12 PM
    #9
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Anyone got any help??
     
  10. Apr 13, 2010 at 8:15 PM
    #10
    NumNutz

    NumNutz One of the original 7928

    Joined:
    Jul 14, 2008
    Member:
    #7928
    Messages:
    4,845
    Gender:
    Male
    Virginia Beach, VA
    Vehicle:
    07 Tacoma 4x4 - Kings, TC, Dakars, broken stuff
    Lots.
    Oh look, there goes this concept sailing right over my head. Goodluck!
     
  11. Apr 13, 2010 at 9:51 PM
    #11
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Updated code please review if you can

    H 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);
    
        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 CarList::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();
            }
    
        void CarList::car_details();
          {
          ListNode * current = firstNodeInList;
          while(current != NULL)
          cout << current->Car;
          }
    
            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();
          };
    

    CPP
    Code:
    #include "CarClass.h"
    #include <iostream>
    using namespace std;
    
    int main()
    {
        CarList inventory;
         Car carObject;
         bool addAnotherCar = true;
         while(addAnotherCar)
          {
            char again;
            string temp;
    
              ListNode *start_ptr = NULL;
                    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;
    
                    cout<<"Would You like To Enter Another Car (Y/N) :";
                    cin>>again;
    
                    if(toupper(again) != 'Y')
                            break;
                    temp->next = new ListNode;
                    temp = temp->next;
                    cout <<"____________________________________________________\n\n";
         
             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;
        }
    
     
  12. Apr 14, 2010 at 9:13 AM
    #12
    ocabj

    ocabj Well-Known Member

    Joined:
    Aug 24, 2009
    Member:
    #21681
    Messages:
    106
    Gender:
    Male
    Riverside, CA
    Vehicle:
    2010 Pyrite Mica Tacoma Standard Cab
    Removing an item from a linked list is simply an issue of pointer reassignment (the previous node's pointer should point to the next pointer of the item being removed), then freeing the memory used by the item to be deleted.

    So basic logic of deleting an item should check:

    1. When you reach the end of the list (basically a while current.next != null)
    2. When you have found the right node (current.data = data to match)
    3. Deallocate the node matched / to be deleted (prev.next set to current.next)
    4. Free the memory used by current node.
     
  13. Apr 14, 2010 at 7:37 PM
    #13
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar

    Ok 2 questions...

    1. How would i add 3 more

    2. will I do current.data = "then the name of the car or position of car??"
     
  14. Apr 14, 2010 at 8:00 PM
    #14
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,946
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Updated Code please review here are my errors and code

    ERRORS:
    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
    1>C:\Users\Justin Puckett\Documents\My Stuff\School Work\Second Semester\Spring 2009\C++\Week11Nodes\Test\Debug\Test.exe : fatal error LNK1120: 3 unresolved externals

    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;
        }
    
    H
    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;
          //}
    
     
  15. Apr 15, 2010 at 8:38 AM
    #15
    ocabj

    ocabj Well-Known Member

    Joined:
    Aug 24, 2009
    Member:
    #21681
    Messages:
    106
    Gender:
    Male
    Riverside, CA
    Vehicle:
    2010 Pyrite Mica Tacoma Standard Cab
    1. Why not just make the input menu infinite until you enter an escape character/string? (i.e. do { menu_input_code } (while input_string != some_exit_string) I don't see why you should hardcode how many vehicles that can be added.

    2. That's up to you. Personally, I'd probably delete by position (node number). Assuming you have a function to display the current list in order, then this would work.
     
To Top