在C#中的LinkedList开头删除节点
要删除LinkedList开头的节点,代码如下-
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
LinkedList<string> list = new LinkedList<string>();
list.AddLast("One");
list.AddLast("Two");
list.AddLast("Three");
list.AddLast("Three");
list.AddLast("Three");
list.AddLast("Four");
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");
LinkedList<string>.Enumerator demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
list.RemoveFirst();
Console.WriteLine("Count of nodes (UPDATED) = " + list.Count);
Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED");
demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
}
}输出结果
这将产生以下输出-
Count of nodes = 6 Elements in LinkedList... (Enumerator iterating through LinkedList) One Two Three Three Three Four Count of nodes (UPDATED) = 5 Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED Two Three Three Three Four
示例
让我们看另一个例子-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
LinkedList<string> list = new LinkedList<string>();
list.AddLast("One");
list.AddLast("Two");
list.AddLast("Three");
list.AddLast("Three");
list.AddLast("Three");
list.AddLast("Four");
Console.WriteLine("Count of nodes = " + list.Count);
Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)");
LinkedList<string>.Enumerator demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
list.RemoveFirst();
Console.WriteLine("Count of nodes (UPDATED) = " + list.Count);
Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED");
demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
list.RemoveFirst();
Console.WriteLine("Count of nodes (UPDATED AGAIN) = " + list.Count);
Console.WriteLine("Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED AGAIN");
demoEnum = list.GetEnumerator();
while (demoEnum.MoveNext()) {
string res = demoEnum.Current;
Console.WriteLine(res);
}
}
}输出结果
这将产生以下输出-
Count of nodes = 6 Elements in LinkedList... (Enumerator iterating through LinkedList) One Two Three Three Three Four Count of nodes (UPDATED) = 5 Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED Two Three Three Three Four Count of nodes (UPDATED AGAIN) = 4 Elements in LinkedList... (Enumerator iterating through LinkedList)..UPDATED AGAIN Three Three Three Four