学习目录:
学习内容:
1.初始化
(1)定义:构造一个空的顺序表
(2)算法步骤:
- 为顺序表L动态分配一个预定义大小的数组空间,使elem指向这段空间的基地址
- 将表的当前长度设置为0
(3)算法描述:
Status InitList(SqList &L)
{
L.elem=new ElemType[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK;
}
2.取值
(1)定义:根据指定的位置序号i,获取顺序表中第i个数据元素的值。由于顺序存储结构具有随机存取的特点,可以直接通过数组下标定位得到,elem[i-1]单元存储第i个数据元素。
(2)算法步骤:
- 判断指定的位置序号i值是否合理(1<=i<=L.length),若不合理,则返回ERROR。
- 若i值合理,则将第i个数据元素L.elem[i-1]赋给参数e,通过e返回第i个数据元素的传值。
(3)算法描述:
Status GetElem(SqList L,int i,ElemType &e)
{
if(i<1||i>L.length) return ERROR;
e=L.elem[i-1];
return OK;
}
3.查找
(1)定义:根据指定的元素值e,查找顺序表中第1个值与e相等的元素。若查找成功,则返回该元素在表中的位置序号;若查找失败,则返回0。
(2)算法步骤:
- 从第一个元素起,依次将其值和e相比较,若找到值与e相等的元素L.elem[i],则查找成功,返回该元素的序号i+1
- 若查遍整个顺序表都没有找到,则查找失败,返回0
(3)算法描述:
Status LocateElem(SqList L,ElemType e)
{
int i;
for(i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;
return 0;
}
4.插入
(1)定义:在表的第i个位置插入一个新的数据元素e,使长度为n的线性表变成长度为n+1的线性表。
(2)算法步骤:
- 判断插入位置是否合法(1<=i<=n+1),若不合法则返回ERROR。
- 判断顺序表的存储空间是否已满,若满则返回ERROR。
- 将第n个至第i个位置的元素依次向后移动一个位置,空出第i个位置(i=n+1时无需移动)。
- 将要插入的新元素e放入第i个位置
- 表长加1
(3)算法描述:
Status ListInsert(SqList &L,int i,ElemType e)
{
int j;
if(i<1||i>L.length+1) return ERROR;
if(L.length==MAXSIZE) return ERROR;
for(j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
++L.length;
return OK;
}
5.删除
(1)定义:将表的第i个元素删去,将长度为n的线性表变成长度为n-1的线性表。
(2)算法步骤:
- 判断删除位置i是否合法(合法值为1<=i<=n),若不合法则返回ERROR。
- 将第i+1个至第n个元素依次向前移动一个位置(i=n无需移动)。
- 表长减1。
(3)算法描述:
Status ListDelete(SqList &L,int i)
{
int j;
if(i<1||i>L.length+1) return ERROR;
for(j=i;j<=L.length;j++)
L.elem[j-1]=L.elem[j];
--L.length;
return OK;
}
6.完整代码的描述
#include <iostream>
using namespace std;
#define MAXSIZE 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef char ElemType;
//顺序表类型SqList的定义
typedef struct
{
ElemType *elem;
int length;
}SqList;
//初始化
Status InitList(SqList &L)
{
L.elem=new ElemType[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK;
}
//销毁
void DestroyList(SqList &L)
{
if(L.elem) delete L.elem;
}
//判断表是否为空
Status ListEmpty(SqList L)
{
if(L.length==0) return 1;
else return 0;
}
//求表长
Status ListLength(SqList L)
{
if(L.length) return L.length;
else return ERROR;
}
//输出顺序表内容
Status TraverseList(SqList L)
{
cout<<"输出顺序表L:";
int i;
for(i=0;i<L.length;i++)
{
cout<<L.elem[i];
}
}
//按位置取值
Status GetElem(SqList L,int i,ElemType &e)
{
if(i<1||i>L.length) return ERROR;
e=L.elem[i-1];
return OK;
}
//按值查找
Status LocateElem(SqList L,ElemType e)
{
int i;
for(i=0;i<L.length;i++)
if(L.elem[i]==e) return i+1;
return 0;
}
//插入数据元素
Status ListInsert(SqList &L,int i,ElemType e)
{
int j;
if(i<1||i>L.length+1) return ERROR;
if(L.length==MAXSIZE) return ERROR;
for(j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];
L.elem[i-1]=e;
++L.length;
return OK;
}
//删除数据元素
Status ListDelete(SqList &L,int i)
{
int j;
if(i<1||i>L.length+1) return ERROR;
for(j=i;j<=L.length;j++)
L.elem[j-1]=L.elem[j];
--L.length;
return OK;
}
void menu()
{
cout<<"-------顺序表的定义和基本操作------\n";
cout<<"- 1初始化顺序表 \n";
cout<<"- 2销毁顺序表 \n";
cout<<"- 3判断表是否为空 \n";
cout<<"- 4求表长 \n";
cout<<"- 5输出顺序表的内容 \n";
cout<<"- 6按位置取值 \n";
cout<<"- 7按值查找 \n";
cout<<"- 8插入数据元素 \n";
cout<<"- 9删除数据元素 \n";
cout<<"- 0退出 \n";
cout<<"请输入菜单对应的数字:";
}
int main()
{
SqList L;
int i,k;
ElemType c;
for(;;)
{
menu();
cin>>k;
switch(k)
{
case 1:InitList(L);
cout<<"初始化顺序表L:L.elem="<<&L.elem<<" , L.length="<<L.length<<"\n";
system("pause");//系统函数,暂停界面,显示结果,按任意键继续
system("cls");//系统函数,清除屏幕上的运行结果和上一次输出的菜单
break;
case 2:DestroyList(L);
cout<<"释放顺序表L:L.elem="<<&L.elem<<" , L.length="<<L.length<<"\n";
system("pause");
system("cls");
break;
case 3:cout<<"判断顺序表L当前是否为空:";
if(ListEmpty(L)) cout<<"L为空\n";
else cout<<"L不为空\n";
system("pause");
system("cls");
break;
case 4:cout<<"顺序表L的当前长度为:"<<ListLength(L)<<"\n";
system("pause");
system("cls");
break;
case 5:TraverseList(L);
cout<<"\n";
system("pause");
system("cls");
break;
case 6:cout<<"请输入查找位置1~"<<ListLength(L)<<";";
cin>>i;
if(GetElem(L,i,c))
cout<<"位置上的数据是"<<c<<"\n" ;
else
cout<<"没有找到,请合适查找位置是否正确。\n";
system("pause");
system("cls");
break;
case 7:cout<<"请输入要查找的数据:";
cin>>c;
if(i=LocateElem(L,c))
cout<<c<<"是顺序表中的第"<<i<<"个数据\n";
else
cout<<"不存在此顺序表中\n";
system("pause");
system("cls");
break;
case 8:cout<<"请输入要插入的数据:";
cin>>c;
cout<<"请输入要插入的数据位置(1-"<<ListLength(L)+1<<"):";
cin>>i;
if(ListInsert(L,i,c))
cout<<"插入成功!\n";
else
cout<<"插入失败,请核实插入位置\n";
system("pause");
system("cls");
break;
case 9: cout<<"请输入要删除的数据位置(1-"<<ListLength(L)<<"):";
cin>>i;
if(ListDelete(L,i))
cout<<"删除成功!"<<"\n";
else
cout<<"删除失败,请核实删除位置\n";
system("pause");
system("cls");
break;
case 0:return 0;
}
}
}
7.效果截图
注:1.每次进入都需要进行初始化,才可以进行其他操作;
2.在进行销毁操作之后是不能进行其他操作的,需要重新初始化;