C#数据结构算法 合链两个链表并去除链表中的重复数据

 

合链两个链表并去除链表中的重复数据

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using DSList;//这个是自己写的算法生成的引用

namespace _20100701

{

    class Program

    {

        static void Main(string[] args)

        {

            LinkList<int> LLa = new LinkList<int>();

            LinkList<int> LLb = new LinkList<int>();

            LinkList<int> LLc = new LinkList<int>();

           

            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<int> Merge(LinkList<int> LLa, LinkList<int> LLb)//LLa,LLb链表合并后存入LLc升序

        {

            LinkList<int> LLc = new LinkList<int>();

            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<int> list)//显示数据

        {

            StringBuilder strBuilder = new StringBuilder();

            Node<int> currentNode = list.Head;

            while (currentNode != null)

            {

 

                strBuilder.Append(currentNode.Data+" ");

                currentNode = currentNode.Next;

              

            }

            Console.WriteLine(strBuilder.ToString());

        }

        static void delRepeat(LinkList<int> list)//去除链表中重复数据

        {

            Node<int> 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;

            }

          

 

        }

    }

   

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值