主要考察的是bool ListDelete(SeqList &L, int i, int &e)函数的使用
#include<stdio.h>
using namespace std;
#define MaxSize 10
typedef struct {
int data[MaxSize];
int length;
}SeqList;
bool ListDelete(SeqList &L, int i, int &e)
{
if (i<1 || i>L.length) //在表头与大于表头的地方都没元素,还删除啥?返回false
{
return false;
}
e = L.data[i - 1];
for (int j = i; j < L.length; j++)
{
L.data[j - 1] = L.data[j];
}
L.length--;
return true;
}
int main()
{
SeqList L;
printf("以下是始化数组,给赋值\n");
for (int i = 0; i <= MaxSize - 3; i++)
{
L.data[i] = i;
printf("%d ", L.data[i]);
L.length = i + 1;
printf("L.length=%d\n", L.length);
}
printf("以下是整个数组\n");
for (int i = 0; i <= MaxSize; i++)
{
printf("%d\n", L.data[i]);
}
int e;
bool valueL = ListDelete(L, 3,e); //顺序表的第三个元素被删除
printf("ListDelete的执行情况%d\n", valueL); //bool的输出是0和1,0失败,1成功
printf("以下是删除元素后的整个数组\n");
for (int i = 0; i <= MaxSize; i++)
{
printf("%d\n", L.data[i]);
}
printf("被删除的元素为%d", e);
return 0;
}