早教吧 育儿知识 作业答案 考试题库 百科 知识分享

数据结构算法实现:利用两个线性表LA和LB分别表示两个集合A和B,现要求一个新的集合A=A并B.利用两个线性表LA和LB分别表示两个集合A和B,现要求一个新的集合A=A并B.算法是voidunion(List&La,ListLb

题目详情
数据结构算法实现:利用两个线性表LA和LB分别表示两个集合A和B,现要求一个新的集合A=A并B.
利用两个线性表LA和LB分别表示两个集合A和B,现要求一个新的集合A=A并B.
算法是
void union(List &La,List Lb){
La_len=ListLength(La);
Lb_len=ListLength(Lb);
for(i=1;i
▼优质解答
答案和解析
#include
#include
#include
typedef int status;
typedef int ElemType;
#define TRUE 1
#define ERROR 0
#define FALSE 0
#define OK 1
#define OVERFLOW -2
#define list_init_size 100
#define listincrement 10
typedef struct{
\x05ElemType *elem;
\x05int length;
\x05int listsize;
}sqlist;
status equal(ElemType a,ElemType b)
{if(a==b)
return TRUE;
else
return FALSE;
}
int listlength(sqlist l)
{ return l.length;}
status listinsert(sqlist *l,int i,ElemType e)
{
\x05ElemType *newbase,*q,*p;
\x05if(i(*l).length+1)
\x05return ERROR;
\x05if((*l).length>=(*l).listsize){
\x05\x05newbase=(ElemType*)realloc((*l).elem,((*l).listsize+listincrement)*sizeof(ElemType));
if(!newbase) exit(OVERFLOW);
\x05 (*l).elem=newbase;
\x05 (*l).listsize+=listincrement;
}
q=&((*l).elem[i-1]);
for(p=&((*l).elem[(*l).length-1]);p>=q;--p)
\x05 *(p+1)=*p;
*q=e;
++(*l).length;
return OK;
}
status initlist(sqlist *l){
\x05(*l).elem=(ElemType*)malloc(list_init_size*sizeof(ElemType));
\x05if(!(*l).elem)
\x05\x05exit(OVERFLOW);
\x05(*l).length=0;
\x05(*l).listsize=list_init_size;
\x05return OK;
}
status getelem(sqlist l,int i,ElemType *e)
{ if(il.length)
exit(ERROR);
*e=*(l.elem+i-1);
return OK;
}
int LocateElem(sqlist L,ElemType e,status(*compare)(ElemType,ElemType))
{
ElemType *p;
int i=1;
p=L.elem;
while(i