• C# -- LinkedList的使用


    C# -- LinkedList的使用

            private static void TestLinkList()
            {
                LinkedList<Person> linkListPerson = new LinkedList<Person>();
                Person p = null;
                for (int i = 1; i < 10; i++)
                {
                    p = new Person($"程序员{i}", i + 18,i%5==1?"":"");
                    //添加
                    linkListPerson.AddLast(p);
                    //linkListPerson.AddFirst(p);               
                }
    
                Console.WriteLine($"新增的总人数:{linkListPerson.Count}");
                Console.WriteLine("-------------------------------------------------------------");
    
    
                //遍历
                LinkedListNode<Person> linkNodePerson = linkListPerson.First;
                linkNodePerson.Value.SayHi();
    
                while (linkNodePerson.Next!=null)
                {
                    linkNodePerson = linkNodePerson.Next;
                    linkNodePerson.Value.SayHi();
                }
    
                Console.WriteLine("-------------------------------------------------------------");
    
                //删除
                while (linkNodePerson.Value != null && linkListPerson.Count > 0)
                {
                    linkNodePerson = linkListPerson.Last;
                    Console.Write($"当前总人数:{linkListPerson.Count}, 即将移除:{linkNodePerson.Value.Name} --> ");
                    linkListPerson.RemoveLast();
                    Console.WriteLine($"移除后总人数:{linkListPerson.Count}");
                }
    
            }
        class Person
        {
            public Person()
            {
    
            }
            public Person(string name, int age, string sex)
            {
                this.Name = name;
                this.Age = age;
                this.Sex = sex;
            }
            public string Name { get; set; }
            public int Age { get; set; }
            public string Sex { get; set; }
            public void SayHi()
            {
                Console.WriteLine("我是{0},性别{1},今年{2}岁了!", this.Name, this.Sex, this.Age);
            }
        }
    View Code

  • 相关阅读:
    shell脚本中生成延时
    linux小技巧
    自定义微信圈分享带的图片和内容
    OOM killer
    svn报错
    Fatal error: Call-time pass-by-reference has been removed
    ThinkPHP3.1.3源码分析---php文件压缩zlib.output_compression 和 ob_gzhandler
    确保 PHP 应用程序的安全
    判断来自电脑还是手机
    以About Us为范例在Zen cart中增加页面
  • 原文地址:https://www.cnblogs.com/ChengWenHao/p/CSharpLinkedList.html
Copyright © 2020-2023  润新知