链表的创建
1、链表为空时:不用排序;
2、链表中有一个节点:不用排序;
3、链表中两个及其以上节点时:排序。
结构体节点:
typedef struct student {
int num; //学号
int score; //分数
char name[20];
struct student *next;//指针域
}STU;
demo:
void link_order(STU *p_head)
{
STU *pb, *pf, temp;
pf = p_head;
if(p_head == NULL) {//链表为空
printf("needn't order.\n");
return ;
}
if(p_head->next == NULL) {//链表有1个节点
printf("only one print, needn't order.\n");
return ;
}
while(pf->next != NULL) {//以pf指向的节点为基准节点
pb = pf->next;//pb从基准点的下一个节点开始
while(pb != NULL) {
if(pf->num > pb->num) {
temp = *pf;
*pf = *pb;
*pb = temp;
temp.next = pf->next;
pf->next = pb->next;
pb->next = temp.next;
}
pb = pb->next;
}
pf = pf->next;
}
return ;
}