gpt4 book ai didi

C链表searchNode/freeList方法(seg fault)

转载 作者:太空宇宙 更新时间:2023-11-04 02:58:29 24 4
gpt4 key购买 nike

我一直在为类(class)从 Java 过渡到学习 C。当前的练习是为 LinkedList 实现 removeAtFront()、searchNode() 和 freeList() 方法。我从理论上理解这是如何工作的——我会用 Java 快速完成它,我已经尝试了几个小时,但不明白为什么下面的代码不起作用。

删除方法似乎有效,生成正确的修改列表,直到删除节点后调用搜索方法。然后总是产生seg fault 11。 free 方法也总是会产生段错误。

我不是要别人做我的作业,但如果我能指出正确的方向,我将不胜感激!

给定的 Node* 结构是:

typedef struct Node
{
char *word;
struct Node *next;
} Node;

main() 之外的方法是这样的:

void insertAtFront( Node **head, char * key )
{
Node *new = malloc( sizeof(Node) );
if (!new) fatal("Malloc of new Node failed");
new->word = key;
new->next = *head;
*head = new;
}

void insertAtTail( Node **head, char * word )
{
if (!(*head)) insertAtFront(head, word);
else insertAtTail(&(*head)->next, word);
}

void removeAtFront( Node ** head )
{
Node *tmp = *head;
if (!tmp) return;

*head = tmp->next;
free(tmp->word);
free (tmp);
}

void removeNode( Node ** head, char * key )
{
Node *tmp = searchNode(*head, key);
if (tmp) removeAtFront (&tmp);
}

Node * searchNode ( Node * head, char * key )
{
if (!head || (strcmp(head->word, key) == 0)) return head;
return searchNode(head->next, key);
}

void freeList( Node ** head )
{
if (!head) return;
if (&(*head)->next) freeList (&(*head)->next);
removeAtFront(head);
}

编辑:其中一条评论解决了我的 freeList() 方法问题,但其他人要求提供更多代码。这个赋值的问题是我只被允许修改 insertAtTail()、removeAtFront()、remove()、search() 和 freeList() 方法。不过,我将在下面发布主要方法。不过,我认为单词值在其中的分配是正确的。

Node *searchNode( Node * head, char * key );
void insertAtFront( Node **head, char * key ); // ALREADY WRITTEN FOR YOU
void insertAtTail( Node **head, char * key );
void removeAtFront( Node ** head );
void removeNode( Node **head, char * key );
void freeList( Node **head );
void printList( Node * head ); // ALREADY WRITTEN FOR YOU
void fatal( char * msg ); // ALREADY WRITTEN FOR YOU

#define BUFFER_CAP 20

int main()
{
Node *head = NULL;

while (1)
{
char option;
printf("\nChoose 'H'ead Insert, 'T'ail insert, 'R'emove, 'S'earch, F'ree, 'Q'uit " );
fflush( stdout );
int result = scanf(" %c%*[^\n]", &option); getchar(); // MAGIC BULLET TO CORRECTLY READ A SINGLE CHAR FROM STDIN
if (result <1) fatal("failure reading from stdin\n");

if (option == 'H' )
{
char * word=malloc(BUFFER_CAP); // DONT ENTER ANY LONG WORDS!
printf("Enter a word to insertAtFront: " );
fflush( stdout );
char * result = fgets( word, BUFFER_CAP, stdin );
if (result==NULL) fatal("failure reading from stdin\n");
strtok(word,"\n"); // overwrites '\n' with '\0'
insertAtFront( &head, word ); /* shallow copy string into list */
printList( head );
}
if (option == 'T' )
{
char * word=malloc(BUFFER_CAP); // DONT ENTER ANY LONG WORDS!
printf("Enter a word to insertAtTail: " );
fflush( stdout );
char * result = fgets( word, BUFFER_CAP, stdin );
if (result==NULL) fatal("failure reading from stdin\n");
strtok(word,"\n"); // overwrites '\n' with '\0'
insertAtTail( &head, word ); /* shallow copy string into list */
printList( head );
}
if (option == 'R' )
{
char * word=malloc(BUFFER_CAP); // DONT ENTER ANY LONG WORDS!
printf("Enter a word to remove: " );
fflush( stdout );
char * result = fgets( word, BUFFER_CAP, stdin );
if (result==NULL) fatal("failure reading from stdin\n");
strtok(word,"\n"); // overwrites '\n' with '\0'
removeNode( &head, word );
printList( head );
free( word ); // we were just using it for matching
}
if (option == 'S' )
{
char * word=malloc(BUFFER_CAP); // DONT ENTER ANY LONG WORDS!
printf("Enter a word to find: " );
fflush( stdout );
char * result = fgets( word, BUFFER_CAP, stdin );
if (result==NULL) fatal("failure reading from stdin\n");
strtok(word,"\n"); // overwrites '\n' with '\0'
if (searchNode( head, word ))
fprintf(stderr, "%s FOUND\n",word );
else
fprintf(stderr, "%s NOT FOUND\n",word );
printList( head );
free( word ); // we were just using it for matching
}
if (option == 'F' ) // free the entire list (remember to set head to NULL)
{
freeList( &head );
printList( head );
}
else if (option == 'Q' )
exit( 0 );
} // END WHILE

return 0;
}

最佳答案

当您使用 Node *new = malloc( sizeof(Node) ); 为节点分配内存时,您是为指针而不是数据分配内存。您确实为 char 分配了内存,例如:(这只是一个想法)

new->word= malloc(sizeof(char)*(strlen(key) + 1));
strcpy(new->word, key)

否则你必须使用你为 key 动态分配内存。 (因为您执行 free(tmp->word);)

我认为你应该再放一些代码。你如何传递 key

关于C链表searchNode/freeList方法(seg fault),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14778405/

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