线性表合并

来源:互联网 发布:淘宝买东西非实名认证 编辑:程序博客网 时间:2024/05/21 07:00
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iomanip>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;    
typedef int ElemType;   
#define MAXSIZE 1000        
typedef struct{
    ElemType *elem;          
    int length;                                                                  
}SqList;
Status InitList_Sq(SqList &L){              
   
       L.elem=new ElemType[MAXSIZE];            
        if(!L.elem) exit(OVERFLOW);                             
        L.length=0;                           
    return OK ;
}
 
Status ListInsert_Sq(SqList &L,int i,ElemType e){   
    int j;                        
         for(j=L.length-1;j>=i-1;j--)                         
          L.elem[j+1]=L.elem[j];                         
        L.elem[i-1]=e;                               
        ++L.length;             
    return OK;
}
Status GetElem(SqList L,int i)
{
    int e;
    e=L.elem[i-1];
    return e;
}
 
int main()

    SqList La,Lb,Lc;
    InitList_Sq(La);
    InitList_Sq(Lb);
    InitList_Sq(Lc);    
    int i=0,j=0,k=0;
    int ai,bj;
    char ch;
    while((scanf("%d%c",&La.elem[i++],&ch)==2)&&ch!='\n');
    while((scanf("%d%c",&Lb.elem[j++],&ch)==2)&&ch!='\n');
    La.length=i, Lb.length=j;       
    i=j=1;
    while(i<= La.length&&j<=Lb.length) 
    {   
       ai=GetElem(La,i);
       bj=GetElem(Lb,j);
        if(ai<=bj)
        {
            ListInsert_Sq(Lc,++k,ai);
            ++i;
        }
        if(bj<ai)
        {
          ListInsert_Sq(Lc,++k,bj);
          ++j;
        }
    }
        while(i<=La.length)
        {
          ai=GetElem(La,i++);
          ListInsert_Sq(Lc,++k,ai);
        }
         while(j<=Lb.length)
        {
          bj=GetElem(Lb,j++);
          ListInsert_Sq(Lc,++k,bj);
        }
     
        for(i=0;i<k-1;i++)
        cout<<Lc.elem[i]<<" ";
        cout<<Lc.elem[k-1];
        return 0;
}
 
原创粉丝点击