c语言链表学生信息管理系统,使用的是冒泡排序,但是执行顺序不同,结果就不同
structStudent*sort1(structStudent*head){structStudent*endpt,*p,*p1,*p2;p1=(structStud...
struct Student *sort1(struct Student *head)
{
struct Student *endpt, *p, *p1, *p2;
p1 = (struct Student *)malloc(LEN);
p1->next = head;
head = p1;
for (endpt = NULL; endpt != head; endpt = p)
{
for (p = p1 = head; p1->next->next != endpt; p1 = p1->next)
{
if (p1->next->all<p1->next->next->all)
{
p2 = p1->next->next;
p1->next->next = p2->next;
p2->next = p1->next;
p1->next = p2;
p = p1->next->next;
}
}
}
p1 = head;
head = head->next;
free(p1);
p1 = NULL;
return head;
}<!--< 展开
{
struct Student *endpt, *p, *p1, *p2;
p1 = (struct Student *)malloc(LEN);
p1->next = head;
head = p1;
for (endpt = NULL; endpt != head; endpt = p)
{
for (p = p1 = head; p1->next->next != endpt; p1 = p1->next)
{
if (p1->next->all<p1->next->next->all)
{
p2 = p1->next->next;
p1->next->next = p2->next;
p2->next = p1->next;
p1->next = p2;
p = p1->next->next;
}
}
}
p1 = head;
head = head->next;
free(p1);
p1 = NULL;
return head;
}<!--< 展开
展开全部
struct Student *sort1(struct Student *head) {
struct Student *p, *pt, *q;
p = (struct Student *)malloc(LEN); // 临时头结点
p->next = head; // 添加到链表的首部
head = p; // 保存链表的头结点地址
p = head; // 为排序做准备
while(p->next != NULL) {
q = p->next;
while(q->next != NULL) {
if(p->next->all < q->next->all) {
pt = p->next;
p->next = q->next;
q->next = p->next->next;
p->next->next = pt;
}
else q = q->next;
}
p = p->next;
}
p = head;
head = head->next;
free(p);
p = NULL;
return head;
}
追问
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询