问题如图:
#include
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
//Status 为函数的类型,其值是函数结果状态代码,如OK等
typedef int Status;
// ElemType为数据元素类型,根据实际情况而定,这里假设为int
typedef int ElemType;
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef struct
{
ElemType data[MAXSIZE]; /* 数组,存储数据元素 */
int length; /* 表示当前数组中有效元素个数 */
}SqList;
/* 函数定义 */
Status input(SqList *L); /*输入过程中,0作为结束输入的标志数,不作为有效;
如果遇到0之前,数组已经装满,则余下的数不再读到数组中*/
void Union(SqList *La,SqList Lb);
Status ListLength(SqList L);
Status GetElem(SqList L,ElemType i,ElemType *e);
Status LocateElem(SqList L,ElemType e,Status compare(ElemType a1,ElemType a2)); /*判断元素e与数组L中
的元素是否存在compare关系*/
Status ListInsert(SqList *L,ElemType i,ElemType e); /*往数组中插入数据插入时,
如果数组已经到达上限,则不再往数组中插入新数据*/
Status ListEmpty(SqList L); //判断数组是否为空
Status ListDelete(SqList *L,ElemType i,ElemType *e); //删除指定位置的元素,成功返回OK,不成功返回ERROR
Status equal(ElemType a1,ElemType a2);
void output(SqList *L); //输出数组中所有元素
int main(void){
int sign,i;
ElemType e;
SqList La,Lb;
input(&La);input(&Lb);
scanf("%d",&i);
Union(&La,Lb);
output(&La);
if(ListDelete(&La,i,&e)==ERROR)
printf("Position Error\n");
else
printf("Delete Point Value: %d\n",e);
output(&La);
return 0;
}
void output(SqList *L)
{
int i;
if(ListEmpty(*L)==OK)
printf("No Element!");
else
for (i = 0; i < L->length; i++)
{
printf("%d ", L->data[i]);
}
printf("\n");
}
void Union(SqList *La,SqList Lb)
{
int lalen, lblen, ii, e;
lblen = ListLength(Lb);
for (ii = 1; ii <= lblen; ii++)
{
GetElem(Lb, ii, &e);
if (!LocateElem(*La,e,equal))
{
lalen = ListLength(*La);
ListInsert(La,++lalen,e);
}
}
}
/* 请在这里填写答案 */
Status input(SqList *L)
{
int i,t,m=0;
scanf("%d",&t);
for(i=0;t!=0;i++)
{
m=1;
L->data[i]=t;
scanf("%d",&t);
}
L->length=i;
if(m==0)
L->length=0;
}
Status ListLength(SqList L)
{
int t;
t=L.length;
return t;
}
Status GetElem(SqList L,ElemType i,ElemType *e)
{
*e=L.data[i-1];
return *e;
}
Status LocateElem(SqList L,ElemType e,Status compare(ElemType a1,ElemType a2))
{
int i;
for(i=0;i<=L.length-1;i++)
{
if(compare(L.data[i],e)==1)
return 1;
}
return 0;
}
Status ListEmpty(SqList L)
{
if(L.length==0)
return OK;
else
return ERROR;
}
Status equal(ElemType a1,ElemType a2)
{
if(a1==a2)
return 1;
else
return 0;
}
Status ListInsert(SqList *L,ElemType i,ElemType e)
{
if(i<=20)
{
int j=0;
SqList *L2;
L2=L;
L2->length=i;
L2->data[i-1]=e;
}
}
Status ListDelete(SqList *L,ElemType i,ElemType *e)
{
int t,k;
SqList L2;
for(t=0;t<=L->length-1;t++)
{
L2.data[t]=L->data[t];
}
if(L->length>=i)
{
*e=L->data[i-1];
k=L->length;
L->length=L->length-1;
for(t=i-1;t<=k-2;t++)
L->data[t]=L2.data[t+1];
return OK;
}
else
return ERROR;
}
请问你的问题解决了吗,我也卡在这里了,虚心求教呜呜呜
定义数组名为:array
从第index个开始,删除n个,n直接替换为需要的数字
array.splic(index,n)
这么改,供参考:
#include<stdio.h>
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
//Status 为函数的类型,其值是函数结果状态代码,如OK等
typedef int Status;
// ElemType为数据元素类型,根据实际情况而定,这里假设为int
typedef int ElemType;
#define MAXSIZE 20 /* 存储空间初始分配量 */
typedef struct
{
ElemType data[MAXSIZE]; /* 数组,存储数据元素 */
int length; /* 表示当前数组中有效元素个数 */
}SqList;
/* 函数定义 */
Status input(SqList* L); /*输入过程中,0作为结束输入的标志数,不作为有效;
如果遇到0之前,数组已经装满,则余下的数不再读到数组中*/
void Union(SqList* La, SqList Lb);
Status ListLength(SqList L);
Status GetElem(SqList L, ElemType i, ElemType* e);
Status LocateElem(SqList L, ElemType e, Status compare(ElemType a1, ElemType a2)); /*判断元素e与数组L中
的元素是否存在compare关系*/
Status ListInsert(SqList* L, ElemType i, ElemType e); /*往数组中插入数据插入时,
如果数组已经到达上限,则不再往数组中插入新数据*/
Status ListEmpty(SqList L); //判断数组是否为空
Status ListDelete(SqList* L, ElemType i, ElemType* e); //删除指定位置的元素,成功返回OK,不成功返回ERROR
Status equal(ElemType a1, ElemType a2);
void output(SqList* L); //输出数组中所有元素
int main(void) {
int sign, i;
ElemType e;
SqList La, Lb;
input(&La); input(&Lb);
scanf("%d", &i);
Union(&La, Lb);
output(&La);
if (ListDelete(&La, i, &e) == ERROR)
printf("Position Error\n");
else
printf("Delete Point Value: %d\n", e);
output(&La);
return 0;
}
void output(SqList* L)
{
int i;
if (ListEmpty(*L) == OK)
printf("No Element!");
else
for (i = 0; i < L->length; i++)
{
printf("%d ", L->data[i]);
}
printf("\n");
}
void Union(SqList* La, SqList Lb)
{
int lalen, lblen, ii, e;
lblen = ListLength(Lb);
for (ii = 1; ii <= lblen; ii++)
{
GetElem(Lb, ii, &e);
if (!LocateElem(*La, e, equal))
{
lalen = ListLength(*La);
ListInsert(La, ++lalen, e);
}
}
}
/* 请在这里填写答案 */
Status input(SqList* L)
{
int i, t, m = 0;
scanf("%d", &t);
for (i = 0; t != 0 && i < MAXSIZE; i++)
//for (i = 0; t != 0; i++) 修改
{
m = 1;
L->data[i] = t;
scanf("%d", &t);
}
L->length = i;
if (m == 0)
L->length = 0;
return OK;
}
Status ListLength(SqList L)
{
int t;
t = L.length;
return t;
}
Status GetElem(SqList L, ElemType i, ElemType* e)
{
if (i < 1 || i > MAXSIZE) //修改
return ERROR;
*e = L.data[i - 1];
return OK; //*e; 修改
}
Status LocateElem(SqList L, ElemType e, Status compare(ElemType a1, ElemType a2))
{
int i;
for (i = 0; i < L.length; i++) //for (i = 0; i <= L.length - 1; i++) 修改
{
if (compare(L.data[i], e) == 1)
return 1;
}
return 0;
}
Status ListEmpty(SqList L)
{
if (L.length == 0)
return OK;
else
return ERROR;
}
Status equal(ElemType a1, ElemType a2)
{
if (a1 == a2)
return 1;
else
return 0;
}
Status ListInsert(SqList* L, ElemType i, ElemType e)
{
if (i <= MAXSIZE) //if (i <= 20) 修改
{
//int j = 0;
//SqList* L2;
//L2 = L;
L->length = i;
L->data[i - 1] = e;
return OK;
}
return ERROR;
}
Status ListDelete(SqList* L, ElemType i, ElemType* e)
{
int t, k;
//SqList L2;
if (i < 1 || i > L->length)
return ERROR;
//if (L->length >= i)
//{
*e = L->data[i - 1];
//k = L->length;
//L->length = L->length - 1;
for (t = i; t < L->length; t++) //for (t = i - 1; t <= k - 2; t++) 修改
L->data[t - 1] = L->data[t]; //修改
L->length--;
return OK;
//}
//else
// return ERROR;
}
都有提示了,数组2没有输入的情况出错
你可以尝试输入一下数组二