#include<iostream>
using namespace std;
class List
{
public:
List(int mSize);
//~List() {Delete element[]};//析构函数
bool IsEmpty() const;//判断顺序表是否为空
int Length() const;//返回线性表长度
bool Find(int i,char& x) const;//在x中返回表中下标为i的元素ai.若不存在,则返回FALSE,否则返回TRUE。
int Search(char x) const;//若x不在表中返回-1,若在表中,返回下标
bool Insert(int i,char w);//在顺序表第i个元素之后插入元素x
bool Delete(int i);//删除线性表第i个元素
bool Update(int i,char x);//更新线性表第i个元素的数据
void Output()const;//输出线性表
bool creatList();
private:
int maxLength;//顺序表最大长度
char* element;//建立一维数组
int n;
};
List::List(int mSize)
{
maxLength=mSize;
element=new char[maxLength];
n=0;
}
bool List::IsEmpty() const
{
return (n==0);
}
int List::Length() const
{
return n;
}
bool List::Find(int i,char& x) const
{
if(i<0||i>n-1)
{
cout<<"Out of Bounds"<<endl;
return false;
}
x=element[i];
return true;
}
int List::Search(char x) const
{
for(int i=0;i<n;i++)
{
if(x==element[i])
return ++i;
}
return -1;
}
bool List::Insert(int i,char w)
{
if(i<-1||i>n+1)
{cout<<"Out of Bounds"<<endl;return false;}
if(n==maxLength)//*************************************上溢出检查**********************
{cout<<"OverFlow"<<endl;return false;}
for(int j=i-1;j<n-1;j++)
{
element[j+1]=element[j];
}
element[i-1]=w;
n++;
return true;
}
bool List::Delete(int i)
{
if(!n){cout<<"UnderFlow"<<endl;return false;}//************************下溢出检查*******************************
if(i<0||i>n)
{cout<<"Out of Bounds"<<endl;return false;}
for(int j=i;j<n;j++)
{
element[j-1]=element[j];
}
n--;
return true;
}
bool List::Update(int i,char x)
{
if(i<0||i>n)
{cout<<"Out of Bounds"<<endl;return false;}
element[i-1]=x;
return true;
}
void List::Output()const
{
for(int i=0;i<n;i++)
cout<<element[i]<<" ";
cout<<endl;
}
void Menu()
{
cout<<"***************Menu*****************"<<endl;
cout<<"*****0. creatList ***************"<<endl;
cout<<"*****1. IsEmpty ***************"<<endl;
cout<<"*****2. Length ***************"<<endl;
cout<<"*****3. Find ***************"<<endl;
cout<<"*****4. Search ***************"<<endl;
cout<<"*****5. Insert ***************"<<endl;
cout<<"*****6. Delete ***************"<<endl;
cout<<"*****7. UPdate ***************"<<endl;
cout<<"*****8. Output ***************"<<endl;
cout<<" "<<endl;
cout<<"************************************"<<endl;
cout<<"Please inter your choose:"<<endl;
}
bool List::creatList()
{
for(int i=0;i<maxLength;i++)
{
cin>>element[i];
n++;
}
return true;
}
int main()
{ int len;
cout<<"请输入表长度"<<endl;
cin>>len;
List L(len);
int choose;
Menu();
cin>>choose;
if(choose<0||choose>8)
{cout<<"Out of Bounds"<<endl;return 0;}
while(choose!=-1)
{
switch(choose)
{
case 0:
cout<<"现在开始创建顺序表"<<endl;
if(L.creatList())
cout<<"创建成功"<<endl;
else
cout<<"创建失败"<<endl;
break;
case 1:
if(L.IsEmpty())
cout<<"顺序表为空表!"<<endl;
else
cout<<"顺序表不是空表"<<endl;
break;
case 2:
int a;
a=L.Length();
cout<<"顺序表长度为: "<<a<<endl;
break;
case 3:
cout<<"请输入您要查找的元素下标"<<endl;
int z;
cin>>z;
char x;
if(L.Find(z,x))
cout<<"查找成功,您要找的元素为: "<<x<<endl;
else
cout<<"查找失败"<<endl;
break;
case 4:
int k;
char c;
cout<<"请输入您要查找的元素:"<<endl;
cin>>c;
k=L.Search(c);
cout<<"您要查找的元素位序为: "<<k<<endl;
break;
case 5:
int i;
char w;
cout<<"请输入您要插入的元素位置:"<<endl;
cin>>i;
cout<<"请输入您要插入的元素:"<<endl;
cin>>w;
if(L.Insert(i,w))
cout<<"插入成功"<<endl;
else
cout<<"插入失败"<<endl;
break;
case 6:
int l;
cout<<"请输入您要删除的位置:"<<endl;
cin>>l;
if(L.Delete(l))
cout<<"删除成功"<<endl;
else
cout<<"删除失败"<<endl;
break;
case 7:
int o;
cout<<"请输入您要更新的位置:"<<endl;
cin>>o;
char p;
cout<<"请输入您要更新的元素:"<<endl;
cin>>p;
if(L.Update(o,p))
cout<<"修改成功"<<endl;
else
cout<<"修改失败"<<endl;
break;
case 8:
L.Output();
break;
};
Menu();
cin>>choose;
}
}