hello guys, if u are kind please explain me why the following function is not correct : void insertAfter(char *);
Here is the implementation.
Header File :
#ifndef _LinkedList_
#define _LinkedList_
class node
{
private :
char *name;
int age;
public:
node * next;
node();
~node();
char * getName() const;
int getAge() const;
void setName(char *);
void setAge(int);
static const int size = 10;
void displayList();
void insertAtTheEnd();
void insertInFrontOfList();
void insertAfter(char *);
void insertBefore(char *);
};
#endif
Here is the function :
void node::insertAfter(char *nume)
{
cout << "Insert after." << endl;
node * temp;
node * temp2;
char tempName[20];
int tempAge;
temp = new node;
cout << "Give the name : ";
cin >> tempName;
cout << "Give the age : " ;
cin >> tempAge;
temp->setAge(tempAge);
temp->setName(tempName);
temp2 = start_ptr;
if (temp2 == NULL)
{ cout << "Empty list." << endl;
start_ptr = temp;
return;
}
else
{
[B] while (temp2->getName() != nume)
{
temp2 = temp2->next;
};[/B]
if (temp2 == NULL )
{
temp2->next = temp;
temp->next = NULL;
}
else
{
temp->next = temp2->next;
temp2->next = temp;
};
};
};
The bolded part is were i thing is the problem. For example i have a list with just one element having name = paul. in the main i call the function insertAfter("paul"); The condition is (temp2->getName() != nume) is considerated true. Is there any problem when i compare the two strings ?