单链表的排序不建议你这么排序,直接做插入排序就行了,你在申请节点的时候,就要考虑到插入到中间的位置 或者最后的位置,这样的执行效率高
void insert_func(void)
{
char s_temp[4];
ptr=(struct student*)malloc(sizeof(struct student));
printf("Student name:");
gets(ptr->name);
printf("Student score:");
gets(s_temp);
ptr->score=atoi(s_temp);
sort_func();
}
void sort_func(void)
{
prev=head;
thiss=head->next;
while ((thiss!=NULL) && (thiss->score>ptr->score))
{
prev=thiss;
thiss=thiss->next;
}
ptr->next=thiss;
prev->next=ptr;
}
void delete_func(void)
{
char del_name[20];
printf("Delete student name:");
gets(del_name);
prev=head;
thiss=head->next;
while ((thiss!=NULL) && (strcmp(thiss->name,del_name)!=0))
{
prev=thiss;
thiss=thiss->next;
}
if (thiss!=NULL)
{
prev->next=thiss->next;
free(thiss);
printf("%s student record deleted\n",del_name);
}
else
{
printf("Student %s not found\n",del_name);
}
anykey_func();
}