【问题标题】:C Insertion Sort - ImplementationC 插入排序 - 实现
【发布时间】:2013-12-15 01:04:48
【问题描述】:

我刚刚开始使用 C 进行编程,需要一些帮助来实现插入排序。

我正在做一个 C 列表插入排序。

这是它的伪代码,我想将其转换为 C

否则
使用循环查找列表中应该在新人之前的最后一项 将新人的“下一个”链接设置为指向此列表项之后的任何内容

设置此项的“下一个”链接指向新的人

返回(开始)列表

这是我的伪代码的部分实现

else    
      {
         for (int i =0; i < HOW_MANY; i++) 
         {

        people = people -> next;
        if (people -> next == NULL) return people;
     } //for
      }//else

       return pointer;    
}

这是我的完整方法:

struct person *insert_sorted (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    
      {
         for (int i =0; i < HOW_MANY; i++) 
         {

        people = people -> next;
        if (people -> next == NULL) return people;
     } //for
      }//else

       return pointer;    
}

如果您需要完整的程序代码,请告诉我。

谢谢!

莎拉 :)

【问题讨论】:

  • 不清楚你在问什么。有什么问题?
  • 嘿 Paddy,很抱歉不够清楚。我的问题是我不确定如何将我的算法(第一个框)实现为 C 代码。第二个框是我尝试进行插入排序,但它不正确。

标签: c list sorting linked-list insertion-sort


【解决方案1】:

在将元素插入列表 *people 之前,您应该检查正确的位置。 试试这个:

struct person *insert_sorted (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;
struct person *cursor = people;
struct person *previous = people;
if(people == NULL){
    pointer->next = NULL;
    return pointer;
}
while(cursor!=NULL && strcmp(pointer->name,cursor->name)<0){
    previous = cursor;
    cursor = cursor->next;
}
if(previous!=NULL)
    previous->next = pointer;
pointer->next = cursor;
return people;
}

通过这种方式,您可以在名称按字母顺序排列的第一个元素之后插入新元素,并将其与下一个元素链接。

【讨论】:

  • 嗯...看来这给了我一个分段错误:/
  • 问题似乎出在previous->next = pointer;另外,我这样调用 insert_sorted 方法: for (i =0; i
  • 同样的问题 :( 由于第 98 行导致分段错误 (previous->next = pointer;) 真的很奇怪 :/
  • 如果要在列表开头设置元素,则需要在 previous->next 之前进行检查 ;-) 立即尝试。
  • 是的! :D 分段错误消失了。现在唯一的问题是由于某种原因它跳过了一个值(在我的例子中是第三个)。任何想法为什么要这样做?
【解决方案2】:

除了正确命名、消除多余的 cmets 并通过 indent -linux 运行它之外,我没有对这段代码做任何事情。

struct person *insert_sorted(struct person *people, char *name, int age)
{
        struct person *newperson = malloc(sizeof *newperson);
        if (newperson == NULL) {
                printf("The program could not allocate memory ");
                exit(-1);
        }

        strcpy(newperson->name, name);
        newperson->age = age;
        newperson->next = people;

        if (people == NULL) {
                newperson->next = people;
                return newperson;
        }

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

        return newperson;
}

有几件事立即弹出:

  • 不清楚您是否正在初始化所有 *newperson。最安全的是newperson = calloc(1, sizeof *newperson),那么读者就不用想了,总是好的结果。

  • 你没有显示结构定义,但你没有检查入站名称是否适合 newperson->name 的存储——如果 newperson->name 是一个指针,你已经分配根本没有。

  • 有一个多余的分配给 newperson->next,提出了相反的问题,是否有一些不明显或可能缺失的东西使得它变得必要。

  • 我看不到您在此处比较键值的位置。

【讨论】:

    猜你喜欢
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多