Cannot get this code working right, any help is greatly appreciated. I've got it printing all of the values, however it's inputting the nodes incorrectly.
void insertTokens(listNode*& head, char* tokens[])
{
// Creates new node and inserts tokenized values
cout << "Inserting: " << tokens[0];
cout << " " << tokens[1];
cout << " " << tokens[2] << endl;
newNode = new listNode;
newNode-> lastName = tokens[0]; // Node value lastName
newNode-> firstName = tokens[1]; // Node value firstName
newNode-> age = tokens[2]; // Node value age
newNode-> next = NULL;
listNode* current = head; // set traversal pointer
if(!head) // If no head node, create one
{
head = newNode;
newNode-> next = NULL;
cout << "Head Node" << endl;
}
elseif(current-> lastName > newNode-> lastName)
{
newNode-> next = current;
head = newNode;
cout << "New Head Node" << endl;
}
else
{
if(current-> next == NULL) // Adds newNode to end of list
{
newNode-> next = current-> next;
current-> next = newNode;
cout << "eol" << endl;
}
cout << "start while" << endl;
while(current-> next-> lastName < newNode-> lastName) // Traverse the list
{
current = current-> next;
cout << "1" << endl;
if(current-> lastName == newNode-> lastName) // Compare lastName
{
/*if(current-> firstName == newNode-> firstName) // Compare firstName
{
}
else //if(current-> firstName < newNode-> firstName)
{
newNode-> next = current-> next;
current-> next = newNode;
}*/
cout << "2" << endl;
reportDuplicate();
}
elseif(current-> lastName < newNode-> lastName) // Insert node by last name
{
newNode-> next = current-> next;
current-> next = newNode;
}
cout << "3" << endl;
}//end while(traversal)
cout << "end while" << endl;
}//end else
This is the output. I am not sure why its printing in reverse order after a certain point.
C:\Users\Zero>g++ LinkListProject.cpp
C:\Users\Zero>a.exe names.txt
Input File is open...
Inserting: 3 3 3
Head Node
Inserting: 2 2 2
New Head Node
Inserting: 1 1 1
New Head Node
Inserting: 4 4 4
start while
1
3
end while
Inserting: 5 5 5
start while
1
3
end while
Inserting: 6 6 6
start while
1
3
end while
Inserting: 7 7 7
start while
1
3
end while
Inserting: 8 8 8
start while
1
3
end while
Inserting: 9 9 9
start while
1
3
end while
Incomplete Record:
Displaying List
--------------------------------------