用C#实现单链表(插入,在第i个前插,在第i个后插)

初学C#记录历程,记录心情。
 
在学习的过程中严重参考了前辈们在网上的分享,当然也不忘有自己的细微思想在里面,写在这里主要是对自己由不懂到能独立写下来的纪念。如有冒犯,还请原谅。

在接口IList中增加:

      void Insert(T item, int i);//在i后插
      void InsertBefore(T item, int i);//在i前插 

节点类和链表类参考前篇。

在链表类LinkList里面直接增加方法。

在第i个数前面插入:

View Code
 1                 /// <summary>
 2                 /// 插入数据到链表(i前插)
 3                 /// </summary>
 4                 /// <param name="item">插入的结点的数据</param>
 5                 /// <param name="i">在第i个位置前插入结点</param>
 6 
 7                 public void InsertBefore(T item, int i)//i前插
 8                 {
 9                     LinkNode<T> node = this.head;
10                     LinkNode<T> nodeTemp;           //临时结点
11                     LinkNode<T> InsertNode = new LinkNode<T>(item, null);   //创建一个要插入的结点
12                     if (this.head == null && i == 1)      //空链表时,插入
13                     {
14                         this.head = InsertNode;
15                         return;
16                     }
17                     if (i < 1 || i > this.GetLength() || (this.head == null && i != 1)) //异常下的处理
18                     {
19                         Console.WriteLine("Error,the location you want to insert in is wrong");
20                         return;
21                     }
22 
23                    if(this.head!=null&&i==1)   //在第一个结点前插入
24                     {
25             
26                         this.head=InsertNode;
27                         InsertNode.Next =node;
28                         return;
29                     }
30 
31                    //下面代码才是主要的,在第i个位置前面插入
32                     int j = 1;
33                     while(node!=null&j<i-1)   //寻找第i-1个结点. i>=2
34                     {
35                         node = node.Next;
36                         j++;
37                     }
38                     nodeTemp = node.Next;
39                     node.Next = InsertNode;
40                     InsertNode.Next = nodeTemp;
41             
42                 } 

在第i个数后面插入:

View Code
 1                 /// <summary>
 2                 /// 插入数据到链表(i后插)
 3                 /// </summary>
 4                 /// <param name="item">要插入的元素</param>
 5                 /// <param name="i">在第i个位置的后面插入新节点</param>
 6                 public void Insert(T item, int i)
 7                 {
 8                     LinkNode<T> node=this.head;
 9                     LinkNode<T> nodeTemp;          //临时结点
10                     LinkNode<T> insertNode=new LinkNode<T>(item,null);   //创建一个要插入的结点
11 
12                     if (this.head == null && i == 1)     //空链表时,插入
13                     {
14                         this.head = insertNode;
15                         return;
16                     }
17             
18                     if (i < 1 || i > this.GetLength()||(this.head==null&&i!=1))     //异常下的处理
19                     {
20                         Console.WriteLine("Error,the location you want to insert in is wrong");
21                         return;
22                     }
23            
24                      if (i == this.GetLength())  //如果是在末尾插入, i〉=1
25                     {
26                         while (node.Next != null)
27                         {
28                             node = node.Next;
29                         }
30                         node.Next = insertNode;
31                         return;
32                     }
33                    
34                     //下面代码才是主要的,在第i个位置后插入
35                         int j = 1;
36                         while (node != null && j < i)//寻找第i个结点. i>=2
37                         {
38                             node = node.Next;
39                             j++;
40                         }
41                         nodeTemp = node.Next;
42                         node.Next = insertNode;
43                         insertNode.Next = nodeTemp;
44             
45                 }

验证插入是否正确:

View Code
 1  static void Main(string[] args)
 2         {
 3                LinkList<int> MyList = new LinkList<int>();
 4                LinkNode<int> node = new LinkNode<int>();
 5                 LinkList<int>.AddPosition HeadInsert = LinkList<int>.AddPosition.Head;
 6 
 7               //验证插入元素
 8           MyList.Add(6, HeadInsert);
 9                MyList.Add(7, HeadInsert);
10                MyList.Add(8, HeadInsert);
11 
12                MyList.Insert(99, 2);
13                MyList.InsertBefore(999, 2);
14 
15                node = MyList.Head;
16                node = PrintData(node);
17 
18                Console.ReadLine();          
19         }
20 
21         private static LinkNode<int> PrintData(LinkNode<int> node)
22         {
23             while (node != null)
24             {
25                 Console.WriteLine("The data of List are:{0}", node.Data);
26                 node = node.Next;
27             }
28             return node;
29         }
原文地址:https://www.cnblogs.com/bloomalone/p/2861309.html