pointers 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

//-----------------------------------------------------------------------------
This is because you are passing 'head' by value. The changes to the pointer are not being reflected outside of the function, because it was only copied. Try changing the declaration of your function to this:
 
void insertTokens(listNode*& head, char* tokens[])


EDIT: Also, please don't cross-post on multiple forums.
Last edited on
Sorry about that, thanks for the help, its always the simple things.

It seems now, that the program is not wanting to link the nodes correctly. I would assume it's due to current-> next equaling NULL. I am not sure where I can correct that though.

This is the updated code.

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
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
  {
   
 while(current-> next)
  {
   if(current-> next-> lastName < newNode-> lastName)
    {
	 current = current-> next;
	 cout << "next" << endl;
    }
   else 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 << "dupe" << endl;
		 reportDuplicate();
     }	
	 
	else if(current-> lastName < newNode-> lastName)
	 {
	  newNode-> next = current-> next;
	  current-> next = newNode;
	  cout << "input" << endl;
	 }

   cout << "end input" << endl;

   }//end while(traversal)
 }//end else

}//end void insertTokens() 


This is the output I am getting after making that referencing change.


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

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

Input File is open...

Inserting: 1 1 1
Head Node
Inserting: 2 2 2
Inserting: 3 3 3
Inserting: 4 4 4
Inserting: 5 5 5
Inserting: 6 6 6
Inserting: 7 7 7
Inserting: 8 8 8
Inserting: 9 9 9

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

1 1 1

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




Topic archived. No new replies allowed.