2014-08-31 150 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