C插入排序-实现

C插入排序-实现,c,list,sorting,linked-list,insertion-sort,C,List,Sorting,Linked List,Insertion Sort,我刚开始用C语言编程,需要一些帮助来实现插入排序 我正在进行C列表插入排序。 这是它的伪代码,我想把它转换成C 否则 使用循环查找列表中应在新人之前的最后一项 将新人的“下一步”链接设置为指向此列表项后面的内容 设置此项目的“下一步”链接以指向新用户 返回(列表的开头)列表 这是我的伪代码的部分实现 else { for (int i =0; i < HOW_MANY; i++) { people = people

我刚开始用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;    
}
else
{
for(int i=0;i<多少;i++)
{
人=人->下一步;
如果(人员->下一步==NULL)返回人员;
}//为了
}//否则
返回指针;
}
以下是我的完整方法:

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;    
}
struct person*insert\u排序(struct person*people,char*name,int-age){
//为新人创建一个新空间
struct person*pointer=malloc(sizeof(struct person));
//检查它是否成功
if(指针==NULL)
{ 
printf(“程序无法分配内存”);
出口(-1);
}
//设置新人员的数据
strcpy(指针->名称,名称);
指针->年龄=年龄;
指针->下一步=人;
//如果当前列表为空
if(people==NULL)
{
//将新人的“下一步”链接设置为指向当前列表
指针->下一步=人;
//返回指向新用户的指针
返回指针;
}
其他的
{
for(int i=0;i<多少;i++)
{
人=人->下一步;
如果(人员->下一步==NULL)返回人员;
}//为了
}//否则
返回指针;
}
如果你需要完整的课程,请告诉我

谢谢大家!


莎拉:)

在将元素插入到列表中之前*请检查元素的位置是否正确。 试试这个:

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;
}
struct person*insert\u排序(struct person*people,char*name,int-age){
//为新人创建一个新空间
struct person*pointer=malloc(sizeof(struct person));
//检查它是否成功
if(指针==NULL)
{ 
printf(“程序无法分配内存”);
出口(-1);
}
//设置新人员的数据
strcpy(指针->名称,名称);
指针->年龄=年龄;
struct person*cursor=人;
struct person*previous=人;
if(people==NULL){
指针->下一步=空;
返回指针;
}
while(cursor!=NULL&&strcmp(指针->名称,光标->名称)下一步;
}
如果(上一个!=NULL)
上一步->下一步=指针;
指针->下一步=光标;
还人,;
}

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

除了正确命名、消除多余注释并通过
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;
}
struct person*insert\u排序(struct person*people,char*name,int-age)
{
struct person*newperson=malloc(sizeof*newperson);
if(newperson==NULL){
printf(“程序无法分配内存”);
出口(-1);
}
strcpy(newperson->name,name);
新人->年龄=年龄;
newperson->next=人;
if(people==NULL){
newperson->next=人;
返回新人;
}
for(int i=0;i<多少;i++){
人=人->下一步;
如果(人员->下一步==NULL)
还人,;
}
返回新人;
}
有几件事马上就出现了:

  • 现在还不清楚您是否正在初始化所有的
    *newperson
    。最安全的是
    newperson=calloc(1,sizeof*newperson)
    ,这样读者就不用考虑它了,结果总是很好的

  • 您没有显示结构定义,但没有检查入站名称是否适合newperson->name的存储区——如果newperson->name是指针,则根本没有为其分配任何名称

  • 《newperson->next》有一个多余的任务,提出了一个相反的问题,即是否有一些不明显的或可能缺失的东西使其成为必要

  • 我看不出你在哪里比较键值


不清楚你在问什么。有什么问题吗?嗨,帕迪,很抱歉我说得不够清楚。我的问题是我不确定如何实现我的算法(第一框)作为C代码。第二个框是我尝试进行插入排序,但它不正确。嗯……这似乎给了我一个分段错误:/问题似乎是上一个->下一个=指针;另外,我调用插入排序方法如下:for(I=0;I下一个=指针;)造成的分段错误真的很奇怪:/previous->下一个之前需要进行检查,以防元素设置在列表的开头;-)立即尝试。是的!:D分段错误消失了。现在唯一的问题是,出于某种原因,它跳过了一个值(在我的例子中是第三个)。你知道它为什么这样做吗?