两个链表要求已经排好序,这儿采用的是比较愚蠢的方法,创建了一个新的链表,然后一个一个的插入。
特别要注意的是,传递的函数参数究竟应该是引用还是值传递,尤其是在有动态资源申请的时候,一定要谨慎,否则可能造成灾难性的后果。
效率高的应该是直接在原来的链表上改。
#ifndef _LISTH
#define _LISTH
class ChainNode;
class Chain;
void MergeList(Chain &La, Chain &Lb, Chain &Lc);
class Chain
{
public:
Chain()
{
first = 0;
}
~Chain();
public:
int LenChain() const;
bool GetElem(int i, int &e);
void InsertNode(int i, int &e);
void OutputChain() const;
friend void MergeList(Chain &La, Chain &Lb, Chain &Lc);
private:
ChainNode *first;
};
class ChainNode
{
public:
ChainNode() { }
~ChainNode() { }
friend Chain;
private:
int data;
ChainNode *link;
};
#endif
//头文件的实现文件
int Chain::LenChain() const //求链表的长度
{
ChainNode *current = first;
int len = 0;
while (current)
{
current = current->link;
++len;
}
return len;
}
bool Chain::GetElem( int i, int &e) //从链表中找到第i个元素,将它的值赋给e
{
if ( i < 0)
{
cout << "The length of the Chain is at least 1 !" << endl;
return false;
}
ChainNode *current = first;
int index = 1;
while ( index < i && current )
{
current = current->link;
++index;
}
if (current)
{
e = current->data;
}
return true;
}
void Chain::InsertNode(int i, int &e)//在链表chain中的i位置插入元素e
{
if ( i < 0 )
{
throw("The length of the Chain is at least 1 !");
}
ChainNode *current = first;
for (int index = 0; index < i-1 && current; ++index)
{
current = current->link;
}
if ( i > 0 && !current)
{
throw("The element is not exit in the chain!");
}
ChainNode *y = new ChainNode;
y->data = e;
if ( i )
{
y->link = current->link;
current->link = y;
}
else
{
y->link = first;
first = y;
}
}
void Chain::OutputChain() const
{
ChainNode *current;
for (current = first; current; current = current->link)
{
cout << current->data <<" ";
}
}
Chain::~Chain()
{
ChainNode *next;
while(first)
{
next = first->link;
delete first;
first = next;
}
}
void MergeList(Chain &La, Chain& Lb, Chain &Lc) //归并算法
{
int i = 1;
int j = 1;
int k = 0;
int lenla = La.LenChain();
int lenlb = Lb.LenChain();
int ai;
int bj;
while( (i <= lenla) && (j <= lenlb) )
{
La.GetElem(i, ai);
Lb.GetElem(j, bj);
if ( ai <= bj )
{
Lc.InsertNode( k++, ai);
++i;
}
else
{
Lc.InsertNode( k++, bj);
++j;
}
}
while ( i <= La.LenChain() )
{
La.GetElem( i++, ai);
Lc.InsertNode( k++, ai);
}
while ( j <= Lb.LenChain() )
{
Lb.GetElem( j++, bj);
Lc.InsertNode( k++, bj);
}
}
//主文件
// DSList.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "list.h"
#include <vector>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
Chain achain;
Chain bchain;
Chain mer_chain;
int len_a = 4;
int len_b = 3;
cout << "Please input the number of Chain a: "<<endl;
int temp;
for( int index = 0; index < len_a; ++index)
{
cin >> temp;
achain.InsertNode( index, temp);
}
cout << "Please input the number of Chain b: "<<endl;
for( int index = 0; index < len_b; ++index)
{
cin >> temp;
bchain.InsertNode( index, temp);
}
MergeList( achain, bchain, mer_chain);
// cout << "Length of achain: " << achain.LenChain()<<endl;
//cout << "Length of bchain: " << bchain.LenChain()<<endl;
cout << "After merged: " << endl;
// achain.OutputChain();
// bchain.OutputChain();
mer_chain.OutputChain();
return 0;
}