Linked List

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

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

#1 tarheelfan_08  Icon User is offline

  • D.I.C Regular

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

Linked List

Posted 11 April 2010 - 09:54 AM

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



Is This A Good Question/Topic? 0
  • +

Replies To: Linked List

#2 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: Linked List

Posted 11 April 2010 - 09:59 AM

For starters, each node should only have one car in it. You need to have a pointer to the start of your list (something like struct ListNode * start;) and then build your list off of that (you will need to write functions to add and remove members from the list.) I'd suggest taking a look around the tutorials section, a lot of people have written some great linked list tutorials.

Cheers,
Bodom
Was This Post Helpful? 0
  • +
  • -

#3 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 - 10:24 AM

Like this??

#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 *start_ptr = NULL;
int main() {
		
	string tempString = new ListNode;
	int tempString = new ListNode;

	
	cout << "Please enter the make of the vehicle: ";
	cin >> temp->make;


Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Linked List

Posted 11 April 2010 - 10:57 AM

i would do something like this
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;


Was This Post Helpful? 1
  • +
  • -

#5 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 - 10:58 AM

Ok cool, but I have a question why did you do it like this
ListNode *next;   //Pointer to next node

        ListNode(void)
                :next(NULL)
        {}


Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Linked List

Posted 11 April 2010 - 11:06 AM

just to set the pointer in the struct to NULL for safety
Was This Post Helpful? 0
  • +
  • -

#7 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 - 07:26 PM

Here is what I have

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

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

This post has been edited by tarheelfan_08: 11 April 2010 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#8 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Linked List

Posted 11 April 2010 - 07:51 PM

I bet you have a book on linked lists. Check it out, lots of good stuff in it. Some books refer to the
ListNode * start;
as the "head"(Nell Dale) of the list.

you can start at the head, which is the pointer to the first element in the list, to iterate through the list.

Quote

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


So.. iterate through the list (it involves using the next pointer, and making it the current pointer).

while current!=NULL
current=current->next;
cout << current->data;


Was This Post Helpful? 0
  • +
  • -

#9 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 - 07:52 PM

I am sorry but I am confused!
Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Linked List

Posted 11 April 2010 - 08:02 PM

can your overthinking that bigtime. Take a look, its just using a loop an allocating memory for the next node for every iteration
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;
	}


Was This Post Helpful? 2
  • +
  • -

#11 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:10 PM

to print it would i do something like


for (int x=0; x<6; x++) {
			ListNode[x].car_details();
		}



EDITED

This post has been edited by tarheelfan_08: 11 April 2010 - 08:13 PM

Was This Post Helpful? 0
  • +
  • -

#12 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Linked List

Posted 11 April 2010 - 08:13 PM

whats car array?

just iterate through your link list starting at your startingPointer
Was This Post Helpful? 0
  • +
  • -

#13 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:18 PM

I edited per your question!

And see I need it to iterate through it and print it using the car_details function. In that format!

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


Was This Post Helpful? 0
  • +
  • -

#14 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1074
  • View blog
  • Posts: 4,533
  • Joined: 09-June 09

Re: Linked List

Posted 11 April 2010 - 08:27 PM

take a look
ListNode *start_ptr = NULL;
int main()
{
	start_ptr = new ListNode;
	ListNode *out = start_ptr;

	while(out->next!=NULL)
	{
		cout<<out->color<<endl
			<<out->make<<endl
			<<out->year<<endl;
		//ect...
		out = out->next;
	}
	


Was This Post Helpful? 1
  • +
  • -

#15 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:32 PM

Yea i got that, but what if I want it to use the car_details function in the header file?? How will I do that??
Was This Post Helpful? 0
  • +
  • -

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