gpt4 book ai didi

C:比较字符串

转载 作者:行者123 更新时间:2023-11-30 15:21:19 25 4
gpt4 key购买 nike

我使用下面的代码将节点添加到双链表中,并根据它们的 word 按字母顺序排列它们。 。节点由 char * word 组成, struct NODES * prev ,和struct NODES * next 。阅读 file testing a 后,我遇到了一个问题,列表看起来像 NULL <- a <-> file <-> testing -> NULL添加一个包含 word = c 的节点它放置c之前a而不是 a 之间和file 。该函数打印 "Add: Placing c before list head c"似乎正在评估c < a 。但即使该评估不正确,我也不知道它如何取代 a在进行任何节点操作之前。如果有人知道可能导致此问题的原因,我将不胜感激。附:传入NODES * arg始终采用 arg -> next == NULL; arg -> prev == NULL; arg -> word != NULL; 的形式但如果尚未添加节点,列表的所有字段都可以为 NULL,list -> prev在调用函数时和函数终止时应始终为 NULL。

int addToList(struct NODES * list, struct NODES * arg){
fprintf(stderr,"Add: Adding %s\n", arg->word);
if(list->word == NULL){
list->word = (char *)malloc(strlen(arg->word));
strcpy(list->word, arg->word);
list->next = NULL;
list->prev = NULL;
fprintf(stderr,"Add: Head %s\n", list->word);
return 2;
}
struct NODES * abc = list;
while(abc->word != NULL){
if(strcmp(abc->word, arg->word)<0){
fprintf(stderr, "abc %s < arg %s", abc->word, arg->word);
if (abc->next != NULL)
abc = abc->next;
else{
abc->next = malloc(sizeof(NODE));
abc->next->prev = abc;
abc = abc->next;
abc->next = NULL;
abc->word = NULL;
}
}
else if(abc == list){
fprintf(stderr, "Add: Placing %s before list head %s\n", arg->word, list->word);
arg->next = list;
list->prev = arg;
arg->prev = NULL;
list = arg;
fprintf(stderr, "Add: Placed %s before %s\n", list->word, list->next->word);
return 3;
}
else{
fprintf(stderr, "Add: Placing %s between %s and %s\n", arg->word, abc->word, abc->next->word);
arg->next = abc;
arg->prev = abc->prev;
if(abc->prev != NULL)
abc->prev->next = arg;
abc->prev = arg;
fprintf(stderr, "Added %s after %s and before %s\n", arg->word, arg->prev, arg->next->word);
return 1;
}
}
abc->word = (char *)malloc(strlen(arg->word));
strcpy(abc->word, arg->word);
fprintf(stderr, "Added %s after %s and before %s\n", abc->word, abc->prev->word, abc->next);
return 1;
}

更新以反射(reflect)建议:

int addToList(struct NODES ** list, struct NODES * arg){
fprintf(stderr,"Add: Adding %s current head %s\n", arg -> word, (*list)->word);
if((*list) -> word == NULL){
(*list) -> word = malloc(strlen(arg->word)+1);
strcpy((*list) -> word, arg -> word);
(*list) -> next = NULL;
(*list) -> prev = NULL;
fprintf(stderr,"Add: Head %s\n", (*list) -> word);
return 2;
}
struct NODES * abc = (*list);
//while arg > abc
fprintf(stderr,"Comparing %s and %s\n", abc->word,arg->word);
while(strcmp(abc->word, arg->word)<0){
fprintf(stderr,"Comparing %s and %s\n", abc->word,arg->word);
if (abc -> next == NULL)
break;
abc = abc -> next;
}
if (abc == (*list)){
if(!(strcmp(abc->word, arg->word)<0)){
arg -> next = abc;
arg -> prev = NULL;
abc -> prev = arg;
*list = arg;
}
else{
abc -> next = arg;
arg -> prev = abc;
abc -> next = NULL;
}
return 5;
}
if(abc -> next != NULL){
fprintf(stderr, "Inserting %s between %s and %s\n", arg -> word, abc->prev->word,abc->word);
arg -> next = abc;
arg -> prev = abc -> prev;
arg -> prev -> next = arg;
abc -> prev = arg;
fprintf(stderr, "Added %s before %s and after %s\n", arg->word, arg->prev->word,arg->next->word);
return 3;
}
return 0
}

最佳答案

函数接收到的list参数是调用者拥有的列表指针的副本。要返回修改后的列表指针,该函数可能如下所示:

int addToList(struct NODES ** list, struct NODES * arg)

它会被称为这样的:

result = addToList(&list, arg);

该函数将提供一个像这样的新列表指针

*list = arg;

并且您当前拥有的所有列表访问权限将更加间接

if(list->word == NULL)

会变成

if((*list)->word == NULL)

更新

尝试这个简化的代码,我发现这比了解你的代码更容易。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct NODES {
struct NODES *prev;
struct NODES *next;
char *word;
};

void showList(struct NODES * list) {
while (list) {
if (list->prev)
printf("%-10s", list->prev->word);
else
printf("%-10s", "NULL");
printf(" <- %-10s -> ", list->word);
if (list->next)
printf("%-10s", list->next->word);
else
printf("%-10s", "NULL");
printf("\n");
list = list->next;
}
}

int addToList(struct NODES ** list, char *word){
struct NODES *wptr, *lptr = *list, *pptr = NULL;
if ((wptr = malloc(sizeof(struct NODES))) == NULL)
return -1;
wptr->prev = NULL;
wptr->next = NULL;
if ((wptr->word = strdup(word)) == NULL)
return -2;

if (lptr == NULL) {
*list = wptr; // first list item
return 0;
}

while (lptr) {
if (strcmp(word, lptr->word) <= 0) {
wptr->next = lptr; // insert before current node
wptr->prev = pptr;
if (pptr)
pptr->next = wptr;
else
*list = wptr;
lptr->prev = wptr;
return 1;
}
pptr = lptr;
lptr = lptr->next;
}

wptr->prev = pptr; // insert at tail
pptr->next = wptr;
return 2;
}

int main()
{
struct NODES *list = NULL;
addToList(&list, "one");
addToList(&list, "two");
addToList(&list, "three");
addToList(&list, "four");
addToList(&list, "five");
showList(list);
return 0;
}

程序输出:

NULL       <- five       -> four
five <- four -> one
four <- one -> three
one <- three -> two
three <- two -> NULL

关于C:比较字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29591958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com