合链两个链表并去除链表中的重复数据
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingDSList;//这个是自己写的算法生成的引用
namespace_20100701
{
class Program
{
static void Main(string[] args)
{
LinkList LLa = new LinkList();
LinkList LLb = new LinkList();
LinkList LLc = new LinkList();
LLa.Append(9);
LLa.Append(10);
LLa.Append(5);
LLa.Append(4);
LLa.Append(1);
Console.WriteLine("LLa中的数据:");
showDatas(LLa);
LLb.Append(15);
LLb.Append(13);
LLb.Append(10);
LLb.Append(10);
LLb.Append(4);
LLb.Append(2);
Console.WriteLine("LLb中的数据:");
showDatas(LLb );
LLc= Merge(LLa, LLb);
Console.WriteLine("合并后的数据:");
showDatas(LLc);
delRepeat(LLc);
Console.WriteLine("去重后的数据:");
showDatas(LLc);
Console.ReadKey();
}
static LinkList Merge(LinkList LLa, LinkList LLb)//将LLa,LLb链表合并后存入LLc升序
{
LinkList LLc = new LinkList();
int i=LLa.GetLength()-1;
int j=LLb.GetLength()-1;
int k=0;
while (i >= 0 && j >= 0)
{
if (LLa.GetElem(i)< LLb.GetElem(j))
{
LLc.Append(LLa.GetElem(i--));
k++;
}
else
{
LLc.Append(LLb.GetElem(j--));
k++;
}
}
while (i+1> 0)
{
LLc.Append(LLa.GetElem(i--));
}
while (j+1 > 0)
{
LLc.Append(LLb.GetElem(j--));
}
return LLc;
}
static void showDatas(LinkList list)//显示数据
{
StringBuilder strBuilder = new StringBuilder();
Node currentNode = list.Head;
while (currentNode != null)
{
strBuilder.Append(currentNode.Data+" ");
currentNode = currentNode.Next;
}
Console.WriteLine(strBuilder.ToString());
}
static void delRepeat(LinkList list)//去除链表中重复数据
{
Node current = list.Head;
while (current!= null)
{
while (current.Next != null)
{
if (current.Data ==current.Next.Data)
{
int i = list.Locate(current.Next.Data);
list.Delete(i);
}
current = current.Next;
}
current = current.Next;
}
}
}
}