📄 main_sqlist.cpp
字号:
#include"c1.h"
typedef int ElemType;
// c2-1.h 线性表的动态分配顺序存储结构
#define MAXLISTSIZE 10 // 线性表存储空间的初始分配量
struct SqList
{
ElemType *elem; // 存储空间基址
int length; // 当前长度
int listsize; // 当前分配的存储容量(以sizeof(ElemType)为单位)
};
char pause;
void handle_choice(int choice);
SqList L;
//bo2-2.cpp
Status comp(ElemType c1,ElemType c2)
{ // 数据元素判定函数(相等为TRUE,否则为FALSE)
if(c1==c2)
return TRUE;
else
return FALSE;
}
void visit(ElemType &c) // 访问操定为:输出元素值
{
cout<<c<<ends;
}
Status CreateSqList(SqList &L,int i) // 算法2.3
{ // 操作结果:构造一个空的顺序线性表
int j;
L.elem=new ElemType[MAXLISTSIZE];
if(!L.elem)
exit(OVERFLOW); // 存储分配失败
L.length=i; // 空表长度为0
for(j=1;j<=i;j++)
cin>>L.elem[j-1];
L.listsize=MAXLISTSIZE; // 初始存储容量
return OK;
}
Status DestroyList(SqList &L)
{ // 初始条件:顺序线性表L已存在。操作结果:销毁顺序线性表L
delete[] L.elem;
L.elem=NULL;
L.length=0;
L.listsize=0;
return OK;
}//DestroyList
int ListLength(SqList L)
{ // 初始条件:顺序线性表L已存在。操作结果:返回L中数据元素个数
return L.length;
}
Status GetElem(SqList L,int i,ElemType &e)
{ // 初始条件:顺序线性表L已存在,1≤i≤ListLength(L)
// 操作结果:用e返回L中第i个数据元素的值
if(i<1||i>L.length)
exit(ERROR);
e=*(L.elem+i-1);
return OK;
}
int LocateElem(SqList L,ElemType e,Status(*compare)(ElemType,ElemType))
{ // 初始条件:顺序线性表L已存在,compare()是数据元素判定函数(满足为1,否则为0)
// 操作结果:返回L中第1个与e满足关系compare()的数据元素的位序。
// 若这样的数据元素不存在,则返回值为0。算法2.6
ElemType *p;
int i=1; // i的初值为第1个元素的位序
p=L.elem; // p的初值为第1个元素的存储位置
while(i<=L.length&&!compare(*p++,e))
++i;
if(i<=L.length)
return i;
else
return 0;
}
Status ListInsert(SqList &L,int i,ElemType e) // 算法2.4
{ // 初始条件:顺序线性表L已存在,1≤i≤ListLength(L)+1
// 操作结果:在L中第i个位置之前插入新的数据元素e,L的长度加1
ElemType *q,*p;
if(i<1||i>L.length+1) // i值不合法
return ERROR;
q=L.elem+i-1; // q为插入位置
for(p=L.elem+L.length-1;p>=q;--p) // 插入位置及之后的元素右移
*(p+1)=*p;
*q=e; // 插入e
++L.length; // 表长增1
return OK;
}
Status ListDelete(SqList &L,int i,ElemType &e) // 算法2.5
{ // 初始条件:顺序线性表L已存在,1≤i≤ListLength(L)
// 操作结果:删除L的第i个数据元素,并用e返回其值,L的长度减1
ElemType *p,*q;
if(i<1||i>L.length) // i值不合法
return ERROR;
p=L.elem+i-1; // p为被删除元素的位置
e=*p; // 被删除元素的值赋给e
q=L.elem+L.length-1; // 表尾元素的位置
for(++p;p<=q;++p) // 被删除元素之后的元素左移
*(p-1)=*p;
L.length--; // 表长减1
return OK;
}
Status ListDisplay(SqList L,void(*vi)(ElemType&))
{ // 初始条件:顺序线性表L已存在
// 操作结果:依次对L的每个数据元素调用函数vi()。一旦vi()失败,则操作失败
// vi()的形参加'&',表明可通过调用vi()改变元素的值
ElemType *p;
int i;
p=L.elem;
for(i=1;i<=L.length;i++)
vi(*p++);
cout<<endl;
return OK;
}
//主函数
int main()
{
system("cls");//执行系统命令cls,清屏
int choice;
//cin.get(pause);
do
{//显示主菜单
//system("cls");
cout<<"1-创建顺序表(长度最长为20)\n";
cout<<"2-在顺序表第i个位置插入元素\n";
cout<<"3-删除顺序表中第i个位置的元素\n";
cout<<"4-返回第i个元素的值\n";
cout<<"5-元素定位\n";
cout<<"6-显示顺序表\n";
cout<<"7-测试表长\n";
cout<<"8-退出\n";
cout<<"Enter choice:";
cin>>choice;
handle_choice(choice);//Call function to direct flow based on choice
}while(choice!=8);//Repeat menu until user chooses to exit
return 0;
}//end of main function
void handle_choice(int choice) //根据用户选择调用对应处理函数
{ int i,e;
switch(choice)
{
case 1://If choice was to add a record to the database
cout<<"请输入要创建的顺序表中元素个数:";
cin>>i;//将创建的链表中数据元素的个数
cout<<endl;
CreateSqList(L,i);
break;
case 2://If choice was to display all records in the database
cout<<"请输入插入位置:";
cin>>i;//将创建的链表中数据元素的个数
cout<<endl;
cout<<"请输入插入元素的值:";
cin>>e;//将创建的链表中数据元素的个数
cout<<endl;
ListInsert(L,i,e);
break;
case 3://If choice was to
cout<<"请输入删除位置:";
cin>>i;//将创建的链表中数据元素的个数
cout<<endl;
ListDelete(L,i,e);
cout<<"被删除元素为:"<<e<<endl;
cin.get(pause);
system("pause");
break;
case 4://If choice was to
cout<<"请输入要查询的元素位置:";
cin>>i;
GetElem(L,i,e);
cout<<"第"<<i<<"个元素值为:"<<e<<endl;
cin.get(pause);
system("pause");
break;
case 5://If choice was to
cout<<"请输入要查询的元素值:";
cin>>e;//
i=LocateElem(L,e,comp);
cout<<"查询元素"<<e<<"位于链表的位置为:"<<i<<endl;
cin.get(pause);
system("pause");
break;
case 6://
ListDisplay(L,visit);
cout<<endl;
cin.get(pause);
system("pause");
break;
case 7://If choice was to
i=ListLength(L);
cout<<"该链表长为:"<<i<<endl;
cin.get(pause);
system("pause");
break;
case 8:
DestroyList(L);
break;
default://If any other(invalid) choice was entered,display error message
cout<<"Invalid choice\n";
break;
}
}//end of function handle_choice
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -