C++单向链表 排序

来源:百度知道 编辑:UC知道 时间:2024/06/22 17:31:00
struct student
{
long num;
float score;
student *next;
};
最好有详细讲解

/*按照那个排序,要求讲的不清楚,不过也写了一个就是分别按照学号升序与成绩升序排序*/
#include<iostream>
using namespace std;

#define OK 1
#define ERROR 0

typedef long LElemType; //学号变量类型
typedef float FElemType; //成绩变量类型

typedef int Status; //返回值的类型

typedef struct student
{
LElemType num;
FElemType score;
student *next;

}node;

typedef struct LIST
{
node *head,*tail;

}link;

node *MakeNode(long num, float score) //生成存放学生信息的节点
{
node *p=(node *)malloc(sizeof(node));
p->num = num; //存放学号
p->score = score; //存放分数
p->next = NULL; //刚生成的节点下一个为空
return p;
}

Status Create(link *s,int n)
{
long num;
float score;
s->head = s->tail = MakeNode(0,-1); //头结点num用来存放学生信息条数。score空着不用
if(NULL == s->head)
{
cout<<"分配失败!"<<endl;
return ERROR;