Problem E: 【线性表】2-25 基于链表的两个非递减有序序列的合并

Problem E: 【线性表】2-25 基于链表的两个非递减有序序列的合并

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 70  Solved: 45
[Submit] [Status] [Web Board] [Creator:]

Description

给定两个非递减的整数序列A和B,利用链表表示序列A和B,将A和B合并为一个非递增的有序序列C,序列C允许有重复的数据。要求空间复杂度为O(1)。

Input

多组数据,每组数据有三行,第一行为序列A和B的长度n和m,第二行为序列A的n个元素,第三行为序列B的m个元素(元素之间用空格分隔)。n=0且m=0时输入结束。

Output

对于每组数据输出一行,为合并后的序列,每个数据之间用空格分隔。

Sample Input Copy

5 5
1 3 5 7 9
2 4 6 8 10 
5 6
1 2 2 3 5
2 4 6 8 10 12
0 0

Sample Output Copy

10 9 8 7 6 5 4 3 2 1
12 10 8 6 5 4 3 2 2 2 1

HINT

#include <iostream>
using namespace std;
typedef struct LNode
{
    int data;
    struct LNode *next;
}LNode,*linkList;
void CreateList_R(linkList &L,int n)
{//后插法创建单链表
    L=new LNode;
    L->next=NULL;
    linkList r=L;
    for(int i=0;i<n;i++)
    {
        linkList p=new LNode;
        cin>>p->data;
        p->next=NULL;
        r->next=p;
        r=p;
    }
}
void PrintList(linkList &L)
{//打印依次输出链表中的数据
    L=L->next;
    while(L){
        if(L->next!=NULL) cout<<L->data<<" ";
        else cout<<L->data;
        L=L->next;
    }
    cout<<endl;
}
void MergeList(linkList &LA,linkList &LB,linkList &LC)
{//求基于链表的两个非递减有序序列的合并
/**************begin************/







    /**************end************/
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        if(n==0&&m==0) break;
        linkList LA,LB,LC;
        CreateList_R(LA,n);
        CreateList_R(LB,m);
        MergeList(LA,LB,LC);
        PrintList(LC);
    }
    return 0;
}