用C#实现单链表(取第i个结点元素,删除第i个结点)

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

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

在接口IList中增加:

           T GetElem(int i);   //取第i个元素
           void ListDelete(int i); //删除第i个结点

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

取第i个元素:

View Code
 1 /// <summary>

 2             /// 取第i个元素

 3             /// </summary>

 4             /// <param name="i">i为要取元素的位置</param>

 5             /// <returns></returns>

 6             public T GetElem(int i)

 7             {

 8                 if (i < 1 || i > this.GetLength() || this.head == null)

 9                 {

10                     Console.WriteLine("The location you want to get is inexistent");

11                     return default(T);                 //对于引用类型会返回空,对于数值类型会返回零

12                 }

13                 else

14                 {

15                     LinkNode<T> node = this.head;

16                     int j = 1;

17                     while (j < i)        //寻找第i个结点,node指向它,i〉=1

18                     {

19                         node = node.Next;

20                         j++;

21                     }

22                     return node.Data;

23                }

24             }

删除第i个结点:

View Code
 1 /// <summary>

 2             /// 删除第i个结点

 3             /// </summary>

 4             /// <param name="i">第i个</param>

 5             public void ListDelete(int i)

 6             {

 7                 LinkNode<T> node = this.head;

 8                 int j = 1;

 9                 if (i < 1 || i > this.GetLength() || this.head == null) //i不合法,大于链表长度或者是空链表时,给出message

10                 {

11                     Console.WriteLine("The location you want to get is inexistent");

12                 }

13 

14                 else

15                 {

16                     if (i == 1)  //如果是要删除第1个

17                     {

18                         this.head = node.Next;

19                     }

20 

21                     else

22                     {

23                         while (j < i - 1)    //寻找第i个结点,并令node指向其前趋, i〉=2

24                         {

25                             node = node.Next;

26                             j++;

27                         }

28                         node.Next = node.Next.Next;

29                     }

30                 }

31 

32             }

验证是否正确:

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 Tail = LinkList<int>.AddPosition.Tail;

 6                 LinkList<int>.AddPosition HeadInsert = LinkList<int>.AddPosition.Head;                           

 7                 

 8              /*    

 9               //验证是否成功取到第i个元素

10               MyList.Add(6, HeadInsert);

11               MyList.Add(7, HeadInsert);

12               MyList.Add(8, HeadInsert);

13 

14               node = MyList.Head;

15               node = PrintData(node);

16               Console.WriteLine("Please input the location that you want to get:");

17               string str = Console.ReadLine();

18               int i;

19               i =Convert.ToInt32(str);

20               Console.WriteLine("the data you want to get is:{0}", MyList.GetElem(i)); 

21       

22            */  

23 

24                 /*

25                 //验证删除是否成功

26                 MyList.Add(6, HeadInsert);

27                 MyList.Add(7, HeadInsert);

28                 MyList.Add(8, HeadInsert);

29 

30                 node = MyList.Head;

31                 node = PrintData(node);

32                 Console.WriteLine("Please input the number which you want to delete:");

33                 string str = Console.ReadLine();

34                 int i;

35                 i =Convert.ToInt32(str);

36                 MyList.ListDelete(i);

37                 Console.WriteLine("After delete:");

38                 node = MyList.Head;

39                 node = PrintData(node);

40                 */

41 

42           

43                 Console.ReadLine();          

44             }

45 

46             private static LinkNode<int> PrintData(LinkNode<int> node)

47             {

48                 while (node != null)

49                 {

50                     Console.WriteLine("The data of List are:{0}", node.Data);

51                     node = node.Next;

52                 }

53                 return node;

54             }

 

 

你可能感兴趣的:(单链表)