C#程序删除链表中第一次出现的节点
以下是带有节点的LinkedList列表。
string [] students = {"Katie","Jennifer","Amy","Vera"}; LinkedList<string> list = new LinkedList<string>(students);
现在,让我们删除带有字符串元素“Vera”的节点。
为此,使用Remove()
方法。
list.Remove("Vera");
示例
using System; using System.Collections.Generic; class Demo { static void Main() { string [] students = {"Katie","Jennifer","Amy","Vera"}; LinkedList<string> list = new LinkedList<string>(students); foreach (var stu in list) { Console.WriteLine(stu); } //删除节点 list.Remove("Vera"); Console.WriteLine("LinkedList after删除节点..."); foreach (var stu in list) { Console.WriteLine(stu); } } }
输出结果
Katie Jennifer Amy Vera LinkedList after删除节点... Katie Jennifer Amy