[小算法] 合并两有序链表
2012-04-23 13:30 Kevin Pan 阅读(142) 评论(0) 编辑 收藏 举报
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("data.txt");
struct Link
{
int Data;
Link *Next;
};
Link MergeLinks(Link *linka, Link *linkb);
void PrintLink(Link * head);
int _tmain(int argc, _TCHAR* argv[])
{
Link linka, linkb, *pointera,*pointerb;
linka.Data = 0, linkb.Data = 0, linka.Next = NULL, linkb.Next = NULL;
pointera = &linka, pointerb = &linkb;
Link *newNode;
//build two links according the data.txt file
while(true)
{
int data = 0;
fin >> data;
if(data != -1)
{
newNode = new Link();
newNode->Data = data;
newNode->Next = NULL;
pointera->Next = newNode;
pointera = pointera->Next;
}
else
{
break;
}
}
cout << "Link A: ";
PrintLink(&linka);
cout << endl;
while(true)
{
int data = 0;
fin >> data;
if(data != -1)
{
newNode = new Link();
newNode->Data = data;
newNode->Next = NULL;
pointerb->Next = newNode;
pointerb = pointerb->Next;
}
else
{
break;
}
}
cout << "Link B: ";
PrintLink(&linkb);
cout << endl;
Link *result = &MergeLinks(&linka, &linkb);
PrintLink(result);
cout << endl;
system("pause");
return 0;
}
//merge the two links, destory the original links
Link MergeLinks(Link *linka, Link *linkb)
{
Link * pointera, * pointerb, *temp;
pointera = linka;
pointerb = linkb;
while(pointera->Next != NULL && pointerb->Next != NULL)
{
if(pointera->Next->Data > pointerb->Next->Data)
{
temp = pointera->Next;
pointera->Next = pointerb->Next;
pointerb->Next = pointerb->Next->Next;
pointera->Next->Next = temp;
}
else
{
pointera = pointera->Next;
}
}
if(pointerb->Next != NULL)
{
pointera->Next = pointerb ->Next;
}
return *linka;
}
void PrintLink(Link * head)
{
Link *pointer = head->Next;
while(pointer != NULL)
{
cout << pointer->Data << " ";
pointer = pointer->Next;
}
}
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("data.txt");
struct Link
{
int Data;
Link *Next;
};
Link MergeLinks(Link *linka, Link *linkb);
void PrintLink(Link * head);
int _tmain(int argc, _TCHAR* argv[])
{
Link linka, linkb, *pointera,*pointerb;
linka.Data = 0, linkb.Data = 0, linka.Next = NULL, linkb.Next = NULL;
pointera = &linka, pointerb = &linkb;
Link *newNode;
//build two links according the data.txt file
while(true)
{
int data = 0;
fin >> data;
if(data != -1)
{
newNode = new Link();
newNode->Data = data;
newNode->Next = NULL;
pointera->Next = newNode;
pointera = pointera->Next;
}
else
{
break;
}
}
cout << "Link A: ";
PrintLink(&linka);
cout << endl;
while(true)
{
int data = 0;
fin >> data;
if(data != -1)
{
newNode = new Link();
newNode->Data = data;
newNode->Next = NULL;
pointerb->Next = newNode;
pointerb = pointerb->Next;
}
else
{
break;
}
}
cout << "Link B: ";
PrintLink(&linkb);
cout << endl;
Link *result = &MergeLinks(&linka, &linkb);
PrintLink(result);
cout << endl;
system("pause");
return 0;
}
//merge the two links, destory the original links
Link MergeLinks(Link *linka, Link *linkb)
{
Link * pointera, * pointerb, *temp;
pointera = linka;
pointerb = linkb;
while(pointera->Next != NULL && pointerb->Next != NULL)
{
if(pointera->Next->Data > pointerb->Next->Data)
{
temp = pointera->Next;
pointera->Next = pointerb->Next;
pointerb->Next = pointerb->Next->Next;
pointera->Next->Next = temp;
}
else
{
pointera = pointera->Next;
}
}
if(pointerb->Next != NULL)
{
pointera->Next = pointerb ->Next;
}
return *linka;
}
void PrintLink(Link * head)
{
Link *pointer = head->Next;
while(pointer != NULL)
{
cout << pointer->Data << " ";
pointer = pointer->Next;
}
}
data.txt内容如下:
1 3 6 8 10 50 215 -1
2 4 6 7 9 12 123 216 217 225 226 -1