2012-11-26 36 views
0

我想在列表的末尾插入項目,但是,當我編譯它時,它確實爲Record ptr分配了內存,但它沒有將項目插入列表的末尾。細分故障。任何人都可以幫忙嗎?乾杯。在C中列表項的末尾插入項產生分段錯誤

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


/* these arrays are just used to give the parameters to 'insert', 
    to create the 'people' array */ 
char names[][10]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim", 
     "Harriet"}; 
int ages[7]= {22, 24, 106, 6, 18, 32, 24}; 


/* declare your struct for a person here */ 
typedef struct Record{ 
    char *name; 
    int age; 
    struct Record *next; 
} Record; 

//set the head pointer at the start of the list 
Record *headptr = NULL; 

static void insert (Record *p, char *s, int n) { 

/* create a new space for the new person */ 
Record *ptr =(Record*) malloc(sizeof(Record)); 

    /* check if it is succeeded */ 
    if(ptr == NULL){ 
     abort(); 
     printf("memory allocation fail"); 
     exit(1); 
    }else{ 
     printf("memory allocation to person - %s - \n", s);  
    } 

    //set the data for the new person 
    ptr->name=s; 
    ptr->age=n; 

    //ptr= NULL; 

    if(headptr==NULL){ 
     headptr = ptr->next; 
     ptr=headptr; 

    }else{ 
     while(ptr->next == NULL) { 
      ptr=ptr->next; 
     headptr=ptr; 
     } 
    } 
} 

int main(int argc, char **argv) { 

    /* declare the people array here */ 
    Record *p=headptr; 
    headptr = NULL; 

    //insert the members and age into the unusage array. 
    for (int i=0; i < 7; i++) { 
     insert (p,names[i], ages[i]); 

    /* do not dereference the pointer */ 
    } 

    /* print out a line before printing the names and ages */ 
    printf("\n"); 

    //set the pointer at the start of the list 
    p = headptr; 

    /* print the people array here*/ 
    for (int i = 0; i < 7; i++, p = p->next) { 
     printf("The name is: %s, the age is:%i\n", p->name, p->age); 
    } 


    /* This is the third loop for call free to release the memory allocated by malloc */ 
    /* the free()function deallocate the space pointed by ptr. */ 
    for(int i=0; i<7;i++){ 
     free(p); 
    } 

} 

回答

2

insert()功能有多個錯誤:

static void insert (Record *p, char *s, int n) { 

    /* create a new space for the new person */ 
    Record *ptr =(Record*) malloc(sizeof(Record)); 

    /* check if it is succeeded */ 
    if(ptr == NULL){ 
     abort(); 
     printf("memory allocation fail"); 
     exit(1); 
    }else{ 
     printf("memory allocation to person - %s - \n", s);  
    } 

    //set the data for the new person 
    ptr->name=s; 
    ptr->age=n; 
    ptr->next=NULL; 

    if(headptr==NULL) 
    { 
     headptr = ptr; 
    }else{ 
     // Iterate over complete list until the last element has been reached. 
     Record* tail = headptr; 
     while(tail->next!=NULL) 
     { 
      tail=tail->next; 
     } 

     // Append new element to last element. 
     tail->next = ptr; 
    } 
} 

然而,這個鏈表實現是相當低效的,我建議你上鍊表是如何用C實現讀了,你繼續編程之前:http://www.thegeekstuff.com/2012/08/c-linked-list-example/

+0

是的。歡呼聲,這解決了這個問題 – user1851359

2

當您分配一個新的Record,你永遠不會初始化它next指針。

您應該將它設置爲NULL而不是使用任何隨機值在那裏。

+0

我該怎麼做?請 – user1851359

+0

@ user1851359:將其設置爲NULL。 – NPE

+0

您確實編寫了OP的代碼,是嗎? @ user1851359 – alk