线性表--算法设计题2.25

假设以两个元素依值递增有序排列的线性表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 ;

} 

转载于:https://www.cnblogs.com/cpoint/archive/2011/04/28/2032318.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值