printf("%d",Element[j]);
}
voidSetNull_SeqList(int*Length_pointer)
{*Length_pointer=0;
}
voidmain()
{intx,i,loca;
intElement[MAXSIZE];/*存储线性表的数据元素*/
intLength;/*存储线性表的长度*/
Init_SeqList(&Length);/*构造一个空表*/
do
{printf("\n");
printf("1---插入一个元素(Insert)\n");
printf("2---查询一个元素(Locate)\n");
printf("3---删除一个元素(Delete)\n");
printf("4---显示所有元素(Show)\n");
printf("5---退出\n");
scanf("%d",&i);
switch(i)
{case1:
printf("请输入要插入的分数:
");
scanf("%d",&x);
if(Insert_Last(Element,&Length,x)!
=OK)
printf("插入失败\n");
break;
case2:
printf("请输入你要查询的分数\n");
scanf("%d",&x);
loca=Location_SeqList(Element,Length,x);
if(loca!
=-1)
printf("查找成功!
存储的位置:
%d",loca);
elseprintf("查找失败!
");
break;
case3:
printf("请输入要删除的分数:
");
scanf("%d",&x);
loca=Location_SeqList(Element,Length,x);
if(loca!
=-1)
if(Delete_SeqList(Element,&Length,loca)!
=OK)
printf("删除失败\n");
break;
case4:
Show_SeqList(Element,Length);break;
case5:
break;
default:
printf("错误选择!
请重选:
");break;
}
}while(i!
=5);
SetNull_SeqList(&Length);
}
2.3.2线性表使用结构体的算法实现
#include"stdio.h"
#defineMaxSize20
#defineOverFlow-1
#defineOK1
#defineError-1
typedefintElemType;
typedefstruct/*数据定义*/
{ElemTypeElement[MaxSize];
intLength;/*线性表的长度*/
}SeqList;
voidInit_SeqList(SeqList*L_pointer)/*构造一个空表*/
{L_pointer->Length=0;
}
intInsert_Last(SeqList*L_pointer,intx)/*插入一个元素(尾插)*/
{if(L_pointer->Length==MaxSize-1)
{printf("表满");
returnOverFlow;
}
else
{L_pointer->Element[L_pointer->Length]=x;/*表尾插入一个元素*/
(L_pointer->Length)++;/*线性表长度加1*/
returnOK;/*插入成功,返回OK*/
}
}
intLocation_SeqList(SeqList*L_pointer,intx)/*查找指定元素*/
{inti=0;
while(iLength&&L_pointer->Element[i]!
=x)
i++;
if(i==L_pointer->Length)/*查找失败*/
return-1;
elsereturni+1;/*查找成功,返回x的逻辑位置*/
}
intDelete_SeqList(SeqList*L_pointer,inti)/*删除指定元素*/
{intj;
if(i<1||i>L_pointer->Length)
{printf("不存在第i个元素");
returnError;
}
for(j=i-1;j<=L_pointer->Length-1;j++)
L_pointer->Element[j]=L_pointer->Element[j+1];/*数据向左移动*/
(L_pointer->Length)--;/*长度减1*/
returnOK;
}
voidShow_SeqList(SeqList*L_pointer)/*遍历线性表*/
{intj;
printf("\n");
if(L_pointer->Length==0)
printf("空表(NULL)!
\n");
else
for(j=0;jLength;j++)/*依次输出显示*/
printf("%d",L_pointer->Element[j]);
}
voidSetNull_SeqList(SeqList*L_pointer)/*清空线性表*/
{L_pointer->Length=0;
}
voidmain()
{intx,i,loca;
SeqListList;
Init_SeqList(&List);/*构造一个空表*/
do
{printf("\n");
printf("1---插入一个元素(Insert)\n");
printf("2---查询一个元素(Locate)\n");
printf("3---删除一个元素(Delete)\n");
printf("4---显示所有元素(Show)\n");
printf("5---退出\n");
scanf("%d",&i);
switch(i)
{case1:
printf("请输入要插入的分数:
");
scanf("%d",&x);
if(Insert_Last(&List,x)!
=OK)
printf("插入失败\n");
break;
case2:
printf("请输入你要查询的分数\n");
scanf("%d",&x);
loca=Location_SeqList(&List,x);
if(loca!
=-1)
printf("查找成功!
存储的位置:
%d",loca);
elseprintf("查找失败!
");
break;
case3:
printf("请输入要删除的分数:
");
scanf("%d",&x);
loca=Location_SeqList(&List,x);
if(loca!
=-1)
if(Delete_SeqList(&List,loca)!
=OK)
printf("删除失败\n");
break;
case4:
Show_SeqList(&List);break;
case5:
break;
default:
printf("错误选择!
请重选:
");break;
}
}while(i!
=5);
SetNull_SeqList(&List);/*清空线性表*/
}
程序2.3
#include"stdio.h"
#include"string.h"
#defineMaxSize20
#defineOverFlow-1
#defineOK1
#defineError-1
typedefcharElemType[10];
typedefstruct/*数据定义*/
{ElemTypeElement[MaxSize];
intLength;/*线性表的长度*/
}SeqList;
voidInit_SeqList(SeqList*L_pointer)/*构造一个空表*/
{L_pointer->Length=0;
}
intInsert_Last(SeqList*L_pointer,ElemTypex)/*插入一个元素(尾插)*/
{if(L_pointer->Length==MaxSize-1)
{printf("表满");
returnOverFlow;
}
else
{strcpy(L_pointer->Element[L_pointer->Length],x);/*表尾插入一个元素*/
(L_pointer->Length)++;/*线性表长度加1*/
returnOK;/*插入成功,返回OK*/
}
}
intLocation_SeqList(SeqList*L_pointer,ElemTypex)/*查找指定元素*/
{inti=0;
while(iLength&&strcmp(L_pointer->Element[i],x)!
=0)
i++;
if(i==L_pointer->Length)/*查找失败*/
return-1;
elsereturni+1;/*查找成功,返回x的逻辑位置*/
}
intDelete_SeqList(SeqList*L_pointer,inti)/*删除指定元素*/
{intj;
if(i<1||i>L_pointer->Length)
{printf("不存在第i个元素");
returnError;
}
for(j=i-1;j<=L_pointer->Length-1;j++)
strcpy(L_pointer->Element[j],L_pointer->Element[j+1]);/*数据向左移动*/
(L_pointer->Length)--;/*长度减1*/
returnOK;
}
voidShow_SeqList(SeqList*L_pointer)/*遍历线性表*/
{intj;
printf("\n");
if(L_pointer->Length==0)
printf("空表(NULL)!
\n");
else
for(j=0;jLength;j++)/*依次输出显示*/
puts(L_pointer->Element[j]);
}
voidSetNull_SeqList(SeqList*L_pointer)/*清空线性表*/
{L_pointer->Length=0;
}
voidmain()
{ElemTypex;
inti,loca;
SeqListList;
Init_SeqList(&List);/*构造一个空表*/
do
{printf("\n");
printf("1---插入一个元素(Insert)\n");
printf("2---查询一个元素(Find)\n");
printf("3---删除一个元素(Delete)\n");
printf("4---显示所有元素(Show)\n");
printf("5---退出\n");
scanf("%d",&i);
switch(i)
{case1:
printf("请输入要插入的名子:
");
scanf("%s",x);
if(Insert_Last(&List,x)!
=OK)
printf("插入失败\n");
break;
case2:
printf("请输入你要查询的名子\n");
scanf("%s",x);
loca=Location_SeqList(&List,x);
if(loca!
=-1)
printf("查找成功!
存储的位置:
%d",loca);
elseprintf("查找失败!
");
break;
case3:
printf("请输入要删除的名子:
");
scanf("%s",x);
loca=Location_SeqList(&List,x);
if(loca!
=-1)
if(Delete_SeqList(&List,loca)!
=OK)
printf("删除失败\n");
break;
case4:
Show_SeqList(&List);break;
case5:
break;
default:
printf("错误选择!
请重选:
");break;
}
}while(i!
=5);
SetNull_SeqList(&List);/*清空线性表*/
}
程序2.13顺序查找
#include"stdio.h"
#defineMaxSize