C语言链表的一个排序问题

来源:百度知道 编辑:UC知道 时间:2024/05/29 23:24:00
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<stdlib.h>
struct student
{

char Number[10];//学号
char Name[10];//姓名
int Cc;//Computer Architecture 计算机结构
int Os;//operating system 操作系统
int Se;//Software Engineering 软件工程
int Sum;//总分
};
struct LinkList
{
struct student User;
LinkList *pNext;
};
//上面是我定义的结构还有链表,下面是我在主函数里需要调用的排序方法
LinkList *com(LinkList *pHead)//按成绩排序
{
LinkList *pTemp=NULL,
*p1=pHead,
*p2=pHead->pNext;
while(p1->pNext!=pTemp)
{
while(p2->pNext!=pTemp)
{
if(p1->pNext->User.Sum<p2->pNext->User.Sum)
{
p2->pNext=p1->pNext;
p1->pNext->pNext=p1->pNext;
p1=p2->pNext->pNext;
}
p1=p1->pNext;
p2=p2->pNext;
}
pTemp=p2;
p1=pHead;
p2=p1->pN

排序代码不对
p2->pNext=p1->pNext;
p1->pNext->pNext=p1->pNext;
p1=p2->pNext->pNext;
不知道你在写些什么,前面两句的意思都是一样的。

下面是修改后的代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <stdlib.h>

struct student
{

char Number[10];
char Name[10];
int Cc;//Computer Architecture 计算机结构
int Os;//operating system 操作系统
int Se;//Software Engineering 软件工程
int Sum;//总分
};
struct LinkList
{
struct student User;

LinkList *pNext;
};
int Users=0; //学生个数
//上面是我定义的结构还有链表,下面是我在主函数里需要调用的排序函数
LinkList *com(LinkList *pHead)//按成绩排序
{
LinkList *pFore=NULL, *p1, *p2,*pTemp;
int i,j;
for(i=0;i<Users-1;i++)
for(pFore=p1=pHead,j=0;j<Users-i-1&&p1->pNext!=NULL;pFore=p1,p1=p1->pNext,j++){

if(p1-