C分段错误 - 链表

问题描述:

我刚刚在几周前开始用C编程,在程序中出现了分段错误。C分段错误 - 链表

我相信这是因为这些线路:

for (int i =0; i < HOW_MANY; i++) 
     { 
     people = people -> next; 
     if (people -> next == NULL) return people; 
    } //for 
     }//else 

这是基于我的psedocode我的意见C程序

struct person *insert_end (struct person *people, char *name, int age) { 
//create a new space for the new person 
    struct person *pointer = malloc(sizeof(struct person)); 
    // check it succeeded 
    if(pointer == NULL) 
    { 
    printf("The program could not allocate memory "); 
     exit(-1); 
    } 
    // set the data for the new person 
     strcpy(pointer -> name, name); 
     pointer -> age = age; 
     pointer -> next = people; 
    // if the current list is empty 
     if (people == NULL) 
     { 
     // set the new person's "next" link to point to the current list" 
    pointer -> next = people; 
    // return a pointer to the new person 
    return pointer; 
     } 
     else 
     { 
    // we need a loop to find the last item in the list 
    // (the one which as a "next" link of NULL) 
     for (int i =0; i < HOW_MANY; i++) 
     { 
     // set the "next link of this item to point 
      // to the new person, so that the person 
      // becomes the last item in the list 
      // (the next person should have a "next" link of NULL) 
     people = people -> next; 
     if (people -> next == NULL) 
     return people; 
    } //for 
     }//else 
     // return the start of the list 
     return pointer;  
} 

另外,让我知道如果你需要我的完整的C代码的程序,因为这只是一种方法

谢谢

萨拉:)

+2

可能更适合[CodeReview](http://codereview.stackexchange.com/) – Kninnug

+1

[不要强制转换malloc()!]的返回值(http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) – 2013-11-28 20:16:34

+0

你说'if(person == 0)'和'(* lastItem.next)= people; '但是没有变量称为人物广告。而且还有其他的这种性质的问题,所以很难弄清楚你有什么问题。 –

这完全是用于代码审查的问题,而是因为你没有问关于如何实现的算法,至少我觉得很适合堆栈溢出的问题。

对于初学者来说,这只是一个语法的事情,但我想你应该更换此:本

pointer -> age = age;

这个符号

(*pointer).age = age;

很多清洁在我意见,并且为了访问结构指针中的元素而被合并到C中。除此之外,你到目前为止的代码缺少一些右花括号。这里有一个版本正确的语法,以及其他重要的东西你的代码(比如什么@ H2CO3提到):

struct person *insert_end (struct person *people, char *name, int age) { 

//create a new space for the new person 
struct person *pointer = malloc(sizeof(struct person)); 

// check it succeeded 
if(pointer == NULL) 
{ 
    printf("The program could not allocate memory "); 
    exit(-1); 
} 

// set the data for the new person 
strcpy(pointer -> name, name); 
pointer -> age = age; 
pointer -> next = people; 

// if the current list is empty 
if (person == 0) 
{ 
    // set the new person's "next" link to point to the current list" 
    pointer -> next = people; 

    // return a pointer to the new person 
    return pointer; 
} 

else 
{ 
    // we need a loop to find the last item in the list 
    // (the one which as a "next" link of NULL) 
    for (int i =0; i < HOW_MANY; i++) 
    { 
     // set the "next link of this item to point 
     // to the new person, so that the person 
     // becomes the last item in the list 
     // (the next person should have a "next" link of NULL) 
     lastItem -> next = people; 
    } 

    // return the start of the list 
    return pointer;  
} 

} 

最后,由于我没有你的整个代码的完整副本,我也不想它可以为您提供如何实现您不确定的伪代码部分的建议。

使用循环在列表中找到的最后一个项目(即具有 “下一步”的一个空的链接)

找到一个链表的最后一个项目是简单的。下面的函数下面我已经制作说明了如何在一定的指数获得一个链表特定节点:

NODE* getNode(NODE* start, int index) 
{ 
    int i; 
    for(i = 0; i < index; i++) 
    { 
     start = start -> next; 
    } 
    return start; 
} 

该代码可以被修改,以便它,而不是返回的最后一个节点。

NODE* getLastNode(NODE* start) 
{ 
    for(;;) 
    { 
     start = start -> next; 
     if(start -> next == NULL) 
      return start; 
    } 
} 

上述代码遍历列表中的每个节点,直到它碰到一个已它的连接节点作为NULL。然后它返回最后一个节点。

现在您可以调用上述函数并获取最后一个节点。

设置此项目的“下一个”链接指向新的人,因此新的 人成为列表中的最后一个项目(即,新的人应该 有NULL的“下一个”链接)

我敢肯定你知道如何做到这一点从你上面提供的代码判断。

+1

嘿Cyg!感谢您的建议。这非常清楚和直接。我的代码现在编译,但出于某种原因,我得到了分段错误。我正在调用方法insert_end int i; (i = 0; i Sarah

+1

顺便说一句,我更新了我的代码,以您的建议。 – Sarah

+0

@Sarah尝试访问无法访问的内存时,通常会发生分段错误。通常,当实现链表时,当你试图访问一个“NULL”节点的成员时,就会发生这种情况。仔细查看代码并密切关注可能出现循环的实例。 – turnt