#include<iostream>
using namespace std;
typedef struct LNode
{
int data;
struct LNode* next;
}LNode, * LinkList;
int CreateList_T(LinkList& L, int n)//尾插
{
LNode* p,*q;
L = new LNode;
L->next = NULL;
q = L;
cout << "请输入数据:";
for (int i = 0; i < n; i++)
{
p = new LNode;
cin >> p->data;
q->next = p;
q = p;
}
return OK;
}
int ConnectList(LinkList& L1, LinkList& L2)
{
LNode* p , * q ,*r = L1;
p = new LNode; q = new LNode;
p = L1->next; q = L2->next;
while (p && q)
{
if (p->data<q->data)
{
r->next = p;
r = r->next;
p = p->next;
continue;
}
if (p->data == q->data)
{
r->next = p;
r = r->next;
p = p->next;
q = q->next;
continue;
}
if (p->data > q->data)
{
r->next = q;
r = r->next;
q = q->next;
continue;
}
}
r->next = p ? p : q;
return OK;
}
int DispList(LinkList& L, int n)
{
LNode* p;
p = new LNode;
p = L;
cout << "输出数据:";
for (int j = 0; j < n; j++)
{
p = p->next;
cout << p->data << " ";
}
return OK;
}
int main()
{
LinkList L3,L4;
cout << "合并两个有序单链表:"<<endl;
cout << "输入有序表L3:";
CreateList_T(L3, 5);
cout << "输入有序表L4:";
CreateList_T(L4, 4);
ConnectList(L3, L4);
cout << "两表递减合并:";
DispList(L3, 9);
}
