2016-11-14 119 views
-1

在下面的程序中,我尝试按升序排序人员列表。然而,我得到了一个分段错误,我不知道如何改变程序以获得预期的结果。有什么建议么?链接列表排序C

#include <stdio.h> 
    #include <stdlib.h> 
    /* these arrays are just used to give the parameters to 'insert', 
     to create the 'people' array 
    */ 

    #define HOW_MANY 7 
    char *names[HOW_MANY]= {"Simon", "Suzie", "Alfred", "Chip", "John", "Tim", 
        "Harriet"}; 
    int ages[HOW_MANY]= {22, 24, 106, 6, 18, 32, 24}; 

    typedef struct person 
    { 
     char *name; 
     int age; 
     struct person *next; 
    }Person; 
    static int compare_people(Person *p1, Person *p2) 
    {  
     return strcmp(p1->name, p2->name); 
    } 

    static Person* insert_sorted(Person *headp, char *name, int age) 
    { 
     Person *p = malloc(sizeof(Person)); 
     if (p == NULL) 
     abort(); 
     p->name = name; 
     p->age = age; 
     if (headp == NULL) 
     { 
     headp = p; 
     return p; 
     } 
     else 
     { 
     Person *current = headp; 
     Person *temp =NULL; 
     while(current != NULL && compare_people(current, p) < 0) 
     { 
      temp = current; 
      if(compare_people(current,p) > 0) 
      break; 
      current = current->next; 
     } 
     p->next = current; 
     temp->next = p; 
     return headp; 
     } 

    } 
    int main(int argc, char **argv) 
    { 
     Person *people2 = NULL; 
     for (int i = 0; i < 7; i++) 
     { 
     people2 = insert_sorted(people2, names[i], ages[i]); 
     //printf ("name: %s, age: %i\n", people2->name, people2->age); 
     } 

     while(people2 != NULL) 
     { 
     printf ("name: %s, age: %i\n", people2->name, people2->age); 
     people2 = people2->next; 
     } 
     return 0; 
    } 
+1

当你的调试器会告诉你没有设置'next'指向任何地方,但你使用它。 –

+0

您将得到的最佳建议是学会使用调试器并学习学习[如何调试小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs /)。从长远来看,运行到Stackoverflow这样的基本调试问题对你来说是非生产性的。 – kaylum

回答

0

对于初学者来说,最好是定义函数compare_people以下方式

static int compare_people(Person *p1, Person *p2) 
{  
    return strcmp(p1->name, p2->name) < 0; 
} 

至于insert_sorted然后在循环中的条件

while(current != NULL && compare_people(current, p) < 0) 
    { 
     temp = current; 
     if(compare_people(current,p) > 0) 
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 
     break; 
     current = current->next; 
    } 

没有意义,因为如果条件是真的,那么当前迭代的while循环最初将不会被执行,因为循环的条件是

compare_people(current, p) < 0 

这些条件相互矛盾。

此外,您忘记将节点添加到空列表时将数据成员next设置为NULL。

使用你的方法的函数可以写成下面的方式

static int compare_people(Person *p1, Person *p2) 
{  
    return strcmp(p1->name, p2->name) < 0; 
} 

static Person* insert_sorted(Person *headp, char *name, int age) 
{ 
    Person *p = malloc(sizeof(Person)); 

    if (p == NULL) abort(); 

    p->name = name; 
    p->age = age; 

    if (headp == NULL || compare_people(p, headp)) 
    { 
     p->next = headp; 
     headp = p; 
    } 
    else 
    { 
     Person *current = headp; 

     while (current->next != NULL && !compare_people(p, current->next)) 
     { 
      current = current->next; 
     } 

     p->next = current->next; 
     current->next = p; 
    } 

    return headp; 
} 
0

(1)需要#include <string.h>

(2)你忘了初始化成员的next

p->name = name; 
p->age = age; 
p->next = NULL;//<--this 
if (headp == NULL) 
{ 
    headp = p; 
    return p; 
} 

(3)不考虑当tempNULL

变化

p->next = current; 
temp->next = p; 

p->next = current; 
if(temp) 
    temp->next = p; 
else 
    headp = p;