int main()
{ // Creat a linked list of Presidents in sorted order
// (internally)
class nodeType //We will use this nodeType for this program, other one can be ignored
{
public:
char president[256]; //char Array with 256 characters
nodeType *link; //pointer variable
};
nodeType *head; // Head of our list
head = NULL; //Head is NULL for now
string FileName = "C:\\Users\\Jordan\\Desktop\\Documents\\College\\Senior Year\\Spring 2013\\Data Structures\\Programs\\Presidents.txt";
ifstream fin;
fin.open(FileName.data());
for (int i = 0 ; i < 100 ; i++)
{
nodeType *temp = new nodeType; //get memory allocation for anew node
fin.getline(temp->president, 256); //read in the line from file
// PUT INPUT COMMAND HERE Delete this: temp->president = " ";
if (head == NULL) //If empty list
{
temp->link = head ; // link our node to the head
head = temp ; //make our node the head now
}
else
{
nodeType *hunter2 = head; //declare another node
for(; hunter2->link != NULL; hunter2 = hunter2->link ) //traverse through whole list
{
if ((hunter2->link)->president == temp->president ) break ; //break out when the next spot equal
}
temp->link = hunter2->link;
hunter2->link = temp;
}
}
// Traverse the linked list
nodeType *hunter = head;
string tester;
while (hunter != NULL)
{
tester = hunter->president;
if (!tester.empty())
{
cout << hunter->president << "\n";
delete hunter;
}
hunter = head;
}
cout << "\n";
system("PAUSE");
return EXIT_SUCCESS;
}
This list is created internally in main..
Look at line 41 if ((hunter2->link)->president == temp->president ) break ;
This is the line that should compare them and help put them in the correct spot. For some reason, no matter if I do == > or < it doesn't work. But for now, with == the list is imported exactly how it is presented in the file...
The file is a list of Presidents names and years of service looking like this...
George Washington (1789-1797)
John Adams (1797-1801)
Thomas Jefferson (1801-1809) ...etc...