// 莆林之光nb
#include <bits/stdc++.h>
using namespace std;
const int MaxSize = 50;
typedef char ElemType;
typedef struct{
ElemType data[MaxSize];
int length;
}SqList;
// 从a[0] ~ a[n - 1] 创建顺序表
void CreateList(SqList *& L, ElemType a[], int n){
int k = 0; // 长度为k
L = (SqList * )malloc(sizeof(SqList));
for(int i = 0; i < n; ++i){
L -> data[i] = a[i];
k ++;
}
L -> length = k;
}
void InitList(SqList *& L){
L = (SqList * ) malloc(sizeof(SqList));
L -> length = 0;
}
void DestoryList(SqList *& L){
free(L);
}
bool ListEmpty(SqList *L){
return L -> length == 0;
}
int ListLength(SqList *L){
return L -> length;
}
void PrintList(SqList *L){
int len = L -> length;
for(int i = 0; i < len; ++i){
printf("%c ", L -> data[i]);
}
puts("");
}
bool GetElem(SqList *L, int i, ElemType &x){
if(i < 1 || i > L -> length) return false;
x = L -> data[i - 1];
return true;
}
int SearchElem(SqList *L, ElemType x){
for(int i = 0; i < L -> length; ++i){
if(x == L -> data[i]){
return i + 1;
}
}
return 0;
}
bool InsertElem(SqList *& L, int i, ElemType x){
if(i <= 0 || i > L -> length + 1 || L -> length >= MaxSize) return false;
i --;
for(int j = L -> length; j > i; -- j){
L -> data[j] = L -> data[j - 1];
}
L -> data[i] = x;
L -> length ++;
return true;
}
bool DeleteElem(SqList *& L, int i, ElemType &e){
if ( i < 1 || i > L -> length) return false;
i --;
e = L -> data[i];
for(int j = i; j < L -> length - 1; ++ j){
L -> data[j] = L -> data[j + 1];
}
L -> length --;
return true;
}
int main(){
char s[] = {'a', 'b', 'c', 'd', 'e'};
char x, dx;
SqList *List;
InitList(List);
CreateList(List, s, 5);
PrintList(List);
cout << ListLength(List) << endl;
if(ListEmpty(List)){
cout << "Empty" << endl;
}else{
cout << "No Empty" << endl;
}
if(GetElem(List, 3, x)) cout << "第三个元素是" << x << endl;
else cout << "没找到第三个元素" << endl;
int pos = SearchElem(List, 'a');
if(pos) cout << "a在" << pos << endl;
else cout << "未找到a" << endl;
InsertElem(List, 4, 'f');
PrintList(List);
DeleteElem(List, 3, dx);
PrintList(List);
DestoryList(List);
return 0;
}