Transfer a normal list to Linked List

So I want to transfer my normal list to a linked list and this is what I have.

Issues in my code:

my code breaks at model, make while loop.

This is the code after I tried to transfer it to linked list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171

#include <string>
#include <iostream>
#include <istream>
#include <fstream>

using namespace std;



class usedCars
{
private:
	int Year, Miles;
	string Make, Model;
	float Price;

public:
	void setYear(int yr)
	{
		this-> Year = yr;
	}
	void setMiles(int mil)
	{
		this-> Miles = mil;
	}
	void setMake(string Mk)
	{
		this-> Make = Mk;
	}
	void setModel(string Md)
	{
		this-> Model = Md;
	}
	void setPrice(float Pr)
	{
		this-> Price = Pr;
	}
	int getYear()
	{
		return this-> Year;
	}
	int getMiles()
	{
		return this-> Miles;
	}
	string getMake()
	{
		return this-> Make;
	}
	string getModel()
	{
		return this-> Model;
	}
	float getPrice()
	{
		return this-> Price;
	}
};

struct node
{
	usedCars car;
	node *next;
};

int main()
{

	int Year;
	string Make, Model;
	float Price;
	char Dollar;
	usedCars * cars;
	int carsCount = 0;

/*	std::ifstream myfile("car.txt");
	if (myfile.is_open())
	{

		while (myfile >> Year >> Make >> Model >> Dollar >> Price)
		{
			carsCount++;
		}
	
	}
	myfile.close();
*/

	node *head = NULL;

	std::ifstream myfile("car.txt");
	if(!myfile.is_open())
	{
		return -1;
	}

	while(!myfile.eof())
	{
		
		node *newCar;
		newCar = (node*)malloc(sizeof(node));
		newCar->next = NULL;

		//set head=first node
		if(head == NULL)
		{
			head = newCar;
		}

		
		myfile >> Year;
		newCar->car.setYear(Year);

		myfile >> Make;
		//newCar->car.setMake(Make);

		myfile >> Model;
		//newCar->car.setModel(Model);

		myfile >> Dollar;
		myfile >> Price;
		newCar->car.setPrice(Price);
	}

	myfile.close();

	/*
	for(int jj=0;jj<carsCount-1;jj++)
	{
		for(int j=0;j<carsCount-1;j++)
		{
			if(cars[j].getPrice() > cars[j+1].getPrice())
			{
				//swap elements
				usedCars tmp;
				tmp = cars[j];
				cars[j] = cars[j+1];
				cars[j+1] = tmp;

			}
			else if(cars[j].getPrice() == cars[j+1].getPrice())
			{
			
				usedCars tmp;
				tmp = cars[j];
				cars[j] = cars[j+1];
				cars[j+1] = tmp;
			
			}
		}
	}*/

	node *temp;
	temp = head;

	if (head != NULL)
	{
		do
		{
			cout << temp->car.getYear() << " " << " " << ": " << Dollar << temp->car.getPrice() << endl;
			temp = temp->next;

		}while(temp != NULL);
	}
	system("pause");
	return 0;



}


the normal list code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168



#include <string>
#include <iostream>
#include <istream>
#include <fstream>

using namespace std;



class usedCars
{
private:
	int Year, Miles;
	string Make, Model;
	float Price;

public:
	void setYear(int yr)
	{
		this-> Year = yr;
	}
	void setMiles(int mil)
	{
		this-> Miles = mil;
	}
	void setMake(string Mk)
	{
		this-> Make = Mk;
	}
	void setModel(string Md)
	{
		this-> Model = Md;
	}
	void setPrice(float Pr)
	{
		this-> Price = Pr;
	}
	int getYear()
	{
		return this-> Year;
	}
	int getMiles()
	{
		return this-> Miles;
	}
	string getMake()
	{
		return this-> Make;
	}
	string getModel()
	{
		return this-> Model;
	}
	float getPrice()
	{
		return this-> Price;
	}
};

struct node
{
	usedCars car;
	node *next;
}

int main()
{

	int Year;
	string Make, Model;
	float Price;
	char Dollar;
	usedCars * cars;
	int carsCount = 0;

/*	std::ifstream myfile("car.txt");
	if (myfile.is_open())
	{

		while (myfile >> Year >> Make >> Model >> Dollar >> Price)
		{
			carsCount++;
		}
	
	}
	myfile.close();
*/

	node *head = null;

	std::ifstream myfile("car.txt");

	while(myfile.is_open())
	{
		node *newCar;
		newCar = (node*)malloc(sizeof(node));
		newCar->next = Null;

		//set head=first node
		if(head == null)
		{
			head = newCar;
		}
		
		myfile >> Year;
		newCar->car.setYear(Year);

		myfile >> Make;
		newCar->car.setMake(Make);

		myfile >> Model;
		newCar->car.setModel(Model);

		myfile >> Dollar;
		myfile >> Price;
		newCar->car.setPrice(Price);
	}

	myfile2.close();

	/*
	for(int jj=0;jj<carsCount-1;jj++)
	{
		for(int j=0;j<carsCount-1;j++)
		{
			if(cars[j].getPrice() > cars[j+1].getPrice())
			{
				//swap elements
				usedCars tmp;
				tmp = cars[j];
				cars[j] = cars[j+1];
				cars[j+1] = tmp;

			}
			else if(cars[j].getPrice() == cars[j+1].getPrice())
			{
			
				usedCars tmp;
				tmp = cars[j];
				cars[j] = cars[j+1];
				cars[j+1] = tmp;
			
			}
		}
	}*/

	node *temp;
	temp = head;

	if (head != Null)
	{
		do
		{
			cout << temp->car.getYear() << " " << temp->car.getMake() << " " << temp->car.getModel() << " " << ": " << Dollar << temp->car.getPrice() << endl;
			temp = temp->next;

		}while(temp != null);
	}
	system("pause");
	return 0;



}
Last edited on
bump..
http://www.cplusplus.com/forum/general/112111/

> I want to transfer my normal list to a linked list
¿what's a "normal" list?
¿what's the difference between the snips?


newCar = (node*)malloc(sizeof(node));
malloc() would not call the class constructor, so your members are not constructed.
Topic archived. No new replies allowed.