顺序表的归并

 
#include<stdio.h>
 #include<conio.h>
 #include<stdlib.h>
 #define List_init_size 50
 #define Listincrement 5
 
 struct SqList
    {
      int *elem;
      int length;
      int listsize;}
     
     
      InitSq(struct SqList *L)
     {    int i,e;
      L->elem=(int *)malloc(List_init_size*sizeof(int));
      if(!L->elem)
      printf("Overflow");
      else
      {
       L->length=0;
       L->listsize=List_init_size;
       printf("/nInit ok/n");
 
      }
 
     }
 
 
 
void Listinsert(struct SqList *L)
{
    int e,i=0;
   if(L->length>=List_init_size )
     L->elem=(int *)realloc(L->elem,List_init_size*sizeof(int));
      if(!L->elem)
      printf("Overflow");
      else
    do
      {
           scanf("%d",&e);
           L->elem[i++]=e;
            
           L->length++;
        }
       while(e);
}
 
 
 
void delete(struct SqList *L)
 {
      struct SqList *p;
      p=&(L->elem[L->length-1]);
      p--;
      L->length--;
 }
 
 
 MergeList(struct SqList *L1,struct SqList *L2,struct SqList *L3)
  {
       int i,j,k=0;
       int La_len,Lb_len;
       La_len=L1->length;
       Lb_len=L2->length;
       for(i=0,j=0;(i<=La_len-1)&&(j<=Lb_len-1);k++,L3->length++)
       {
              if(L1->elem[i]<=L2->elem[j])
              {
                L3->elem[k]=L1->elem[i];
                i++;
              }
              else
              {
                  L3->elem[k]=L2->elem[j];
                  j++;
              }
               
       }
 
 
   
       while(i<=La_len-1)
       { 
     L3->elem[k]=L1->elem[i];
       k++;
     i++;
     L3->length++;
   }
   
       while(j<=Lb_len-1)
     {
      L3->elem[k]=L2->elem[j];
      k++;
             j++;
             L3->length++;
     }
     
}
         
 
 void print(struct SqList *L)
       {
           int i,*q;
           printf("/nOutput the List/n");
             for(i=0;i<L->length;i++)
            { q=&(L->elem[i]);
                printf("%d ",*q);
             }
       }
 
 
 
 
   main()
       {   
           struct SqList L1,L2,L3;
           clrscr();
           InitSq(&L1);
           printf("Please enter numbers for List1 and end by 0/n");
           Listinsert(&L1);
           delete(&L1);  
         print(&L1);
         InitSq(&L2);
         printf("Please enter numbers for List2 and end by 0/n");
         Listinsert(&L2);
         delete(&L2);  
       print(&L2);
          InitSq(&L3);
          MergeList(&L1,&L2,&L3);
          print(&L3);}
         
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值