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

时间:2013-02-08 17:49:21

标签: c search linked-list free nodes

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

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

我不是要求人们做我的作业,但如果我能指出正确的方向,那将非常感激!

给定的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;
}

2 个答案:

答案 0 :(得分:2)

当您使用Node *new = malloc( sizeof(Node) );为节点分配内存时,您将为指针分配内存,但不为数据分配内存。你的确为char分配内存也像:( 它只是一个想法

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

除此之外,您必须动态地为key分配内存。 (因为你做free(tmp->word);

我认为你应该再添加一些代码。你如何通过key

答案 1 :(得分:0)

是的,如前面的回答所示,虽然您已为节点分配了内存,但您并未在每个节点中为单词分配内存。有时它可能会工作,而不会导致段错误,那时你正在使用你没有声称的“某人”内存,导致破坏内存位置。