broken comparisons?

I am having an issue getting this function to set these nodes properly. I don't know if I am just overlooking something or what....

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
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;
  }
 
 else if(current-> lastName > newNode-> lastName)
  {
   newNode-> next = current;
   head = newNode;
   cout << "New Head Node" << endl;
  }
 
 else
  {
    cout << "start while" << endl;
 while((current-> next != NULL) && (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();
     }	
	 
	else if(current-> lastName < newNode-> lastName)
	 {
	  newNode-> next = current-> next;
	  current-> next = newNode;
	 }

	cout << "3" << endl;

   }//end while(traversal)
 cout << "end while" << endl;
 }//end else


}//end void insertTokens() 


This is my output--------------


C:\Users\Zero>g++ LinkListProject.cpp

C:\Users\Zero>a.exe input.txt

Input File is open...

Inserting: Charles Price 25
Head Node
Inserting: Beatrice Louvre 16
New Head Node
Inserting: Alexander Bell 36
New Head Node
Inserting: Devon Wheatley 62
start while
1
3
end while
Inserting: Franklin Roosevelt 51
start while
1
3
end while
Inserting: Edward ScissorHands 44
start while
1
3
end while
Inserting: Liam Neeson 56
start while
1
3
end while
Inserting: Gabriel Iglesias 33
start while
1
3
end while
Inserting: Hermione Granger 22
start while
1
3
end while
Inserting: Ian Newman 26
start while
1
3
end while
Inserting: Jack Ollivander 31
start while
1
3
end while
Inserting: Mike Baker 89
start while
1
3
end while
Inserting: Nolan Ryan 7
start while
1
3
end while
Inserting: Hermione Granger 22
start while
1
3
end while
Inserting: Oedipus Rex 99
start while
1
3
end while
Inserting: Perseus Maximus 55
start while
1
3
end while
Inserting: Quentin Tarantino 34
start while
1
3
end while
Inserting: Robert Galant 13
start while
1
3
end while
Inserting: Steven Rodgers 69
start while
1
3
end while
Inserting: Tyrian Lannister 30
start while
1
3
end while
Inserting: Ulysses Grant 65
start while
1
3
end while
Inserting: Victor Ey 66
start while
1
3
end while
Inserting: Xavier Bronson 43
start while
1
3
end while
Inserting: Yusef Johnson 76
start while
1
3
end while
Inserting: Robert Galant 13
start while
1
3
end while
Inserting: Zebulon Ngoc 49
start while
1
3
end while

Displaying List
--------------------------------------------------------

Alexander Bell 36
Beatrice Louvre 16
Zebulon Ngoc 49
Robert Galant 13
Yusef Johnson 76
Xavier Bronson 43
Victor Ey 66
Ulysses Grant 65
Tyrian Lannister 30
Steven Rodgers 69
Robert Galant 13
Quentin Tarantino 34
Perseus Maximus 55
Oedipus Rex 99
Hermione Granger 22
Nolan Ryan 7
Mike Baker 89
Jack Ollivander 31
Ian Newman 26
Hermione Granger 22
Gabriel Iglesias 33
Liam Neeson 56
Edward ScissorHands 44
Franklin Roosevelt 51
Devon Wheatley 62
Charles Price 25

--------------------------------------------------------
End of List

Hold 'Ctrl' and press 'C' to exit...


The names are supposed to be in alphabetical order by last name as of right now, and then I will work on first name, then age. I just want to get it into last name order.
current-> lastName > newNode-> lastName
It's impossible to know what this means without knowing what lastName is.
I'm sorry, I didn't want to fill up the page with code.

lastName is a string.

(I'm not sure if that is the answer you are looking for)
I have a hard time reasoning about what your sort is doing. The main problem is basically that you're modifying the list as you iterate it, so at one point current and newNode may be equal (the pointers, not the objects). After that, I have no idea what it'll do.
Don't insert the newNode as part of the iteration. First find where it should go and only then insert it.
I'm not sure what changes I could make to insert it differently.
Topic archived. No new replies allowed.