typedef struct Student
{
long num;
char name[10];
char sex[4];
int age;
int score;
}StudentType; //命名结构体名
#define Maxsize 100 //宏定义
typedef StudentType DataType; //定义DataType为StudentType
typedef struct
{
DataType List[Maxsize]; //存放数据的数组成员
int size; //记录数组当前个数的成员
}SeqList; //命名结构体名
void ListInitiate(SeqList *L) //初始化函数
{
L -> size = 0; //定义初始数据元素个数
}
int ListLength(SeqList L)
{
return L.size; //求数组当前元素个数
}
int ListInsert(SeqList *L,int i,DataType x)
//在位置参数i(0=<i<=size)前插入数据元素x函数
//插入成功时函数返回1,否则返回0
{
int j;
if(L -> size >= Maxsize) //程序健壮性
{
printf("数组已满,无法插入!\n");
return 0;
}
else if(i < 0||i > L->size)
{
printf("位置参数i不合法!\n");
return 0;
}
else //插入数据元素
{
for(j = L->size;j > i;j--)
{
L -> List[j] = L -> List[j-1]; //位置参数i后的元素向后移一位
}
L -> List[i] = x;
L -> size++; //当前元素个数加1
return 1;
}
}
int ListGet(SeqList L,int i,DataType *x)
//取数组第i位置的函数
//取到的数组的值传入x中,函数返回1时成功取出,返回0时取出失败
{
if(i < 0||i > L.size-1) //程序健壮性
{
printf("位置参数i不合法!\n");
return 0;
}
else
{
*x = L.List[i]; //取出的值放入x的地址中
return 1;
}
}
void ListContrast(StudentType *L,int n)
{
int i,j,temp;
int a[] = {L -> score};
for(i = 0;i < n;i++)
{
for(j = 0;j < n-1-i;j++)
{
/*if(L->score[j-1] < L->score[j])
{
temp = L->score[j];
L->score[j] = L->score[j-1];
L->score[j-1] = temp;
}*/
if(a[j-1] < a[j])
{
temp = a[j];
a[j] =a[j-1];
a[j-1] = temp;
}
}
}
}
#include<stdio.h>
#include"SeqList.h"
void main(void)
{
SeqList myList;
int i,k;
StudentType str;
StudentType x[3] = {{2000001,"张三","男",20,66},
{2000002,"李四","男",21,88},
{2000003,"王五","女",22,99}};
ListInitiate(&myList); //调用初始化函数
ListContrast(&str,3);
for(i = 0;i < 3;i++)
{
if(ListInsert(&myList,i,x[i]) == 0) //调用插入函数
{
printf("error!\n");
return;
}
}
k = ListLength(myList); //调用求当前长度函数
for(i =0;i < k;i++)
{
if(ListGet(myList,i,&str) == 0) //调用取值函数
{
printf("error!\n");
return;
}
else
{
printf("%d %s %s %d\n",str.num,str.name,str.sex,str.age);//输出数据
}
}
getchar();
getchar();
}