C插入排序-实现
我刚开始用C语言编程,需要一些帮助来实现插入排序 我正在进行C列表插入排序。 这是它的伪代码,我想把它转换成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
使用循环查找列表中应在新人之前的最后一项 将新人的“下一步”链接设置为指向此列表项后面的内容 设置此项目的“下一步”链接以指向新用户 返回(列表的开头)列表 这是我的伪代码的部分实现
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》有一个多余的任务,提出了一个相反的问题,即是否有一些不明显的或可能缺失的东西使其成为必要
- 我看不出你在哪里比较键值