c语言 - 为什么没有将新元素添加到我的链表中?



这只是代码的一个片段,但我检查并知道所有字符串都很好地保存到"新的";元素(在函数SortedInsert中(;新的";没有链接到头部?我已经尝试了我能想到的一切,希望我只是错过了一些显而易见的东西。

typedef struct _Info* Position;
typedef struct _Info{
char name[MAX];
char surname[MAX];
Position next;
} Info;
(declaration inside main function:
Info headInfo = {.name = {0}, .surname {0}, .next = NULL};
Position head = &headInfo;
)
int SortedInsert(Position head, char name[], char surname[]){
Position prev = NULL, temp = NULL, new = NULL;
prev = head;
temp = head->next;
new = (Position)malloc(sizeof(Info));
if(!new){
return EXIT_FAILURE;
}
strcpy(new->name, name);
strcpy(new->surname, surname);
new->next = NULL;
if(head->next==NULL){
temp = new;
}
else{
// first sort, by surname
while(strcmp(temp->surname, new->surname) < 0){
prev = temp;
temp = temp->next;
}
// second sort, by name
while(strcmp(temp->name, new->name) < 0){
prev = temp;
temp = temp->next;
}        
new->next = prev->next;
prev->next = new;
}
return EXIT_SUCCESS;
}
int PrintList(Position head){
Position temp = NULL;
temp = head->next;
while(temp){
printf("%s ", temp->name);
printf("%sn", temp->surname);
printf("---n");
temp = temp->next;
}
return EXIT_SUCCESS;
}

一些问题:

  • temp = new不会在列表中插入任何内容。它只是将对新节点的引用复制到局部变量中。分配应为head->next。此外,没有必要为此单独设立一个案例。它可以用else部分中的代码来处理。

  • 插入点的检索不正确。如果在第一个循环中,strcmp调用返回1(而不是0(,那么第二个while循环根本不应该迭代:在这种情况下,名字是什么样的并不重要。temp的姓氏已经更大了,所以已经找到了插入点。类似地,如果strcmp调用返回0,则第二个循环应在第二次迭代中继续验证姓氏是否相同,。。。此外,该逻辑可以组合在一个循环中。

正确执行不是问题,但仍然:

  • 许多人认为,在代码中定期取消引用指针的结构中,对指针进行类型定义是一种糟糕的做法。请参阅以下问题的答案:键入def指针是个好主意吗?了解一些背景。所以我会继续使用Info *

  • 创建一个单独的函数来创建和初始化节点。

  • 评论说";第一排序"第二类";具有误导性。注释后面的循环中没有发生排序。列表已被排序。接下来的过程只是想根据排序顺序找到插入点。因此,评论可以改进。

  • 许多人认为最好不要强制转换malloc返回的值。

以下是SortedInsert函数的校正,以及用于创建节点的分离函数:

Info *createNode(char name[], char surname[]) {
Info *new = malloc(sizeof(*new));
if (new != NULL) {
strcpy(new->name, name);
strcpy(new->surname, surname);
new->next = NULL;
}
return new;
}
int SortedInsert(Info *head, char name[], char surname[]){
Info *new = createNode(name, surname);
if (new == NULL) {
return EXIT_FAILURE;
}
Info *prev = head;
Info *temp = head->next;
// Find insertion spot according to sort order
while (temp != NULL) {
int cmp = strcmp(temp->surname, new->surname);
if (cmp == 0) { // It's a tie. Then use name as discriminator
cmp = strcmp(temp->name, new->name);  
}
if (cmp >= 0) { // Found insertion spot
break;
}
prev = temp;
temp = temp->next;
}
new->next = prev->next;
prev->next = new;
return EXIT_SUCCESS;
}

最新更新