#include
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXSIZE 100
using namespace std;
typedef int Status;
typedef int elemtype;
typedef struct
{ elemtype *elem; //BASE ADDRESS
int length; //NUMBER OF BOOk
}SqList;
Status InitList_Sq(SqList &L){
L.elem=new elemtype[MAXSIZE];
//L.elem= new elemtype[MAXSIZE];
if(!L.elem) exit(OVERFLOW);
L.length=0;
return OK; }
Status GetElem(SqList L,int i,elemtype &e)
{
if (i<1||i>L.length) return ERROR;
e=L.elem[i-1];
return OK;
}
int 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_Sq(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_Sq(SqList &L,int i){
int j;
if((i<1)||(i>L.length)) return ERROR;
if (L.length==0) return ERROR;
for (j=i;j<=L.length-1;j++)
L.elem[j-1]=L.elem[j];
--L.length;
return OK;
}
void print(SqList L)
{
int i;
for(i=0;i
cout<" " ;
}
void menu()
{
cout<<" 0:Create."<;
cout<<" 1: Access."<;
cout<<" 2: Locate."<;
cout<<" 3: Insert."<;
cout<<" 4: Delete."<;
cout<<" 5: print."<;
cout<<" 6: Exit."<;
}
int main()
{
SqList Q;
int option,k;
elemtype x;
while(1)
{
menu();
cout<<"Please input your option(0-6):";
cin>>option;
switch(option)
{
case 0:if(InitList_Sq(Q)==OK)
cout<<"Succeed!\n";
else cout<<"Fail!\n";
break;
case 1:cout<<"please input the number:";
cin>>k;
if(GetElem(Q,k,x))
cout<<"the "<<k<<" elementn is "<<x<<endl;
else cout<<"fail!\n";
break;
case 2:cout<<"please input the data you want to locate:";
cin>>x;
k=LocateELem(Q,x);
if(k==0)
cout<<x<<" is not found\n";
else cout<<x<<" is "<<k<<"th element\n";
break;
case 3:cout<<"please input the location:";
cin>>k;
cout<<"please input the data you want to insert:";
cin>>x;
if(ListInsert_Sq(Q,k,x))
cout<<x<<" is inserted.\n";
else cout<<x<<" is not inserted.\n";
break;
case 4:cout<<"please input the location:";
cin>>k;
if(ListDelete_Sq(Q,k))
cout<<"success!\n";
else cout<<"fail!\n";
break;
case 5:print(Q);break;
case 6:exit(0);break;
default: cout<<"illegal input!\n";break;
}
}
return 1;
}
我的建议是: