设计一个算法InsertList(SqList *&L,int i,int e):在顺序表L中插入第i个元素e;
设计一个算法DeletList(SqList *&,int i):删除顺序表L的第i个元素;
函数接口定义:
在这里描述函数接口。例如:
bool InsertList(SqList *&L,int i,int e);
bool deletList(SqList *&L,int i);
L为顺序表,i为插入、删除的位置,e为插入元素值;
裁判测试程序样例:
#include<stdio.h>
#include<malloc.h>
#define MaxSize 20
typedef struct {
int data[MaxSize];
int length;
}SqList;
void InitList(SqList *&L)
{ L=(SqList *)malloc(sizeof(SqList));
L->length=0;
}
void CreateList(SqList *&L,int a[],int n)
{ int i;
for(i=0;i<n;i++)
L->data[i]=a[i];
L->length=n;
}
void Display(SqList *&L)
{ int i;
for(i=0;i<L->length;i++)
printf("%d ",L->data[i]);
printf("\n");
}
bool deletList(SqList *&L,int i);
bool InsertList(SqList *&L,int i,int e);
int main()
{ int a[MaxSize];
int n,i,e;
SqList *L;
InitList(L);
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d%d",&i,&e);
CreateList(L,a,n);
if(InsertList(L,i,e))
Display(L);
else
printf("插入位置参数错误,插入失败!");
return 0;
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
6
1 2 3 4 5 6
3 8
6
输出样例:
在这里给出相应的输出。例如:
1 2 8 3 4 5 6
1 2 8 3 4 6
参考答案:
bool InsertList(SqList *&L,int i,int e){
int j;
if(i<1 || i>L->length+1) return 0;
i--;
for(j=L->length;j>i;j--)
L->data[j]=L->data[j-1];
L->data[i]=e;
L->length++;
return 1;
}
bool deletList(SqList *&L,int i){
int j;
if(i<1 || i>L->length) return 0;
i--;
for(int j=i;j<L->length-1;j--)
L->data[j]=L->data[j+1];
L->length--;
return 1;
}
249

被折叠的 条评论
为什么被折叠?



