linked list

hi everyone!
can someone help me with this program?
I have to make a linked list by reading a file. I have to read the command from the file and make an if else structure. If the command is "add"(this is the part I am having problems) I have to call a function that reads data in the file and adds those to the linked list. the "addOne" function is the one where the problem generates. Can you guys tell me what I am doing wrong?
This is the code:


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;


struct webData
{
string domain;
string IPnumber;
};

typedef webData infoType ; // typedef on page 425 in Malik


struct nodeType
{
infoType info;
nodeType *link;
};


void print (nodeType *list);
nodeType * addOne ( nodeType * first, infoType newItem );


int main(int argc, char *argv[])
{
string filename;
ifstream inFile;
int count;
string command;
string newIP;


nodeType *webList, *temp,;
webData webInfo;


//Prompt the user for an input file
cout << "Enter the name of the input file: " ;
cin >> filename;

inFile.open(filename.c_str () );

if (!inFile)
{
cout << "\nInput file is missing! \n\n";
system ("pause");
return 1;
}

cout << filename << " was opened." << endl<<endl;


webList = NULL;


inFile >> command;


while ( command != "quit" )
{

if ( command == "display" )
{
print(webList);
}

///////////////////////////////////////////////////
else if (command == "add")
{
inFile >> webInfo.domain;
inFile >> webInfo.IPnumber;
webList = addOne ( webList, webInfo );
///////////////////////////////////////////////////

//count++;
}

/* else if ( command == "change" )
{
inFile >> webInfo.domain;
inFile >> newIP;

count++;
}

else if ( command == "eliminate" )
{
inFile >> webInfo.domain;

}
else if ( command == "search" )
{
inFile >> webInfo.domain;

count++;
} */


inFile >> command;
}







system("PAUSE");
return EXIT_SUCCESS;
}

///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////

void print(nodeType *list)
{


nodeType *current;
current = list;


while (current != NULL)
{
cout << setw(30) <<left<< (current->info.domain);

cout <<setw(15)<<left << (current->info.IPnumber);

current = current->link;
cout << endl;
}
cout << endl;

}

//////////////////////////////////////////////////////////////////

nodeType * addOne ( nodeType * first, infoType newItem )
{

nodeType *newNode, *last;

last = NULL;

newNode = new nodeType;
newNode->info = newItem;
newNode->link = NULL;

if ( first == NULL ) //case 1: list is empty
{
first = newNode;
last = newNode;
}
else
{
newNode -> link = newNode;
last = newNode;

}

return first ;
}

////////////////////////////////////////////////////////////////


This is a part of the input file:

add www.spacebook.com 194.188.203.011
add www.myfacespace.org 191.198.103.223
add www.ratemystudent.com 192.168.251.244
add www.addtoothysmile.tv 191.168.312.112
display
quit
Last edited on
closed account (D80DSL3A)
Is it just the addOne() you are having trouble with?
The local variable last serves no purpose. You could eliminate it.
This line:newNode -> link = newNode; points newNode to itself, creating a closed list with one node.
Are you trying to add a new node to the front of the list here?
If so, the following should work:
1
2
3
4
nodeType *newNode = new nodeType;
newNode->info = newItem;
newNode->link = first;// good for ALL cases
first = newNode;
Topic archived. No new replies allowed.