【发布时间】:2016-10-01 06:25:32
【问题描述】:
我一直在阅读,但仍然无法理解链接列表。我的问题是,由于某种原因,当我遍历列表以确定列表中的任何结构是否具有相同的名称或年龄时,它会删除列表,或者当我尝试将新节点添加到列表中时,它也会被删除。
该列表是动态的,因此我不会根据任何类型的计数,而是根据用户决定输入的结构数量。
任何方向都将不胜感激。
int add(char* name, char* genderValueString, char* breed, int age, float weight)
{
int result;
int count = 0;
gender curGen = 1;
struct dog *temp = malloc(sizeof(struct dog));
strcpy(temp->name, name);
strcpy(temp->breed, breed);
temp->age = age;
temp->weight = weight;
if (strcmpi(genderValueString, "male") == 0) {
curGen = 0;
}
temp->genderValue = curGen;
if (list == NULL) {
temp->next = list;
list = temp;
result = 1;
}
else {
while (list != NULL) {
if (strcmpi(list->name, name) == 0 && list->age == age) {
result = 0;
}
else {
result = 1;
}
list = list->next;
}
if (result == 1) {
while (list != NULL) {
if (strcmpi(list->name, name) > 0 || list->age > age) {
struct dog *prev = list;
list = temp;
list->next = prev;
}
list = list->next;
}
}
}
return result;
}
【问题讨论】:
标签: c linked-list