在 C# 中移除链表中的所有节点
要从 LinkedList 中移除所有节点,代码如下 -
示例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main() {
int [] num = {10, 20, 30, 40, 50};
LinkedList<int> list = new LinkedList<int>(num);
Console.WriteLine("LinkedList nodes...");
foreach (var n in list) {
Console.WriteLine(n);
}
list.Clear();
Console.WriteLine("LinkedList is empty now!");
foreach (var n in list) {
Console.WriteLine(n);
}
}
}输出
这将产生以下输出 -
LinkedList nodes... 10 20 30 40 50 LinkedList is empty now!
示例
我们来看另一个示例 -
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
LinkedList<String> list = new LinkedList<String>();
list.AddLast("A");
list.AddLast("B");
list.AddLast("C");
list.AddLast("D");
list.AddLast("E");
list.AddLast("F");
list.AddLast("G");
list.AddLast("H");
list.AddLast("I");
list.AddLast("J");
Console.WriteLine("Count of nodes = " + list.Count);
list.Clear();
Console.WriteLine("Count of nodes (updated) = " + list.Count);
}
}输出
这将产生以下输出 -
Count of nodes = 10 Count of nodes (updated) = 0
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP