```cpp
运行环境:devcpp
编写语言:C++
线性表的顺序结构算法操作
#include<iostream>
using namespace std;
#define MaxSize 20
typedef struct{
int data[MaxSize];
int length;
}Sqlist;
int main(){
void CreateList(Sqlist *&L,int a[],int n);
void GetList(Sqlist *&L);
void InitList(Sqlist *&L);
void DestoryList(Sqlist *&L);
bool ListEmpty(Sqlist *L);
int ListLength(Sqlist *L);
int GetElem(Sqlist *L,int i);
bool ListInsert(Sqlist *&L,int i,int e);
bool ListDelete(Sqlist *&L,int i,int &e);
Sqlist *L;
int e;
int a[] = {1,2,3,4,5};
CreateList(L,a,5);
e = GetElem(L,2);
GetList(L);
cout<<'\n'<<"下标为2的元素"<<e<<" "<<endl;
ListInsert(L,5,6);
cout<<"在第5个位置插入一个数字:6,后线性表为:"<<endl;
GetList(L);
cout<<'\n'<<"在第2个位置删除一个数字,后线性表为:"<<endl;
ListDelete(L,2,e);
GetList(L);
return 0;
}
bool ListInsert(Sqlist *&L,int i,int e){
if(i>L->length||i<0){
return false;
}
i--;
for(int j = L->length;j>i;j--){
L->data[j] = L->data[j-1];
}
L->data[i] = e;
L->length++;
return true;
}
bool ListDelete(Sqlist *&L,int i,int &e){
if(i>L->length||i<0){
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;
}
void CreateList(Sqlist *&L,int a[],int n){
int i = 0,k = 0;
L = new Sqlist();
for(i = 0;i<n;i++){
L->data[i] = a[i];
k++;
}
L->length = k;
}
void DestoryList(Sqlist *&L){
delete L;
}
void GetList(Sqlist *&L){
for (int i =0;i<L->length;i++){
cout<<L->data[i]<<" ";
}
}
void InitList(Sqlist *&L){
L = new Sqlist;
L->length = 0;
}
bool ListEmpty(Sqlist *L){
return (L->length == 0);
}
int ListLength(Sqlist *L){
return (L->length);
}
int GetElem(Sqlist *L,int i){
if(i>L->length||i<0){
return 0;
}
return (L->data[i]);
}