我无法按数字顺序将结构插入到链表中。每个结构都有一个“数字”,如下面的代码所示。我试图让编号最小的结构成为列表的头部(即:成为“人”指向的结构)。我整天都在盯着这段代码,我不知道它有什么问题。任何帮助深表感谢。谢谢
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;
}
}
编辑**
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;
}