📜  C#| LinkedList类

📅  最后修改于: 2021-05-29 20:03:10             🧑  作者: Mango

LinkedList < T >类存在于System.Collections.Generic命名空间中。这种通用类型允许快速插入和删除元素。它实现了经典的链表。每个对象都是单独分配的。在LinkedList中,某些操作不需要复制整个集合。但是在许多常见情况下,LinkedList会影响性能。

LinkedList类的特征:

  • LinkedList < T >是通用链表。它支持枚举器。
  • 插入和删除是O(1)操作。
  • 您可以删除节点,然后将它们重新插入到同一列表或另一个列表中,这样就不会在堆上分配任何其他对象。
  • 由于列表还维护内部计数,因此获取Count属性是O(1)操作。
  • LinkedList < T >对象中的每个节点的类型为LinkedListNode < T >。
  • LinkedList类不支持链接,拆分,循环或其他可能使列表处于不一致状态的功能。
  • 如果LinkedList为空,则FirstLast属性包含null。
  • LinkedList是双重链接的,因此,每个节点都指向下一个节点,并指向上一个节点。

建设者

Constructor Description
LinkedList() Initializes a new instance of the LinkedList class that is empty.
LinkedList(IEnumerable) Initializes a new instance of the LinkedList class that contains elements copied from the specified IEnumerable and has sufficient capacity to accommodate the number of elements copied.
LinkedList(SerializationInfo, StreamingContext) Initializes a new instance of the LinkedList class that is serializable with the specified SerializationInfo and StreamingContext.

例子:

// C# code to create a LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a LinkedList of Strings
        LinkedList myList = new LinkedList();
  
        // Adding nodes in LinkedList
        myList.AddLast("Geeks");
        myList.AddLast("for");
        myList.AddLast("Data Structures");
        myList.AddLast("Noida");
  
        // To check if LinkedList is empty or not
        if (myList.Count > 0)
            Console.WriteLine("LinkedList is not empty");
        else
            Console.WriteLine("LinkedList is empty");
    }
}

输出:

LinkedList is not empty

特性

Property Description
Count Gets the number of nodes actually contained in the LinkedList.
First Gets the first node of the LinkedList.
Last Gets the last node of the LinkedList.

例子:

// C# code to illustrate the
// LinkedList class properties
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a LinkedList of Strings
        LinkedList myList = new LinkedList();
  
        // Adding nodes in LinkedList
        myList.AddLast("GeeksforGeeks");
        myList.AddLast("GFG");
        myList.AddLast("Data Structures");
        myList.AddLast("Noida");
  
        // ------- Count Property -------
          
        // To get the first node of the LinkedList
        if (myList.Count > 0)
            Console.WriteLine(myList.First.Value);
        else
            Console.WriteLine("LinkedList is empty");
              
        // ------- Last Property -------
          
        // To get the last node of the LinkedList
        if (myList.Count > 0)
            Console.WriteLine(myList.Last.Value);
        else
            Console.WriteLine("LinkedList is empty");    
              
    }
}

输出:

GeeksforGeeks
Noida

方法

Method Description
AddAfter Adds a new node or value after an existing node in the LinkedList.
AddBefore Adds a new node or value before an existing node in the LinkedList.
AddFirst Adds a new node or value at the start of the LinkedList.
AddLast Adds a new node or value at the end of the LinkedList.
Clear() Removes all nodes from the LinkedList.
Contains(T) Determines whether a value is in the LinkedList.
CopyTo(T[], Int32) Copies the entire LinkedList to a compatible one-dimensional Array, starting at the specified index of the target array.
Equals(Object) Determines whether the specified object is equal to the current object.
Find(T) Finds the first node that contains the specified value.
FindLast(T) Finds the last node that contains the specified value.
GetEnumerator() Returns an enumerator that iterates through the LinkedList.
GetHashCode() Serves as the default hash function.
GetObjectData(SerializationInfo, StreamingContext) Implements the ISerializable interface and returns the data needed to serialize the LinkedList instance.
GetType() Gets the Type of the current instance.
MemberwiseClone() Creates a shallow copy of the current Object.
OnDeserialization(Object) Implements the ISerializable interface and raises the deserialization event when the deserialization is complete.
Remove(LinkedListNode) Removes the specified node from the LinkedList.
Remove(T) Removes the first occurrence of the specified value from the LinkedList.
RemoveFirst() Removes the node at the start of the LinkedList.
RemoveLast() Removes the node at the end of the LinkedList.
ToString() Returns a string that represents the current object.

例子:

// C# code to check if a
// value is in LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
        // Creating a LinkedList of Strings
        LinkedList myList = new LinkedList();
  
        // Adding nodes in LinkedList
        myList.AddLast("A");
        myList.AddLast("B");
        myList.AddLast("C");
        myList.AddLast("D");
        myList.AddLast("E");
  
        // To check if a value is in LinkedList
        Console.WriteLine(myList.Contains("B"));
    }
}

输出:

True

例子:

// C# code to remove the specified
// node from the LinkedList
using System;
using System.Collections;
using System.Collections.Generic;
  
class GFG {
  
    // Driver code
    public static void Main()
    {
  
        // Creating a LinkedList of Integers
        LinkedList myList = new LinkedList();
  
        // Adding nodes in LinkedList
        myList.AddLast(2);
        myList.AddLast(4);
        myList.AddLast(6);
        myList.AddLast(8);
  
        // To get the count of nodes in LinkedList
        // before removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
  
        // Removing the first node from the LinkedList
        myList.Remove(myList.First);
  
        // To get the count of nodes in LinkedList
        // after removing all the nodes
        Console.WriteLine("Total nodes in myList are : " + myList.Count);
  
        // Displaying the nodes in LinkedList
        foreach(int i in myList)
        {
            Console.WriteLine(i);
        }
    }
}

输出:

Total nodes in myList are : 4
2
4
6
8
Total nodes in myList are : 3
4
6
8

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.collections.generic.linkedlist-1?view=netframework-4.7.2