C插入排序 - 实施

时间:2013-11-28 22:16:37

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

我刚开始使用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;    
}

如果你需要完整的程序,请告诉我。

谢谢!

莎拉:)

2 个答案:

答案 0 :(得分:1)

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

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;
}

以这种方式在第一个元素之后插入新元素,其名称按字母顺序降低,并将其与下一个元素链接。

答案 1 :(得分:0)

除了正确命名之外,我没有对此代码做任何事情,消除了多余的注释,并通过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-&gt;名称 - 如果newperson-&gt; name是指针,那么''完全没有为它分配。

  • 对新人进行了多余的分配 - >接下来,提出了相反的问题,即是否有一些不明显或可能缺失的东西使得它成为必要。

  • 我没有看到你在这里比较关键值。