76 lines
1.3 KiB
C++
76 lines
1.3 KiB
C++
#include <iostream>
|
|
using namespace std;
|
|
#include "LinkList.h"
|
|
template <class DT>
|
|
bool CreateOrderedList(LNode<DT>*& L, int n)
|
|
{
|
|
int i;
|
|
LNode<DT>* p, * s;
|
|
p = L;
|
|
cout << "依次输入" << n << "个数据元素:" << endl;
|
|
for (i = 1; i <= n; i++)
|
|
{
|
|
s = new LNode<DT>;
|
|
if (!s) return false;
|
|
cin >> s->data;
|
|
if (s->data >= p->data || p == NULL)
|
|
{
|
|
s->next = p->next;
|
|
p->next = s;
|
|
p = s;
|
|
}
|
|
else i--;
|
|
}
|
|
return true;
|
|
}
|
|
template <class DT>
|
|
void MergeTwoOrderedLists(LNode<DT>*& L1, LNode<DT>*& L2)
|
|
{
|
|
LNode<DT>* p, * q, * s;
|
|
p = L1->next;
|
|
q = L1->next->next;
|
|
s = L2->next;
|
|
while (!ListEmpty(L2))
|
|
{
|
|
if (q == NULL)
|
|
{
|
|
p->next = s;
|
|
return;
|
|
}
|
|
else if ((q->data) >= (s->data) && (p->data) <= (s->data))
|
|
{
|
|
LNode<DT>* t;
|
|
t = s->next;
|
|
s->next = q;
|
|
p->next = s;
|
|
s = t;
|
|
if (s == NULL) return;
|
|
p = p->next;
|
|
}
|
|
else
|
|
{
|
|
p = p->next;
|
|
q = q->next;
|
|
}
|
|
}
|
|
}
|
|
int main()
|
|
{
|
|
LNode<int>* L;
|
|
InitList(L);
|
|
int length;
|
|
cout << "请输入有序表的长度: ";
|
|
cin >> length;
|
|
CreateOrderedList(L, length);
|
|
DispList(L);
|
|
LNode<int>* L2;
|
|
InitList(L2);
|
|
int length2;
|
|
cout << "请输入另一个有序表的长度: ";
|
|
cin >> length2;
|
|
CreateOrderedList(L2, length2);
|
|
DispList(L2);
|
|
MergeTwoOrderedLists(L, L2);
|
|
DispList(L);
|
|
return 0;
|
|
} |