线性表两种存储形式
#define MAX 100
typedef struct
{
ElemType data[MAX];//用数组做数据
int listSize;
int length;
}SqList;
typedef struct
{
ElemType *data;//指向一个数据的指针,操作更具灵活性
int listSize;
int length;
}SqList;
下面是一个完整的程序,为了便于理解,不至于太抽象,书上讲的ADT(抽象数据类型),这里我们把它具体化了
//假设有学生组数据,用线性表存储
#include<stdio.h>
#include<string.h>
#include<stdlib.h>//犯了一个比较低级的错误,没有引入标准库函数,导致用不了malloc
typedef struct
{
char name[20];
float score;
}STD;
typedef struct
{
STD *data;
int ListSize; //总容量
int length; //当前长度
}SqList;
//初始化线性表,
//入口参数:线性表和容量,操作成功返回1
int initSqList(SqList *L,int max)
{
L->data=(STD*)malloc(max*sizeof(STD));//可以看出一次性申请了一大块存储空间,不具有动态增删特性
if(L->data==NULL)
{
printf("空间申请失败!\n");
return 0;
}
L->ListSize=max;
L->length=0;
return 1;
}
//插入操作,在第i个插入数据,
//参数:线性表、要插入的位置,要插入的数据,操作成功返回1
int insertSqList(SqList *L,int i,STD x)
{
int k;
if(i<1||i>L->length+1) //线性表是从第一个数起的,等于零不行,可以插在末尾即length+1
{
printf("Insert location error!\n");
return 0;
}
if(L->length==L->ListSize)
{
printf("undercapacity!\n");
return 0;
}
for(k=L->length;k>=i;k--) //数组从0数,一直到length-1
L->data[k]=L->data[k-1];//从第i个数据开始整体往后移,因为我要插在第i-1这个位置
L->data[i-1]=x; //腾出来的位置让要插入的数占据,数组下标会比真实位置小1,这个地方为什么不能统一呢?
L->length=L->length+1;
return 1;
}
//删除操作
//入口参数:线性表,删除位置,删除数据,操作成功返回1
int deleteSqList(SqList *L,int i,STD *x)
{
int k;
if(L->length==0)
{
printf("There is no data to delete!\n");
return 0;
}
if(i<1||i>L->length)
{
printf("Delete location error!\n");
return 0;
}
*x=L->data[i-1]; //得到要删除的数据
for(k=i;k<L->length;k++) //后面的数向前挪一位,把i位置覆盖,就删除了
L->data[k-1]=L->data[k];
L->length=L->length-1;
return 1;
}
//更新操作
//入口参数线性表,要修改的位置,修改后的数,操作成功返回1
int modifySqList(SqList *L,int i,STD x)
{
if(i<1||i>L->length) //线性表范围1~length
{
printf("Insert location error!\n");
return 0;
}
if(L->length==0)
{
printf("There is no data to motify!\n");
return 0;
}
L->data[i-1]=x; //将对应位置修改就行了
return 1;
}
//获取操作
//入口参数:线性表,获取位置,接收的数
int GetElem(SqList L,int i,STD *x)
{
if(i<1||i>L.length) //线性表范围1~length
{
printf("Get location error!\n");
return 0;
}
if(L.length==0)
{
printf("There is no data to motify!\n");
return 0;
}
*x=L.data[i-1]; //将对应位置修改就行了
return 1;
}
//定位操作
int locationSqList(SqList L,char *x)
{
int i;
if(L.length==0)
{
printf("there is no data!\n");
return 0;
}
for(i=0;i<L.length;i++)
if(strcmp(L.data[i].name,x)==0)
return i+1; //找到就返回位置
return 0; //找不到就返回0
}
//遍历操作
int dispSqList(SqList L)
{
int i;
if(L.length==0)
{
printf("There is no data!\n");
return 0;
}
for(i=0;i<L.length;i++)
printf("%10s%7.2f\n",L.data[i].name,L.data[i].score);
return 1;
}
//组合操作,可以完成多功能用途,例如创建链表
void creatSqList(SqList *L,int maxsize)
{
int n=0;
STD x;
char yn;
initSqList(L,maxsize);
do
{
printf("Please input the student's name and score:\n");
scanf("%s,%f",x.name,x.score);//这里%s,%f要用逗号隔开,不然会产生异常
getchar(); // 为什么要用getchar()
insertSqList(L,n+1,x);
printf("Continue inputing? Y/N?");
yn=getchar();
}while(yn=='Y'||yn=='y');
}
//用于测试的主函数
void main()
{
SqList *L;
STD x1,x2,x3,get,dele;
strcpy(x1.name,"teenway");
x1.score=89.43;
strcpy(x2.name,"zhanghu");
x2.score=89.54;
strcpy(x3.name,"zhangxiaotian");
x3.score=94.32;
L=(SqList*)malloc(sizeof(SqList));//注意,这里使用sl使用之前务必申请一个空间才能用
initSqList(L,3); //初始化操作正常
insertSqList(L,1,x1);
insertSqList(L,2,x2);
insertSqList(L,3,x3);// 插入操作测试正常
printf("teenway at %d\n",locationSqList(*L,"teenway"));//定位操作测试正常
GetElem(*L,2,&get);
printf("%10s,%7.2f\n\n",get.name,get.score);//获取操作正常
dispSqList(*L); //遍历操作正常
printf("\n");
modifySqList(L,1,get);//修改操作正常
dispSqList(*L);
printf("\n");
deleteSqList(L,1,&dele);//删除操作正常
dispSqList(*L);
}
//评注:到这里线性表就算讲完了,有了这些操作之后可以综合起来做一个管理系统
```c