C# |从链接列表中删除所有节点

原文:https://www . geeksforgeeks . org/c-sharp-remove-all-nodes-from-linked list/

链接列表<T1T5。清除方法用于从链表中移除所有节点< T >。

语法:

public void Clear ();

下面给出了一些例子,以便更好地理解实现:

例 1:

// C# code to remove all
// nodes from LinkedList
using System;
using System.Collections;
using System.Collections.Generic;

class GFG {

    // Driver code
    public static void Main()
    {
        // Creating a LinkedList of Strings
        LinkedList<String> myList = new LinkedList<String>();

        // Adding nodes in LinkedList
        myList.AddLast("A");
        myList.AddLast("B");
        myList.AddLast("C");
        myList.AddLast("D");
        myList.AddLast("E");

        // To get the count of nodes in LinkedList
        // before removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);

        // Removing all nodes from LinkedList
        myList.Clear();

        // To get the count of nodes in LinkedList
        // after removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
    }
}

输出:

Total nodes in myList are : 5
Total nodes in myList are : 0

例 2:

// C# code to remove all
// nodes from LinkedList
using System;
using System.Collections;
using System.Collections.Generic;

class GFG {

    // Driver code
    public static void Main()
    {
        // Creating a LinkedList of Integers
        LinkedList<int> myList = new LinkedList<int>();

        // Adding nodes in LinkedList
        myList.AddLast(2);
        myList.AddLast(4);
        myList.AddLast(6);

        // To get the count of nodes in LinkedList
        // before removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);

        // Removing all nodes from LinkedList
        myList.Clear();

        // To get the count of nodes in LinkedList
        // after removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
    }
}

输出:

Total nodes in myList are : 3
Total nodes in myList are : 0

注:

  • Count 设置为零,集合元素对其他对象的引用也会被释放。
  • 第一个最后一个设置为
  • 这个方法是一个 O(n) 运算,其中 n 是 Count。

参考: