赞
踩
(1)定义:构造一个空的顺序表
(2)算法步骤:
(3)算法描述:
- Status InitList(SqList &L)
- {
- L.elem=new ElemType[MAXSIZE];
- if(!L.elem) exit(OVERFLOW);
- L.length=0;
- return OK;
- }
(1)定义:根据指定的位置序号i,获取顺序表中第i个数据元素的值。由于顺序存储结构具有随机存取的特点,可以直接通过数组下标定位得到,elem[i-1]单元存储第i个数据元素。
(2)算法步骤:
(3)算法描述:
- Status GetElem(SqList L,int i,ElemType &e)
- {
- if(i<1||i>L.length) return ERROR;
- e=L.elem[i-1];
- return OK;
- }
(1)定义:根据指定的元素值e,查找顺序表中第1个值与e相等的元素。若查找成功,则返回该元素在表中的位置序号;若查找失败,则返回0。
(2)算法步骤:
(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;
- }
(1)定义:在表的第i个位置插入一个新的数据元素e,使长度为n的线性表变成长度为n+1的线性表。
(2)算法步骤:
(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;
- }
(1)定义:将表的第i个元素删去,将长度为n的线性表变成长度为n-1的线性表。
(2)算法步骤:
(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;
- }
- #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;
- }
- }
- }

注:1.每次进入都需要进行初始化,才可以进行其他操作;
2.在进行销毁操作之后是不能进行其他操作的,需要重新初始化;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。