/*
* @Author:
* @Date: 2019-12-03 20:18:46
* @Last Modified by:
* @Last Modified time: 2019-12-03 20:44:28
*/
#include <iostream>
#include <cstdio>
using namespace std;
#define MAXSIZE 100
typedef int Status;
typedef int Elem;
typedef struct
{
/* data */
Elem *date;
int len;
} SqList;
//初始化
Status InitList(SqList &L)
{
L.date = new Elem[MAXSIZE];
L.len = 0;
if (L.date)
return -1;
return 1;
}
//插入
Status InsertList(SqList &L, int i, Elem e)
{
if (i < 1 || i > L.len + 1)
return -1;
if (L.len == MAXSIZE)
return -1;
for (int j = L.len; j >= i - 1; j--)
L.date[j + 1] = L.date[j];
L.date[i - 1] = e;
++L.len;
return 1;
}
Status MergeList_Sq(SqList LA, SqList LB, SqList &LC)
{
LC.len = LA.len + LB.len;
LC.date = new Elem[LC.len];
Elem *pa, *pb, *pc;
Elem *pa_last, *pb_last;
pc = LC.date, pa = LA.date, pb = LB.date;
pa_last = LA.date + LA.len - 1;
pb_last = LB.date + LB.len - 1;
//逻辑上好理解 LA LB 一起找 小的就塞到LC 如果都其中一个空了 另一个就全塞进去
while (pa <= pa_last && pb <= pb_last)
{
if (*pa <= *pb) *pc++ = *pa++;
else *pc++ = *pb++;
}
while (pa <= pa_last) *pc++ = *pa++;
while (pb <= pb_last) *pc++ = *pb++;
return 1;
}
int main()
{
SqList LA, LB, LC;
InitList(LA);
InitList(LB);
InitList(LC);
int num_A, num_B;
cin >> num_A;
cin >> num_B;
for (int i = 1; i <= num_A; i++)
{
Elem OBJ;
cin >> OBJ;
//1
InsertList(LA, i, OBJ);
}
for (int i = 1; i <= num_B; i++)
{
Elem OBJ_B;
cin >> OBJ_B;
InsertList(LB, i, OBJ_B);
}
//1
MergeList_Sq(LA, LB, LC);
for (int i = 0; i < LC.len; i++)
cout << LC.date[i];
}
标签:LB,LC,LA,int,合并,len,有序,date,数据结构
From: https://blog.51cto.com/u_16014765/6132940