假设以两个元素依值递增有序排列的线性表A和B分别表示两个集合(即同一表中的元素值各不相同),现要求另辟空间构成一个线性表C,其元素为A和B中元素的交集,且表C中的元素有依值递增有序排列。试对顺序表编写求C的算法。
C code:
#include
<
stdio.h
>
#include < stdlib.h >
#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 2
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define TRUE 1
typedef int Status;
struct SqList
{
int * elem;
int length;
int listsize;
};
void InitList(SqList & L)
{
L.elem = ( int * )malloc(LIST_INIT_SIZE * sizeof ( int ));
if ( ! L.elem)
exit(OVERFLOW);
L.length = 0 ;
L.listsize = LIST_INIT_SIZE;
}
Status ListInsert(SqList & L, int i, int e)
{
int * newbase, * q, * p;
if (i < 1 || i > L.length + 1 )
return ERROR;
if (L.length == L.listsize)
{
newbase = ( int * )realloc(L.elem,(L.listsize + LIST_INCREMENT) * sizeof ( int ));
if ( ! newbase)
exit(OVERFLOW);
L.elem = newbase;
L.listsize += LIST_INCREMENT;
}
q = L.elem + i - 1 ;
for (p = L.elem + L.length - 1 ; p >= q; -- p)
* (p + 1 ) =* p;
* q = e;
++ L.length;
return OK;
}
void CreateList(SqList & L, int len)
{
int i,t;
printf( " Please input elems: " );
for (i = 1 ; i <= len; i ++ )
{
scanf( " %d " , & t);
ListInsert(L,i,t);
}
}
void ListTraverse(SqList L, void ( * vist)( int & ))
{
int * p = L.elem;
int i;
for (i = 1 ; i <= L.length; i ++ )
{
vist( * p ++ );
}
printf( " \n " );
}
void print1( int & c)
{
printf( " %d " ,c);
}
int cmp( const void * a, const void * b)
{
return * ( int * )a - * ( int * )b;
}
void ListSort(SqList & L)
{
qsort(L.elem,L.length, sizeof ( int ),cmp);
}
void CreateList_C(SqList La,SqList Lb,SqList & Lc)
{
int i = 0 ,j = 0 ,k = 1 ;
while (i < La.length && j < Lb.length)
{
if (La.elem[i] < Lb.elem[j])
i ++ ;
else
{
if (La.elem[i] > Lb.elem[j])
j ++ ;
else
{
ListInsert(Lc,k,La.elem[i]);
i ++ ;
k ++ ;
}
}
}
}
int main()
{
SqList La,Lb,Lc;
InitList(La);
InitList(Lb);
InitList(Lc);
int La_len,Lb_len;
printf( " input length of La: " );
scanf( " %d " , & La_len);
printf( " input length of Lb: " );
scanf( " %d " , & Lb_len);
CreateList(La,La_len);
CreateList(Lb,Lb_len);
printf( " List a: " );
ListTraverse(La,print1);
printf( " List b: " );
ListTraverse(Lb,print1);
printf( " After sorting List a: " );
ListSort(La);
ListTraverse(La,print1);
printf( " After sorting List b: " );
ListSort(Lb);
ListTraverse(Lb,print1);
CreateList_C(La,Lb,Lc);
printf( " List c: " );
ListTraverse(Lc,print1);
return 0 ;
#include < stdlib.h >
#define LIST_INIT_SIZE 10
#define LIST_INCREMENT 2
#define ERROR 0
#define OK 1
#define OVERFLOW -1
#define TRUE 1
typedef int Status;
struct SqList
{
int * elem;
int length;
int listsize;
};
void InitList(SqList & L)
{
L.elem = ( int * )malloc(LIST_INIT_SIZE * sizeof ( int ));
if ( ! L.elem)
exit(OVERFLOW);
L.length = 0 ;
L.listsize = LIST_INIT_SIZE;
}
Status ListInsert(SqList & L, int i, int e)
{
int * newbase, * q, * p;
if (i < 1 || i > L.length + 1 )
return ERROR;
if (L.length == L.listsize)
{
newbase = ( int * )realloc(L.elem,(L.listsize + LIST_INCREMENT) * sizeof ( int ));
if ( ! newbase)
exit(OVERFLOW);
L.elem = newbase;
L.listsize += LIST_INCREMENT;
}
q = L.elem + i - 1 ;
for (p = L.elem + L.length - 1 ; p >= q; -- p)
* (p + 1 ) =* p;
* q = e;
++ L.length;
return OK;
}
void CreateList(SqList & L, int len)
{
int i,t;
printf( " Please input elems: " );
for (i = 1 ; i <= len; i ++ )
{
scanf( " %d " , & t);
ListInsert(L,i,t);
}
}
void ListTraverse(SqList L, void ( * vist)( int & ))
{
int * p = L.elem;
int i;
for (i = 1 ; i <= L.length; i ++ )
{
vist( * p ++ );
}
printf( " \n " );
}
void print1( int & c)
{
printf( " %d " ,c);
}
int cmp( const void * a, const void * b)
{
return * ( int * )a - * ( int * )b;
}
void ListSort(SqList & L)
{
qsort(L.elem,L.length, sizeof ( int ),cmp);
}
void CreateList_C(SqList La,SqList Lb,SqList & Lc)
{
int i = 0 ,j = 0 ,k = 1 ;
while (i < La.length && j < Lb.length)
{
if (La.elem[i] < Lb.elem[j])
i ++ ;
else
{
if (La.elem[i] > Lb.elem[j])
j ++ ;
else
{
ListInsert(Lc,k,La.elem[i]);
i ++ ;
k ++ ;
}
}
}
}
int main()
{
SqList La,Lb,Lc;
InitList(La);
InitList(Lb);
InitList(Lc);
int La_len,Lb_len;
printf( " input length of La: " );
scanf( " %d " , & La_len);
printf( " input length of Lb: " );
scanf( " %d " , & Lb_len);
CreateList(La,La_len);
CreateList(Lb,Lb_len);
printf( " List a: " );
ListTraverse(La,print1);
printf( " List b: " );
ListTraverse(Lb,print1);
printf( " After sorting List a: " );
ListSort(La);
ListTraverse(La,print1);
printf( " After sorting List b: " );
ListSort(Lb);
ListTraverse(Lb,print1);
CreateList_C(La,Lb,Lc);
printf( " List c: " );
ListTraverse(Lc,print1);
return 0 ;
}