线性表顺序存储的存储表示、初始化、插入、取值、查找、删除
存储表示
typedef struct{
ElemType *elem;//存储空间的基地址,ElemType是可定义的数据类型
int length;//当前长度
}Sqlist;//顺序表的结构类型为Sqlist
初始化
//初始化
Status InitList(Sqlist &L)
{//初始化线性表,构造一个空的顺序表
L.elem=new ElemType[MAXSIZE];//为顺序表分配一个大小为MAXSIZE的数组空间
if(!L.elem) exit(OVERFLOW);//存储分配失败退出
L.length=0;//将表的初始长度置为0
return OK;
}
插入
//插入
Status ListInsert(Sqlist &L,int i,ElemType e)
{//在顺序表L中第i个位置插入新的元素e,i值的合法范围是1<<i<<L.length+1
if((i<1)||(i>L.length+1)) return ERROR;
//如果插入的位置小于1或者大于长度+1
//(+1的原因是在顺序表的最后也可以插入元素),则返回ERROR
if(L.length==MAXSIZE) return OVERFLOW;//当前存储空间已满
int j;//j为数组元素的下标
for(j=L.length-1;j>=i-1;j--)
{
L.elem[j+1]=L.elem[j];//插入位置及之后的元素后移
}
L.elem[i-1]=e;//将新元素e放进第i个位置.下标为i-1
++L.length;//表长+1
return OK;
}
取值
//取值
Status GetElem(Sqlist L,int i,ElemType &e)
{
if((i<1)||(i>L.length)) return ERROR;//判断i是否合理,如果不合理则返回ERROR
e=L.elem[i-1];//elem[i-1]储存第i个元素
return OK;
}
查找
//查找
Status LocateElem(Sqlist L,ElemType e)
{
for(int i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;//在线性表中查找,返回元素序号,若找不到返回0
return 0;
}
删除
//删除
Status ListDelete(Sqlist &L,int i)
{//在线性表中删除第i个元素,i的合法范围是1<<i<<L.length
if((i<1)||(i>L.length)) return ERROR;//i值不合法
for(int j=i;j<=L.length-1;j++)//j代表下标
L.elem[j-1]=L.elem[j];
--L.length;//表长-1
return OK;
}
总代码
#include<iostream>
//iostream是指iostream库,意思是输入输出流,i(in)、o(out)、stream(流)
using namespace std;//调用命名空间std
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int ElemType;//ElemType是可定义的数据类型
typedef int Status;//Status是函数返回值类型
//存储表示
typedef struct{
ElemType *elem;
int length;
}Sqlist;
//初始化
Status InitList(Sqlist &L)
{//初始化线性表,构造一个空的顺序表
L.elem=new ElemType[MAXSIZE];//为顺序表分配一个大小为MAXSIZE的数组空间
if(!L.elem) exit(OVERFLOW);//存储分配失败退出
L.length=0;//将表的初始长度置为0
return OK;
}
//插入
Status ListInsert(Sqlist &L,int i,ElemType e)
{//在顺序表L中第i个位置插入新的元素e,i值的合法范围是1<<i<<L.length+1
if((i<1)||(i>L.length+1)) return ERROR;
//如果插入的位置小于1或者大于长度+1
//(+1的原因是在顺序表的最后也可以插入元素),则返回ERROR
if(L.length==MAXSIZE) return OVERFLOW;//当前存储空间已满
int j;//j为数组元素的下标
for(j=L.length-1;j>=i-1;j--)
{
L.elem[j+1]=L.elem[j];//插入位置及之后的元素后移
}
L.elem[i-1]=e;//将新元素e放进第i个位置.下标为i-1
++L.length;//表长+1
return OK;
}
//取值
Status GetElem(Sqlist L,int i,ElemType &e)
{
if((i<1)||(i>L.length)) return ERROR;//判断i是否合理,如果不合理则返回ERROR
e=L.elem[i-1];//elem[i-1]储存第i个元素
return OK;
}
//查找
Status LocateElem(Sqlist L,ElemType e)
{
for(int i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;//在线性表中查找,返回元素序号,若找不到返回0
return 0;
}
//删除
Status ListDelete(Sqlist &L,int i)
{//在线性表中删除第i个元素,i的合法范围是1<<i<<L.length
if((i<1)||(i>L.length)) return ERROR;//i值不合法
for(int j=i;j<=L.length-1;j++)//j代表下标
L.elem[j-1]=L.elem[j];
--L.length;//表长-1
return OK;
}
int main()
{
Sqlist Lscore; //用来存储学生成绩
InitList(Lscore); //初始化线性表
//线性表的初始化
int i,s;
cout<<"输入5个学生成绩,添加到线性表中:"<<endl;
for(i=1; i<=5; i++)
{//输入5个学生成绩,添加到线性表中
cin>>s;
ListInsert(Lscore,i,s); //线性表中添加数据
}
//线性表的插入
int a,s1;
cout<<"请输入要插入的位置和成绩:"<<endl;
cin>>a>>s1; //输入要插入的位置和成绩
ListInsert(Lscore,a,s1);
int len=Lscore.length;
cout<<"输出插入后的成绩:"<<endl;
for(i=1;i<=len;i++)
{
GetElem(Lscore,i,s); //线性表的取值
cout<<s<<endl;//输出成绩
}
//线性表的查找
int ls;
cout<<"请输入要查找的成绩:"<<endl;
cin>>ls; //输入要查找的成绩
//在线性表中查找,返回元素序号,若找不到返回0
int loc=LocateElem(Lscore,ls);
if(loc==0) cout<<"Not found!"<<endl;
else cout<<"Loc:"<<loc<<endl;
//线性表的删除
int l;
cout<<"请输入要删除的位置:"<<endl;
cin>>l; //输入要删除的位置
if(ListDelete(Lscore,l)==OK) cout<<"delete success!"<<endl;
else cout<<"Error!"<<endl;
for(i=1; i<=Lscore.length;i++) //遍历线性表
{
int ele;
GetElem(Lscore,i,ele);
cout<<ele<<endl;
}
return 0;
}