如下:
一道 c语言数据结构的题
算法就是求 B中不包含A的 插入A队列 然后输出A 想知道在哪里如何修正
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100
#define OVERFLOW 3
#define OK 1
#define ERROR 0
typedef struct
{//顺序表的存储结构
int* elem;//存储空间的基地址
int length;
}SqList;
int InitList(SqList& L) {
L.elem = new int[MAXSIZE];//为顺序表分配一个大小为MAXSIZE的数组空间
if (!L.elem)
exit(OVERFLOW);//存储分配失败退出
L.length = 0;
return OK;
}
int ListInsert(SqList& L, int i, int e) {
if ((i < 1) || i > L.length + 1)
return ERROR;
if (L.length == MAXSIZE)
return ERROR;
for (int j = L.length; j >= i; j--) {
L.elem[j ] = L.elem[j-1];
}
L.elem[i - 1] = e;
++L.length;
return OK;
}
int GetElem(SqList L, int i, int& e) {
if (i<1 || i>L.length)
return ERROR;
e = L.elem[i - 1];
return OK;
}
int LocateElem(SqList L, int e) {
for (int i = 0; i < L.length; i++) {
if (L.elem[i] == e)
return i + 1;
return 0;
}
}
void MergeList(SqList& La, SqList Lb) {
//将所有在线性表LB当不在线性表La中的数据元素插入到LA中
int m = La.length;
int n = Lb.length;
int e;
for (int i = 1; i <= n; i++) {
GetElem(Lb, i,e);
if (!LocateElem(La, e)) {
ListInsert(La, ++m, e);
}
}
}
int main() {
SqList A;
SqList B;
InitList(A);
InitList(B);
ListInsert(A,1,7);
ListInsert(A,2,5);
ListInsert(A,3,3);
ListInsert(A,4,11);
ListInsert(B,1,2);
ListInsert(B,2,6 );
ListInsert(B,3,3);
for (int i = 0; i < A.length; i++) {
printf("%d,", A.elem[i]);
}
printf("\n");
MergeList(A, B);
for (int i = 0; i < A.length; i++) {
printf("%d,", A.elem[i]);
}
}
*