custom list class read and write functions

i have to build a custom list class. everything works fine except my readFrom and WriteTo functions. i can read from the file to a temporary list but im having trouble setting the list in the in the function to the temporary list.
at line 152-154 i set myFirst,myLast, and mySize to that of the temp list but when it gets to the end of the function they all get changed.
input.txt contains: 11 22 33 44 55. and only mySize is beign correctly retured

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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include "List.h"
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <cassert>

List::List() {
	mySize = 0;
	myFirst = myLast = 0;
}

unsigned List::getSize() const {
	return mySize;
}

Item List::getFirst() const {
	if (mySize < 0 || myFirst == NULL) {
		throw std::underflow_error("Error");
	} else {
		return myFirst->myItem;
	}
}

Item List::getLast() const {
	if (mySize < 0 || myLast == NULL) {
		throw std::underflow_error("Error");
	} else {
		return myLast->myItem;
	}
}

void List::append(const Item it) {
	Node *nodePtr = new Node(it, NULL);
	if ( mySize == 0 ) {
		myFirst = nodePtr;
	} else {
		myLast->myNext = nodePtr;
	}
	myLast = nodePtr;
	mySize = mySize + 1;
}

List::List(const List& original) {
	myFirst = myLast = NULL;
	mySize = 0;
	if( original.getSize() > 0 ) {
		Node* oPtr = original.myFirst;
		while (oPtr != NULL) {
			append(oPtr->myItem);
			oPtr = oPtr->myNext;
		}
	}
}

List& List::operator=(const List& original) {
	if (this != &original) {
		if ( mySize != 0) {
			mySize = 0;
			delete myFirst;
			myFirst = myLast = NULL;

		}
		if ( original.mySize != 0) {
			Node *nPtr = original.myFirst;
			while ( nPtr != NULL ) {
				append(nPtr->myItem);
				nPtr = nPtr->myNext;
			}
		}
	}
	return * this;
}

bool List::operator==(const List& original) {
	if (mySize == original.mySize) {	//When both lists have the same size
		if (mySize == 0) {		//Both list are empty
			return true;
		}
		Node *nPtr = original.myFirst;		//Temp node for original
		Node *nPtr2 = myFirst;		//Temp node for this
		while (nPtr != NULL) {		//While myNext is still pointing at another node
			if (nPtr2->myItem == nPtr->myItem) {		//If the items of each node are equal return true
				return true;
			} else {
				return false;
			}
			nPtr = nPtr->myNext;		//Temp pointers point to the next node in the list
			nPtr2 = nPtr2->myNext;
		}

	}
	if (mySize != original.mySize) {		//If the lists are not the same size return false
		return false;
	} else {
		return false;
	}
}

bool List::operator!=(const List& original) {
	if (mySize == original.mySize) {	//When both lists have the same size
		if (mySize == 0) {		//Both list are empty
			return false;
		}
		Node *nPtr = original.myFirst;		//Temp node for original
		Node *nPtr2 = myFirst;		//Temp node for this
		while (nPtr != NULL) {		//While myNext is still pointing at another node
			if (nPtr2->myItem == nPtr->myItem) {		//If the items of each node are equal return false
				return false;
			} else {
				return true;
			}
			nPtr = nPtr->myNext;		//Temp pointers point to the next node in the list
			nPtr2 = nPtr2->myNext;
		}

	}
	if (mySize != original.mySize) {		//If the lists are not the same size return false
		return true;
	} else {
		return false;
	}
}

void List::readFrom(istream& in) {
	Item it;
	List l1;
	cin >> it;
	l1.append(it);
	myFirst = l1.myFirst;
	myLast = l1.myLast;
	mySize = l1.mySize;
}

void List::writeTo(ostream& out) {
	Node *nodePtr = myFirst;
	for (unsigned i = 0; i < mySize; i++) {
		out << nodePtr->myItem << ' ';
		nodePtr = nodePtr->myNext;
	}
}

void List::readFrom(const string& fileName) {
	ifstream fin( fileName.c_str() );
	assert( fin.is_open() );
	Item it;
	List l1;
	while (fin.good()) {
		fin >> it;
		l1.append(it);
	}
	fin.close();
	myFirst = l1.myFirst;
	myLast = l1.myLast;
	mySize = l1.mySize;
}

void List::writeTo(const string& fileName) const {
	ifstream fout( fileName.c_str() );
	assert(fout.is_open() );
	Node *nodePtr = myFirst;
	for (unsigned i = 0; i < mySize; i++) {
		cout << nodePtr->myItem;
		cout << ' ';
		nodePtr = nodePtr->myNext;
	}
	fout.close();
}

void List::prepend(const Item it) {
	 Node *nodePtr = new Node(it, myFirst);		//makes a node with item it point at myFirst
	 myFirst = nodePtr;		//myFirst points and the new node
	 mySize++;		//increment mySize
}

unsigned List::getIndexOf(const Item it) const {
	Node *tempPtr = new Node(it, myFirst);		//make a node with item it point at myFirst
	int index = 0;
	while (tempPtr->myNext != NULL) {		//while there is still another node in the list
		if (it == tempPtr->myNext->myItem) {	//if it equals the item in next node
			return index;	//return the index
		}
		index++;	//increment the index
		tempPtr = tempPtr->myNext;		//advance the pointer to the next node
	}
	index = -1;		//else set and return index as -1
	return index;
}

void List::insert(const Item it, unsigned index) {
	Node *nodePtr = new Node(it, NULL);		//create new node containing it
	Node *nodePtr2 = myFirst;		//create a node pointing at myFirst
	if ( mySize == 0 ) {
 		myFirst = nodePtr;	//set myFirst and myLast to the new pointer
 		myLast = nodePtr;
 	} else {
 		for ( unsigned i = 0; i < index-1; i++) {
 			nodePtr2 = nodePtr2->myNext;	//advance the pointer to the next node untill it is at the correct index
 		}
 		nodePtr->myNext = nodePtr2->myNext;		//set the new nodes myNext to the node after the given index
		nodePtr2->myNext = nodePtr;		//set the node before my index to point at the new node
		if (index == mySize) {		//if index is at the end of the list
			myLast = nodePtr;		//set myLast to the new pointer
		}
 	}
 	mySize = mySize + 1;	//increment mySize
}

Item List::remove(unsigned index) {
	Node *nodePtr = myFirst;		//points to myFirst
	Node *nodePtr2 = myFirst;		//points to myFirst
	if (index >= mySize-1) {
		for (unsigned i = 0; i < mySize-2; i++) {
			nodePtr = nodePtr->myNext;		//advance the pointer to the second to last node in the list
		}
		nodePtr->myNext = NULL;		//set that nodes myNext to NULL
		myLast = nodePtr;		//set myLast to that node
	}
	if (index <= 0) {
		myFirst = myFirst->myNext;		//set myFirst to the second node
		nodePtr->myNext = NULL;		//set the original myFirsts myNext to NULL
	}
	if (index > 0 && index < mySize-2) {
		for (unsigned i = 0; i < index-1; i++) {
			nodePtr = nodePtr->myNext;		//advances pointer to the node before the index
		}
		for (unsigned i = 0; i < index; i++) {
			nodePtr2 = nodePtr2->myNext;		//advances point to the node at the index
		}
		nodePtr->myNext = nodePtr2->myNext;		//sets the node before the index myNext value to the node after the index
		nodePtr2->myNext = NULL;		//sets the node at the index myNext value to NULL
	}
	mySize--;		//decrement mySize
}

List::~List() {
	delete myFirst;
	myFirst = myLast = NULL;
	mySize = 0;
}

List::Node::Node() {
	myItem = 0;
	myNext = NULL;
   }

List::Node::Node(Item it, Node* next) {
	myItem = it;
	myNext = next;
}

List::Node::~Node() {
//	cout << " ~Node()\n" << flush;
	delete myNext;
	myNext = NULL;
	myItem = 0;
}

bump
Topic archived. No new replies allowed.