pointer help

Having a problem getting these pointers to work. Not sure what im doing wrong, any help is appreciated.

The code keeps creating a head in the linklist instead of making a comparison. It keeps reading head as NULL, so creates a new head.



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
69
70
71
72
73
74
75
76
77
78
79
80
81
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

 cout << newNode-> lastName << newNode-> firstName << newNode-> age << endl;

 cout << "here" << endl;

 if(!current) // If no head node, create one
  {
   head = newNode;
   newNode-> next = NULL;

   cout << head-> lastName << endl;
   cout << "head created" << endl;
  }
 //else if(head-> lastName.compare(newNode-> lastName) > 0)
 else if(head-> lastName > newNode-> lastName)
  {
   newNode-> next = head;
   head = newNode;
   cout << "new head" << endl;
  }
 
 else
  {
	  cout << "there" << endl;
	 
  while((current-> next != NULL) && (current-> next-> lastName < newNode-> lastName)) // Traverse the list
   {
	  current = current-> next;

	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;
	   }*/
		 reportDuplicate();
     }	
	 
	else if(current-> lastName < newNode-> lastName)
	 {
	  newNode-> next = current-> next;
	  current-> next = newNode;
	 }
	cout << "node" << endl;
   }//end while(traversal)
 }//end else

}//end void insertTokens()
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

  // Possible node activity //

  // newNode-> lastName;   -value of lastName
  // newNode-> firstName;  -value of firstName
  // newNode-> age;        -value of age
  // newNode-> next;       -pointer to next node

//-----------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 if(!current) // If no head node, create one
  {
   head = newNode; 
   newNode-> next = NULL;

   cout << head-> lastName << endl;
   cout << "head created" << endl;
  }
 //else if(head-> lastName.compare(newNode-> lastName) > 0)
 else if(head-> lastName > newNode-> lastName)
  {
   newNode-> next = head; // here next node is assigned to parent node
   head = newNode;  // here parent node is assigned back to child node ??
   cout << "new head" << endl;
  }


Is the above commented part supposed to do this? (assigning something that's already assigned)
That is to keep head at the head of the list. The input file contains last names, first names, and ages and have to be put in order.

If a new node that is less than the one that is currently the head node, link its next to the current head node, then make it the head node.
Topic archived. No new replies allowed.