头文件
#include<iostream>
#define OK 1
#define ERROR 0
using namespace std;
typedef int status;
typedef int elemType;
顺序表数据结构
#define MAXSIZE 100//最大表长为100
typedef struct{
elemType *elem;//基地址
int length;//线性表长度
}sqList;
//solution类
class solution{
public:
status null_List(sqList &L);//构造空表、初始化
void out_List(sqList L);//顺序表输出
status get_Value(sqList L,int i,elemType &e);//取值
status locate_Elem(sqList L,elemType e);//查找元素
status insert_Elem(sqList &L,int i,elemType e);//插入
status delete_Elem(sqList &L,int i)//删除
void showMenu();//菜单函数
};
函数实现:
//构造空表、初始化
status solution::null_List(sqList &L){
L.elem=new elemType[MAXSIZE];//分配内存
if(!L.elem)//分配失败,退出
return ERROR;
L.length=0;//空表长为0
return OK;
}
//顺序表输出
void solution::out_List(sqList L){
for (int i=0;i<L.length-1;i++)
cout<<L.elem[i]<<"\t";
}
//顺序表取值
status solution::get_Value(sqList L,int i,elemType &e){
if(i<1||i>L.length)//判断i值的合理性
return ERROR;
for (int i=0;i<=L.length-1;i++){
cin>>L.elem[i];
e=L.elem[i-1];//elem[i-1]存储第i个数据
}
return OK;
}
//顺序表的查找
status solution::locate_Elem(sqList L,elemType e){//查找值为e 的元素
for (int i=0;i<L.length;i++){
if(L.elem[i]==e)
return i+1;
}
return ERROR;
}
//顺序表的插入
status solution::insert_Elem(sqList &L,int i,elemType e){
//在L中第i个位置插入新元素e,i的范围是[1,L.length+1];
if(i<1||i>L.length+1)
return ERROR;
if(L.length==MAXSIZE)//表满
return ERROR;
for (int j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];//元素后移
L.elem[i-1]=e;//新插入元素e在i个位置
++L.length;
return OK;
}
//顺序表的删除
status solution::delete_Elem(sqList &L,int i){
//在L中第i个位置删除元素,i的范围是[1,L.length];
if(i<1||i>L.length)
return ERROR;
for (int j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[i];//被删除元素之后的元素前移
--L.length;
return OK;
}
代码实现(合集)
#include"head.h"
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef int elemType;
typedef int status;
typedef struct{
elemType *elem;//存储空间基地址
int length;//当前长度
}sqList;
//类
class Solution{
public:
//构造空表、初始化
status null_List(sqList &L){
L.elem=new elemType[MAXSIZE];//分配内存
if(!L.elem)//分配失败,退出
return ERROR;
L.length=0;//空表长为0
return OK;
}
//顺序表输出
void out_List(sqList L){
for (int i=0;i<L.length-1;i++)
cout<<L.elem[i]<<"\t";
}
//顺序表取值
status get_Value(sqList L,int i,elemType &e){
if(i<1||i>L.length)//判断i值的合理性
return ERROR;
for (int i=0;i<=L.length-1;i++){
cin>>L.elem[i];
e=L.elem[i-1];//elem[i-1]存储第i个数据
}
return OK;
}
//顺序表的查找
status locate_Elem(sqList L,elemType e){//查找值为e 的元素
for (int i=0;i<L.length;i++){
if(L.elem[i]==e)
return i+1;
}
return ERROR;
}
//顺序表的插入
status insert_Elem(sqList &L,int i,elemType e){
//在L中第i个位置插入新元素e,i的范围是[1,L.length+1];
if(i<1||i>L.length+1)
return ERROR;
if(L.length==MAXSIZE)//表满
return ERROR;
for (int j=L.length-1;j>=i-1;j--)
L.elem[j+1]=L.elem[j];//元素后移
L.elem[i-1]=e;//新插入元素e在i个位置
++L.length;
return OK;
}
//顺序表的删除
status delete_Elem(sqList &L,int i){
//在L中第i个位置删除元素,i的范围是[1,L.length];
if(i<1||i>L.length)
return ERROR;
for (int j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[i];//被删除元素之后的元素前移
--L.length;
return OK;
}
};
int main(){
Solution Demo;
sqList L;
elemType e;
int i,length,choice;
cout<<"Please enter your choice:\n";
cout<<"******************************\n";
cout<<"(1)创建线性表.\t\t(2)插入元素.\n"//showMenu();
<<"(3) 删除元素.\t\t(4)查找元素.\n"
<<"(0) 退出."<<endl;
cin>>choice;
do{
switch(choice){
case 1:
Demo.null_List(L);
Demo.get_Value(L,i,e);
Demo.out_List(L);
cout<<"OK!\n";
break;
case 2:
cout<<"\n 请输入插入位置(大于等于1,小于等于"
<<L.length+1<< "):"
<<L.length<<endl;
cin>>i;
cout<<"\n 请输入要插入元素值:";
cin>>e;
Demo.insert_Elem(L,i,e);
Demo.out_List(L);
break;
case 3:
cout<<"\n 请输入删除位置(大于等于1,小于等于"<<L.length+1<< "):" <<L.length<<endl;
cin>>i;
Demo.delete_Elem(L,i);
Demo.out_List(L);
break;
case 4:
cout<<"Please cin the location you want to ask:\n";
cin>>i;
Demo.locate_Elem(L,e);
cout<<"it's location is: "<<L.elem[i-1]<<endl;
default:
cout<<"非法输入!\n";
}
cout<<"Please enter your choice:"<<endl;
cout<<"******************************"<<endl;
cout<<"(1)创建线性表.\t\t(2)插入元素.\n"
<<"(3) 删除元素. \t\t(4)查找元素.\n"
<<"(0)退出."<<endl;
cin>>choice;
}while(choice!=0);
cout<<"Bye..."<<endl;
system("pause>nul");
return 0;
}