用C#实现单链表(merge两个有序单链表)

初学C#记录历程,记录心情。

LinkNode 类:

View Code
 1 public class LinkNode<T>
 2     {
 3        private LinkNode<T> next;  //指向下一个结点对象 字段
 4        private T data;            //结点的数据  字段
 5  
 6         /// <summary>
 7        ///  结点的数据 属性
 8         /// </summary>
 9         public T Data
10         {
11             get { return this.data; }
12             set { this.data = value; }
13         }
14  
15         /// <summary>
16         /// 指向下一个结点对象 属性
17         /// </summary>
18         public LinkNode<T> Next           
19         {
20             get { return this.next; }
21             set { this.next = value; }
22         }          
23 
24         public LinkNode()
25         {
26             this.data = default(T);
27             this.next = null;
28         }
29 
30         public LinkNode(T t)
31         {
32             this.data = t;
33             this.next = null;
34         }
35 
36         public LinkNode(T t, LinkNode<T> node)
37         {
38             this.data = t;
39             this.next = node;
40         }
41        
42     }

LinkList 类,包括add()方法, merge方法

View Code
 1 //定义链表类,继承接口IComparer,用来实现比较器
 2         public class LinkList<T> : IComparer<LinkNode<T>>
 3         {
 4             private LinkNode<T> head;    //单链表的表头
 5             public LinkNode<T> Head
 6             {
 7                 get { return this.head; }
 8                 set { this.head = value; }
 9             }
10 
11             public LinkList()
12             {
13                 this.head = null;
14             }
15 
16             public LinkList(LinkNode<T> node)
17             {
18                 this.head = node;
19             }
20 
21 #region Add 方法
22            /// <summary>
23                 /// 添加数据到链表尾部
24            /// </summary>
25                 /// <param name="item">要添加的结点的数据</param>
26                 public void Add(T item)
27                 {
28                     LinkNode<T> AddedNode = new LinkNode<T>(item, null);  //创建一个结点
29                     if (this.head == null)
30                     {
31                         this.head = AddedNode;
32 
33                     }
34                     else
35                     {
36                         LinkNode<T> node = this.head;
37                         while (node.Next != null)
38                         {
39                             node = node.Next;
40                         }
41                         node.Next = AddedNode;
42                     }
43                 }
44 #endregion
45 
46          //比较器
47       public int Compare(LinkNode<T> x, LinkNode<T> y)
48             {
49                 return Comparer<T>.Default.Compare(x.Data, y.Data);
50             }
51 
52             /// <summary>
53             /// merge 两个升序单链表到一个新的有序单链表(采用尾插法) 
54             /// </summary>
55             /// <param name="La">链表a</param>
56             /// <param name="Lb">链表b</param>
57             /// <returns>返回新链表c</returns>
58             public LinkList<T> MergeList(LinkList<T> La, LinkList<T> Lb)
59             {
60                 LinkList<T> Lc = new LinkList<T>();
61                 LinkNode<T> nodec = new LinkNode<T>();
62                 LinkNode<T> nodea = La.head;
63                 LinkNode<T> nodeb = Lb.head;
64                 Lc.head = nodec = La.head;
65                 while (nodea != null && nodeb != null)
66                 {
67                     if (this.Compare(nodea, nodeb) < 0)
68                     {
69                         nodec = nodea;
70                         nodea = nodea.Next;
71                     }
72                     else
73                     {
74                         nodec = nodeb;
75                         nodeb = nodeb.Next;
76                     }
77 
78                     this.Add(nodec.Data);
79                  }
80 
81                 if(nodea==null)
82                 {
83                     nodea = nodeb;
84                 }
85 
86                 while(nodea!=null)
87                 {
88                     nodec = nodea;
89                     nodea = nodea.Next;
90                     this.Add(nodec.Data);
91                 }
92                 return Lc;
93      
94             }
95 
96         }


在LinkList类中再加入create()方法,见前篇。

测试是否通过:

注意:创建单链表采用头插法,因此从键盘输入升序序列时要逆序输入。

View Code
 1  namespace testing01
 2         {
 3             class Program
 4             {
 5                 static LinkList<int> MyListA = new LinkList<int>();
 6                 static LinkList<int> MyListB = new LinkList<int>();
 7                 static LinkList<int> MyListC = new LinkList<int>();
 8                 static LinkNode<int> node;
 9 
10 
11                 static void Main(string[] args)
12                 {
13                     //创建升序单链表a
14                     Console.Write("Pleae input the lenght of your ListLinkA,nA= ");
15                     string strA = Console.ReadLine();
16                     int nA;
17                     nA= Convert.ToInt32(strA);
18                     MyListA.CreateListHead(nA);
19 
20                     //创建升序单链表b
21                     Console.Write("Pleae input the lenght of your ListLinkB,nB= ");
22                     string strB = Console.ReadLine();
23                     int nB;
24                     nB = Convert.ToInt32(strB);
25                     MyListB.CreateListHead(nB);
26 
27                     MyListC.MergeList(MyListA, MyListB); //merge
28 
29                     //输出新链表c
30                     node = MyListC.Head;
31                     while (node != null)
32                     {
33                         Console.WriteLine("The data of ListC are:{0}", node.Data);
34                         node = node.Next;
35                     }
36                 
37                     Console.ReadLine();
38                 }
39             
40             }
41     
42         }
原文地址:https://www.cnblogs.com/bloomalone/p/2936454.html