C# -- LinkedList的使用
程序员文章站
2022-06-28 20:11:09
C# -- LinkedList的使用 class Person { public Person() { } public Person(string name, int age, string sex) { this.Name = name; this.Age = age; this.Sex = ......
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); } }
上一篇: 游戏也输了,女朋友也没了
下一篇: PHP6 先修班 JSON实例代码