I'm having trouble inserting a struct into a linked list in numerical order. Each struct has a "number" as indicated in the code below. I'm trying to have the struct with the lowest number be the head of the list (ie: be the struct pointed to by "people"). I've been staring at this code on and off all day and I can't figure out what's wrong with it. Any help is much appreciated. Thanks
Person *addPerson(Person *people, Person *addition, int &numList)
{
if (people == NULL && numList == 0)
{
people = addition;
numList++;
return people;
}
if (people->number >= addition->number)
{
addition->nextPerson = people;
people = addition;
return people;
}
else if (people->number < addition->number && people->nextPerson != NULL)
{
addPerson(people->nextPerson, addition, numList);
}
else if (people->number < addition->number && people->nextPerson == NULL)
{
people->nextPerson = addition;
numList++;
return people;
}
}
EDIT**
int main()
{
Person *blake = new Person;
Person *kyra = new Person;
Person *elon = new Person;
Person *bill = new Person;
Person *people = NULL;
blake->number = 1;
blake->name = "blake";
blake->lastName = "madden";
blake->nextPerson = NULL;
kyra->number = 2;
kyra->name = "kyra";
kyra->lastName = "madden";
kyra->nextPerson = NULL;
elon->number = 3;
elon->name = "elon";
elon->lastName = "musk";
elon->nextPerson = NULL;
bill->number = 4;
bill->name = "bill";
bill->lastName = "gates";
bill->nextPerson = NULL;
int num = 0;
int &numList = num;
people = addPerson(people, blake, numList);
people = addPerson(people, kyra, numList);
people = addPerson(people, elon, numList);
people = addPerson(people, bill, numList);
cout << people->name << '\n' << people->lastName;
}