#include <iostream>
using namespace std;
#define Max 100
typedef struct{
int *p;
int length;
} SQlist;
bool Initlist(SQlist &L){
L.p=new int[Max];
if(!L.p) return 0;
L.length=0;
return 1;
}
void Destroylist(SQlist &L){
if(L.p) delete [] L.p;
}
int Getlength(SQlist L){
return(L.length);
}
bool Isempty(SQlist L){
if(L.length==0) return 1;
else return 0;
}
bool Getelem(SQlist L,int i,int &e){
if(i<1||i>L.length) return 0;
e=L.p[i-1];
return 1;
}
int Locateelem(SQlist L,int e){
for(int i=0;i<L.length;i++){
if(L.p[i]==e) return i+1;
}
return 0;
}
bool Insertelem(SQlist &L,int i,int e){
if(i<1||i>L.length+1) return 0;
if(L.length==Max) return 0;
for(int j=L.length;j>i-1;j--){
L.p[j]=L.p[j-1];
}
L.p[i-1]=e;
L.length++;
return 1;
}
bool Deletelem(SQlist &L,int i){
if(i<1||i>L.length) return 0;
for(int j=i;j<=L.length-1;j++){
L.p[j-1]=L.p[j];
}
L.length--;
return 1;
}
int main() {
SQlist A;
int a;
printf("%d=1,创建成功\n",Initlist(A));
printf("%d=1,顺序表为空\n",Isempty(A));
Insertelem(A, 1, 1);
Insertelem(A, 2, 2);
Insertelem(A, 3, 3);
Insertelem(A, 4, 4);
printf("顺序表长度为%d\n",Getlength(A));
Getelem(A,2,a);
printf("第二位是%d\n",a);
printf("查找 3 是%d 位\n",Locateelem(A, 3));
Insertelem(A, 2, 5);
for(int j=0;j<A.length;j++){
printf("%d",A.p[j]);
}
printf("\n");
Deletelem(A, 1);
for(int j=0;j<A.length;j++){
printf("%d",A.p[j]);
}
Destroylist(A);
system("pause");
return 0;
}
|