Linked List

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 2981 Views - Last Post: 18 April 2010 - 01:09 AM Rate Topic: -----

#16 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 11 April 2010 - 08:40 PM

I got it!!! I just need to figure out now how to set a variable to recognize how many vehicles were intered in

#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;
	}



#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();
        }


Was This Post Helpful? 0
  • +
  • -

#17 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 11 April 2010 - 08:48 PM

Never mind...for some reason that only prints the first one then errors out! I can not get it to print each car!
Was This Post Helpful? 0
  • +
  • -

#18 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 11 April 2010 - 09:01 PM

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??

#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;
	}


This post has been edited by tarheelfan_08: 11 April 2010 - 09:23 PM

Was This Post Helpful? 0
  • +
  • -

#19 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 11 April 2010 - 09:18 PM

Edited post
Was This Post Helpful? 0
  • +
  • -

#20 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 11 April 2010 - 09:36 PM

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.)
Was This Post Helpful? 0
  • +
  • -

#21 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

Re: Linked List

Posted 11 April 2010 - 10:33 PM

I think you should do some more reading on linked list.
	start_ptr = new ListNode;
	ListNode *temp = start_ptr;
	//ListNode Car[6]; //what is the point of THIS??

	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;
	}


	ListNode *details = start_ptr;

	for (int i=0; i<6; i++) 
	{
		details->car_detail();
		details = details->next;
	}



Was This Post Helpful? 0
  • +
  • -

#22 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 12 April 2010 - 04:03 AM

Ok so lets say that I do it the way that you say. By doing it that way is that considered using listnode to hold and object of the car class.

And second! If I wanted to edit my code how would you do this with out a direct variable?? Like if I want to change the year of a car or the make of a car!
Was This Post Helpful? 0
  • +
  • -

#23 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 12 April 2010 - 06:43 PM

Edit: I have figured out how to edit my cars in this program. But my next question is how will I add 3 more cars later on in the program and delete one of my other cars. And I can not do this by adding to the loop, whatever I do will have to come after I edit my 2 cars in the program!
Was This Post Helpful? 0
  • +
  • -

#24 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

Re: Linked List

Posted 12 April 2010 - 07:04 PM

to add more cars then just keep adding links on to the list (thats why its called a linked list). go to your last car and allocate the new node, then set your pointer to that node
Was This Post Helpful? 0
  • +
  • -

#25 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 13 April 2010 - 12:57 PM

Here is my new code and it works. It ask for however many cars I want and prints 6 of them. But I am unsure how to get it to change something! For instance, lets say I need to change the year of the second car that is entered...How can I do that?? And how would I add more cars to it, once it has changed that car. And do you know how to find and delete one from the list??

#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;
				cout <<"____________________________________________________\n\n";
        }

		cout <<"____________________________________________________\n\n";

        ListNode *details = start_ptr;

        for (int i=0; i<6; i++) 
        {
                details->car_details();
                details = details->next;
        }



	    return 0;
	}



#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();
        }



Sorry I had to go back a few steps..my code did not do exactly what I wanted it to!

This post has been edited by tarheelfan_08: 13 April 2010 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#26 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 13 April 2010 - 08:11 PM

Anyone got any help??
Was This Post Helpful? 0
  • +
  • -

#27 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 13 April 2010 - 09:50 PM

Updated code please review if you can

H file
#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
#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;
	}


Was This Post Helpful? 0
  • +
  • -

#28 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 14 April 2010 - 07:37 PM

Sorry to be naggy or dumb but has anyone had a chance to look at this and help me out
Was This Post Helpful? 0
  • +
  • -

#29 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 14 April 2010 - 08:00 PM

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
#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

#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;
	  //}


Was This Post Helpful? 0
  • +
  • -

#30 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: Linked List

Posted 17 April 2010 - 03:10 PM

Here is my 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;
	}



#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??
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3