#include<stdio.h>
#include<malloc.h>
#define OK 1 //执行成功
#define ERROR 0 //执行出错
#define MAXSIZE 100
typedef int ElemType;
typedef int Status;
typedef struct
{
ElemType *dataspace; //存储空间的首地址
int len; //标记存储的元素个数
//1.存储数据元素
//2.表达数据元素之间的逻辑关系
} LinearList;
void ElemInput(ElemType *e) //输入
{
scanf("%d",e);
};
void ElemOutput(ElemType e) //输出
{
printf("%d",e);
};
//1.初始化线性表,申请空间
Status InitList(LinearList *Lp)
{
Lp->dataspace=(ElemType *)malloc(sizeof(ElemType)*MAXSIZE) ;
if(Lp->dataspace == NULL)
return ERROR;
Lp->len=0;
return OK;
}
//2.销毁线性表,释放空间
Status DestoryList(LinearList *Lp)
{
if(Lp->dataspace == NULL)
return ERROR;
free(Lp->dataspace );
Lp->dataspace = NULL;
Lp->len = 0;
return OK;
}
//3.取线性表的长度
Status GetLength(LinearList L,int *lenp)
{
if(L.dataspace == NULL)
return ERROR;
*lenp = L.len;
return OK;
}
//4.判断线性表是否为空
Status IsEmpty(LinearList L,int *emptyp)
{
if(L.dataspace == NULL)
return ERROR;
*emptyp = (L.len == 0);
return OK;
}
//5.指定位置插入元素
Status Insert(LinearList *Lp,int i,ElemType e)
{
int index = i-1,p=Lp->len -1;
if(Lp->dataspace == NULL || (index<0 || index>Lp->len))
return ERROR;
for(;p>=index;p--)
Lp->dataspace[p+1] = Lp->dataspace[p];
Lp->dataspace[index]=e;
Lp->len++;
return OK;
}
//6.删除指定位置元素
Status Delete(LinearList *Lp,int i,ElemType *ep)
{
int index = i-1,p=i;
if(Lp->dataspace == NULL || (index<0 || index>=Lp->len))
return ERROR;
*ep = Lp->dataspace[index];
for(;p<Lp->len;p++)
Lp->dataspace[p-1] = Lp->dataspace[p];
Lp->len--;
return OK;
}
//7.取指定位置元素值
Status GetData(LinearList L,int i,ElemType *ep)
{
int index = i-1;
if(L.dataspace == NULL || (index<0 || index>=L.len))
return ERROR;
*ep = L.dataspace [index];
return OK;
}
//8.查询指定元素值在表中的位置
Status Locate(LinearList L,ElemType e,int *locp)
{
int i;
if(L.dataspace == NULL)
return ERROR;
for(i=0;i<L.len;i++)
if(L.dataspace[i]==e)
{
*locp=i+1;
return OK;
}
*locp=0;
return OK;
}
//9.依次输出线性表中所有数据元素
Status Print(LinearList L,char split)
{
int i;
if(L.dataspace == NULL)
return ERROR;
for(i=0;i<L.len;i++)
{
ElemOutput(L.dataspace[i]);
printf("%c",split);
}
printf("\n");
return OK;
}
int main()
{
int order,i,len,empty;
LinearList L;
ElemType e;
do
{
printf("====线性表的基本操作演示程序====\n");
printf("==1.初始化创建线性表============\n");
printf("==2.销毁线性表==================\n");
printf("==3.查询线性表的长度============\n");
printf("==4.判断线性表是否为空==========\n");
printf("==5.执行元素插入操作============\n");
printf("==6.执行元素删除操作============\n");
printf("==7.查询指定位置元素============\n");
printf("==8.查询指定元素位置============\n");
printf("==9.依次输出所有的数据元素======\n");
printf("================================\n");
printf("请输入操作指令序号:");
scanf("%d",&order);
switch(order)
{
case 1:
if(InitList(&L)==OK)
printf("操作成功,线性表已被初始化\n");
else
printf("线性表初始化失败,空间不足\n");
break;
case 2:
if(DestoryList(&L)==OK)
printf("操作成功,所占空间已被全部释放\n");
else
printf("操作失败,线性表未被初始化\n");
break;
case 3:
if(GetLength(L,&len)==OK)
printf("当前线性表长度为:%d\n",len);
else
printf("操作失败,线性表未被初始化\n");
break;
case 4:
if(IsEmpty(L,&empty)==ERROR)
printf("操作失败,线性表未被初始化\n");
else if(empty)
printf("当前线性表为空\n");
else
printf("当前线性表不为空\n");
break;
case 5:
printf("请依次输入插入位置及元素值(空格隔开):");
scanf("%d",&i);
ElemInput(&e);
if(Insert(&L,i,e)==OK)
printf("元素插入成功\n");
else
printf("元素插入失败\n");
Print(L,' ');
break;
case 6:
printf("请输入要删除的元素位置:");
scanf("%d",&i);
if(Delete(&L,i,&e)==OK)
printf("元素删除成功\n",e);
else
printf("元素删除失败\n");
Print(L,' ');
break;
case 7:
printf("请输入要查询的元素位置:");
scanf("%d",&i);
if(GetData(L,i,&e)==OK)
printf("查询成功,该元素为:%d\n",e);
else
printf("元素查询失败\n");
break;
case 8:
printf("请输入要查询的元素值:");
ElemInput(&e);
if(Locate(L,e,&i)==ERROR || i<1)
printf("元素查询失败\n");
else
printf("查询成功,该元素所在位置为:%d\n",i);
break;
case 9:
if(Print(L,' ')==ERROR)
printf("打印成功\n");
break;
}
printf("\n\n");
}while(order>0);
return 0;
}
DS||线性表顺序存储结构
最新推荐文章于 2020-07-25 18:45:24 发布