删除顺序表前i个元素。已知顺序表的数据结构如下:
#define MaxSize 100
typedef struct
{
int data[MaxSize];
int last;
} SeqList;
使用如下函数原型:
bool fnDelete( SeqList *L, int i );
//成功删除则返回true,否则返回false
设计算法并在主函数调用:
#include "stdio.h"
#include "stdbool.h"
#include "stdlib.h"
#define MaxSize 6
typedef struct
{
int data[MaxSize];
int last;
} SeqList;
// 使用如下函数原型:
bool fnDelete(SeqList *L, int i);
//成功删除则返回true,否则返回false
int main(int argc, char const *argv[])
{
SeqList seqlist = {
.data = {1, 2, 3, 4, 5},
.last = 5};
// bool res = fnDelete(&seqlist, 0); // 1 2 3 4 5 last:5
// bool res = fnDelete(&seqlist, 2); // 3 4 5 last:3
bool res = fnDelete(&seqlist, 5); // last:0
return 0;
}
bool fnDelete(SeqList *L, int i)
{
if(i >= 0 && i < L->last + 1 ){
for(int j = 0; j < L->last - i; j++){
L->data[j] = L->data[j + i];
}
L->last -= i;
return true;
}else{
return false;
}
}