6-2 集合合并-(数组) (30 分)

先分别输入两个数组La,Lb的元素数据(0 结束元素元素输入,输入数据没有排序要求),Lb中出现的新元素插入依次到La的后面,然后输入删除结点的位置(指La,Lb合并后的数组中欲删除的元素位置,位置从1开始计算),最后删除所有元素。

函数接口定义:

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));
Status ListInsert(SqList *L,ElemType i,ElemType e);
Status ListEmpty(SqList L);
Status ListDelete(SqList *L,ElemType i,ElemType *e); //如果删除位置不存在,输出"Position Error\n"
Status equal(ElemType a1,ElemType a2);
Status input(SqList *L);  //输入过程中,0作为结束输入的标志数,不作为有效数 
void output(SqList *L);  //输出数组中所有元素 
void empty(SqList *L);   //删除所有元素 

裁判测试程序样例:

#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;

/* 函数定义 */
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));
Status ListInsert(SqList *L,ElemType i,ElemType e);
Status ListEmpty(SqList L);
Status ListDelete(SqList *L,ElemType i,ElemType *e);
Status equal(ElemType a1,ElemType a2);
Status input(SqList *L);  //输入过程中,0作为结束输入的标志数,不作为有效数 
void output(SqList *L);  //输出数组中所有元素 
void empty(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);

    ListDelete(&La,i,&e);   
    output(&La);

    empty(&La);  
    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");
}

/* 请在这里填写答案 */

输入样例:

先分别输入La,Lb(0结束输入),然后输入欲在合并后数组中删除的结点位置

1 2 3 4 5 0
2 3 4 5 6 7 8 9 0
2

输出样例:

先输出合并后的数组,再输出在合并数组中删除结点后的数组

1 2 3 4 5 6 7 8 9 
1 3 4 5 6 7 8 9 
No Element!

我的解答

#include<memory.h>
void Union(SqList* La, SqList Lb) {
    ElemType e;
    int La_len, Lb_len;
    int i;
    La_len = ListLength(*La);
    Lb_len = ListLength(Lb);
    for (i = 1; i <= Lb_len; i++) {
        GetElem(Lb, i, &e);
        if (!LocateElem(*La, e, equal)) {
            ListInsert(La, ++La_len, e);
        }
    }
}
Status ListLength(SqList L) {//取列表长度函数
    int i = 0;
    while (1)
    {
        if (L.data[i] == 0)
        {
            break;
        }
        i++;
    }
    return i;
}
Status GetElem(SqList L, ElemType i, ElemType* e) {// 把 DATA[i] 给对应的 e
    int t;
    if (L.length > 0 && i <= L.length)
    {
        *e = L.data[i - 1];
        return OK;
    }
    else
        return ERROR;

}
Status LocateElem(SqList L, ElemType e, Status compare(ElemType a1, ElemType a2)) {//寻找符合要求元素的位序
    int i = 0;
    while (i < L.length)
    {
        if (compare(e, L.data[i]))
            break;
        i++;
    }
    if (i < L.length) {
        return i + 1;
    }
    return 0;
}
Status ListInsert(SqList* L, ElemType i, ElemType e) {//插入新的数据元素
    (*L).data[i - 1] = e;
    (*L).length = i;
    return OK;
}
Status ListEmpty(SqList L) {//判断列表是否为空
    if (L.length == 0)
        return OK;
    else
        return ERROR;
}
Status ListDelete(SqList* L, ElemType i, ElemType* e)//如果删除位置不存在,输出"Position Error\n"
{
    if (i <= 0 || i > (*L).length)
    {
        printf("Position Error\n");
        return ERROR;
    }
    int j;
    for (j = i - 1; j < (*L).length; j++)
    {
        (*L).data[j] = (*L).data[j + 1];//移动元素位置 向前移动
    }
    (*L).length--;
    return OK;
}
Status equal(ElemType a1, ElemType a2) {//判断两个数
    if (a1 == a2)
        return TRUE;
    else
        return FALSE;
}
Status input(SqList* L) {//输入过程中,0作为结束输入的标志数,不作为有效数 
    int  i = 0;//计数
    while (1)
    {
        scanf("%d", &(*L).data[i]);
        if ((*L).data[i] == 0)
            break;//若是最后一个0 跳出循环
        i++;
        /*
        * 列表长度函数单独处理
        i++;
        (*L).length = i;//列表的长度
        */
        
    }
    (*L).length = i;
    return OK;
}  
void empty(SqList* L) {
     memset(L,0,sizeof(SqList));
//    (*L).length = 0;
}//删除所有元素 

运行截图

工程实践:模块化程序设计(2)插图