#include<stdio.h>
#define MaxSize 10
typedef struct {
int data[MaxSize];
int length;
}SqList;
void InitList(SqList& L) { //一定要初始化顺序表为0
L.length = 0;
}
bool ListInsert(SqList& L, int i, int e) { //不是void时,都要返回值
if (i > L.length||i<1||i>L.length+1) {
return false;
}
else {
for (int j = L.length; j >= i; j--) {
L.data[j] = L.data[j-1]; //一定是j-1赋给j,
}
L.data[i-1] = e;
L.length++;
return true;
}
}
int main() {
SqList L;
InitList(L);
int a = 0;
L.length = 5; //不要忘记设置长度
for (int i = 0; i < L.length; ++i) { //向顺序表里加入数据
L.data[i] = 1 + a++;
printf("%d ", L.data[i]);
}
printf("\n");
ListInsert(L, 2, 6); //在第二个位置插入6
for (int i = 0; i < L.length; ++i) { //打印插入后顺序表数据
printf("%d ", L.data[i]);
}
return 0;
}