程序填空题:顺序表插入操作
顺序表插入操作
```
#include
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int datatype;
typedef struct {
datatype *elem;
int length;
} SqList;
int ListInsert_Sq(SqList &L, int i, datatype e) {
if ((i < 1) || (i > L.length + 1))
return ERROR;
if (L.length == MAXSIZE)
return ERROR;
for (int j = L.length - 1; j >= i - 1; j--)
@@[L.elem[j + 1] = L.elem[j]](2);
@@[L.elem[i - 1] = e](2);
++L.length;
return OK;
}
int main() {
SqList L;
int i = 0, n,a;
datatype e;
L.elem = new datatype[MAXSIZE];
L.length = 0;
cin >> n;
for (i=0;i cin >> L.elem[i];
L.length = i;
cin >> a >> e;
if (ListInsert_Sq(L, a, e))
{
for (i = 0; i < L.length; i++)
if(i==0)
cout << L.elem[i];
else
cout << " " << L.elem[i];
}
else
cout << "ERROR";
return 0;
}
```
答案:
第1空:L.elem[j + 1] = L.elem[j]
第2空:L.elem[i - 1] = e
```
#include
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int datatype;
typedef struct {
datatype *elem;
int length;
} SqList;
int ListInsert_Sq(SqList &L, int i, datatype e) {
if ((i < 1) || (i > L.length + 1))
return ERROR;
if (L.length == MAXSIZE)
return ERROR;
for (int j = L.length - 1; j >= i - 1; j--)
@@[L.elem[j + 1] = L.elem[j]](2);
@@[L.elem[i - 1] = e](2);
++L.length;
return OK;
}
int main() {
SqList L;
int i = 0, n,a;
datatype e;
L.elem = new datatype[MAXSIZE];
L.length = 0;
cin >> n;
for (i=0;i
L.length = i;
cin >> a >> e;
if (ListInsert_Sq(L, a, e))
{
for (i = 0; i < L.length; i++)
if(i==0)
cout << L.elem[i];
else
cout << " " << L.elem[i];
}
else
cout << "ERROR";
return 0;
}
```
答案:
第1空:L.elem[j + 1] = L.elem[j]
第2空:L.elem[i - 1] = e