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
#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
returntrue;
}
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
returntrue;
} else {
returnfalse;
}
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
returnfalse;
} else {
returnfalse;
}
}
bool List::operator!=(const List& original) {
if (mySize == original.mySize) { //When both lists have the same size
if (mySize == 0) { //Both list are empty
returnfalse;
}
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
returnfalse;
} else {
returntrue;
}
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
returntrue;
} else {
returnfalse;
}
}
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;
}