2014-08-31 146 views
0

我刚开始学习c,我想为链接列表创建自己的库,因为目前我只需要它来处理字符串,但是我无法使其工作。我遇到了分段错误,我知道这可能是一个愚蠢的错误,但我无法找到它,任何人都可以帮助我吗?我的链接列表库不工作

这是头文件

#ifndef LIST_H 
#define LIST_H 

// node structure 
typedef struct _node 
{ 
    char *data; 
    struct _node *next;  
} node; 

void append(node *head, char* strd); 

void insert(node *head, char* strd); 

void del(node *head, char* strd); 

void display(node *head); 

int lenght(node *head); 

int search(node *head, char* strd); 

void freel(node *head); 

#endif 

这里是.c文件

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

void append(node *head, char* strd) 
{ 
    node *temp, *aux; 
    temp = malloc(sizeof(node)); 
    temp->data = malloc(strlen(strd) + 1); 
    strcpy(temp->data, strd); 
    aux = head; 
    while(aux->next != NULL) 
     aux = aux->next; 
    aux->next = temp; 
    temp->next = NULL; 
} 

void insert(node *head, char* strd) 
{ 
    node *temp; 
    temp = malloc(sizeof(node)); 
    temp->data = malloc(strlen(strd) + 1); 
    strcpy(temp->data, strd); 
    if(head == NULL) 
    { 
     head = temp; 
     temp->next = NULL; 
    } 
    else 
    { 
     temp->next = head; 
     head = temp; 
    } 
} 

void del(node *head, char* strd) 
{ 
    node *temp, *aux; 
    temp = head; 
    while(temp != NULL) 
    { 
     if(strcmp(temp->data, strd) == 0) 
     { 
      if(head == temp) 
      { 
       head = temp->next; 
       free(temp->data); 
       free(temp); 
      } 
      else 
      { 
       aux->next = temp->next; 
       free(temp->data); 
       free(temp); 
      } 
     } 
     else 
     { 
      aux = temp; 
      temp = temp->next; 
     } 
    } 
} 

void display(node *head) 
{ 
    node *aux; 
    aux = head; 
    while(aux != NULL) 
    { 
     printf("%s ", aux->data); 
     aux = aux->next; 
    } 
    printf("\n"); 
} 

int lenght(node *head) 
{ 
    int c = 0; 
    node *aux; 
    aux = head; 
    while(aux != NULL) 
    { 
     aux = aux->next; 
     c++; 
    } 
    return c; 
} 

int search(node *head, char* strd) 
{ 
    node *aux; 
    aux = head; 
    while(aux != NULL) 
    { 
     if(strcmp(aux->data, strd) == 0) 
     { 
      return 1; 
     } 
    } 
    return 0; 
} 

void freel(node *head) 
{ 
    node *aux, *prev; 
    aux = head; 
    prev = aux; 
    while(aux != NULL) 
    { 
     aux = aux->next; 
     free(prev->data); 
     free(prev); 
     prev = aux; 
    } 
} 

我用这个测试库

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

int main(void) 
{ 
    char *str = "testing"; 
    char *str2 = "hello"; 
    char name[50]; 
    printf("what's your name: "); 
    scanf("%49s",name); 
    node *head; 
    head = NULL; 
    insert(head, str); 
    append(head, str2); 
    append(head, name); 
    display(head); 
    freel(head); 
    return 0; 
} 
+4

您是否尝试通过调试程序与您的代码一起查看分段错误发生的位置?部分学习'C'正在学习调试发生分段错误的地方。 – GWW 2014-08-31 22:11:38

+0

谢谢,我正在学习使用gdb。 – james 2014-08-31 22:40:05

回答

3

head总是NULL因为你永远不分配其他任何东西给它。 insert中的分配对于具有相同名称的不同变量。

+0

非常感谢,我没有意识到我正在制作一个头部的副本,永远不会返回任何东西。 – james 2014-08-31 22:34:36