zoukankan      html  css  js  c++  java
  • 用C#实现单链表(创建单链表,在头部插入)

    初学C#记录历程,记录心情。
     
    节点类和链表类参考前篇。

    在接口IList中增加:

      void CreateListHead(int length); //创建单链表,在头部插入结点

    在LinkList类中创建方法:

    View Code
     1  /// <summary>
     2             /// 创建单链表
     3             /// </summary>
     4             /// <param name="length">单链表长度</param>
     5             public void CreateListHead(int length)
     6             {
     7                 if (length <= 0)
     8                 {
     9                     Console.WriteLine("Error! the length of list is incorrect.please run again");
    10                     return;
    11                 }
    12                 else
    13                 {
    14                     Console.WriteLine("Please input the node data of the list:");
    15                     for (int i = length; i > 0; i--)
    16                     {
    17                         T item = default(T);   //定义新结点数据域
    18                         item = (T)Convert.ChangeType(Console.ReadLine(), typeof(T)); //从键盘输入的字符串转换成T。当不知道当前类型要转换成什么类型的情况下,用Convert.ChangeType(value,typeof(type))
    19                         LinkNode<T> NewNode = new LinkNode<T>(item);//new一个新结点
    20                         NewNode.Next = this.Head;
    21                         this.Head = NewNode;
    22                         //NewNode.Data = item;
    23                     }
    24                 }
    25             }

    验证创建是否正确:

    View Code
     1 static LinkList<int> MyList = new LinkList<int>();
     2                 static LinkNode<int> node;
     3 
     4                private static LinkNode<int> PrintData(LinkNode<int> node)
     5                {
     6                    node = MyList.Head;
     7                    while (node != null)
     8                    {
     9                        Console.WriteLine("The data of List are:{0}", node.Data);
    10                        node = node.Next;
    11                    }
    12                    return node;
    13                }
    14 
    15                 static void Main(string[] args)
    16                 {
    17                           
    18                     Console.Write("Pleae input the lenght of your ListLink,n= ");
    19                     string str = Console.ReadLine();
    20                     int n;
    21                     n= Convert.ToInt32(str);
    22                     MyList.CreateListHead(n);
    23 
    24                     if (MyList.Head == null)
    25                     {
    26                         Console.WriteLine("List is empty");
    27                     }
    28                     else
    29                     {
    30                       Console.WriteLine("After created:");
    31                       node = PrintData(node);
    32                     }
    33                     Console.WriteLine("Please Enter and exit.Thanks!");
    34                     Console.ReadLine();
    35                 }
  • 相关阅读:
    PAT 甲级 1027 Colors in Mars
    PAT 甲级 1026 Table Tennis(模拟)
    PAT 甲级 1025 PAT Ranking
    PAT 甲级 1024 Palindromic Number
    PAT 甲级 1023 Have Fun with Numbers
    PAT 甲级 1021 Deepest Root (并查集,树的遍历)
    Java实现 蓝桥杯VIP 算法训练 无权最长链
    Java实现 蓝桥杯VIP 算法训练 无权最长链
    Java实现 蓝桥杯 算法提高 抽卡游戏
    Java实现 蓝桥杯 算法提高 抽卡游戏
  • 原文地址:https://www.cnblogs.com/bloomalone/p/2873518.html
Copyright © 2011-2022 走看看