#include
#include
#include
#define MaxSize 100
#define ElemType int
#define Status int
using namespace std;
typedef struct node
{
ElemType data;
node* next;
}node, * LinkList;
Status InitList(LinkList& L)
{
L = new node;
L->next = NULL;
return 1;
}
int ListLength(LinkList& L)
{
LinkList p = L;
int sum = 0;
while (p)
{
sum++;
p = p->next;
}
return sum - 1;
}
void CreatList(LinkList& L)
{
int i = 0; int choice;
int a[MaxSize];
ifstream infile;
cout << "请选择要给链表赋值的文件(test1 or test2):";
cin >> choice;
switch (choice)
{
case 1:infile.open("Test1.txt"); break;
case 2:infile.open("Test2.txt"); break;
default:cout << "输入错误"; break;
}
while (infile.good())
{
infile >> a[i];
i++;
}
infile.close();
sort(a, a + i);
LinkList head = new node;
head->next = NULL;
node* tail = new node;
head=tail;
for (int n = 0; n < i; n++)
{
node* p = new node;
p->data = a[n];
p->next = NULL;
tail->next = p;
tail = tail->next;
}
node* t = new node;
t = head;
int f = 0;
cout << "链表输出为:";
while (t->next != NULL)
{
if (f == 0)
{
cout << t->next->data;
f++;
t = t->next;
}
else
{
cout << " " << t->next->data;
f++;
t = t->next;
}
}
L = head;
cout << endl;
}
void Display(LinkList L)
{
if (L == NULL) return;
LinkList p = L->next;
while (p != NULL)
{
cout << p->data << " ";
p = p->next;
}
}
void CombineList(LinkList L1, LinkList L2,LinkList L3)
{
node* p = new node;
node* q = new node;
node* r = new node;
p = L1->next;
q = L2->next;
r = L3;
L3 = L1;
while (p&&q)
{
if (p->data < q->data)
{
r->next = q;
r = q;
q = q->next;
}
else
{
r->next = p;
r = p;
p->next = p;
}
}
r->next = p ? p : q;
delete L2;
cout << "合并后的链表为:";
}
int main()
{
LinkList LA, LB,LC;
InitList(LA);
InitList(LB);
InitList(LC);
CreatList(LA);
CreatList(LB);
CombineList(LA, LB,LC);
Display(LC);
return 0;
}