C# LinkedList类用法示例介绍

2021年5月9日16:16:30 发表评论 1,781 次浏览

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

LinkedList类的特征:

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

建设者

建设者 描述
LinkedList() 初始化一个为空的LinkedList类的新实例。
LinkedList(IEnumerable) 初始化LinkedList类的新实例, 该实例包含从指定IEnumerable复制的元素, 并具有足够的容量来容纳复制的元素数量。
LinkedList(SerializationInfo, StreamingContext) 初始化LinkedList类的新实例, 该实例可使用指定的SerializationInfo和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<String> myList = new LinkedList<String>();
  
         //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

属性

属性 描述
计数 获取实际包含在LinkedList中的节点数。
第一 获取LinkedList的第一个节点。
持续 获取LinkedList的最后一个节点。

例子:

//C# code to illustrate the
//LinkedList<T> 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<String> myList = new LinkedList<String>();
  
         //Adding nodes in LinkedList
         myList.AddLast( "lsbin" );
         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" );    
              
     }
}

输出如下:

lsbin
Noida

方法

方法 描述
AddAfter 在LinkedList中的现有节点之后添加新节点或值。
AddBefore 在LinkedList中的现有节点之前添加新节点或值。
AddFirst 在LinkedList的开头添加新的节点或值。
AddLast 在LinkedList的末尾添加一个新节点或值。
Clear() 从LinkedList中删除所有节点。
Contains(T) 确定值是否在LinkedList中。
CopyTo(T [], Int32) 从目标数组的指定索引处开始, 将整个LinkedList复制到兼容的一维数组。
Equals(对象) 确定指定对象是否等于当前对象。
Find(T) 查找包含指定值的第一个节点。
FindLast(T) 查找包含指定值的最后一个节点。
GetEnumerator() 返回遍历LinkedList的枚举数。
GetHashCode() 用作默认哈希函数。
GetObjectData(SerializationInfo, StreamingContext) 实现ISerializable接口, 并返回序列化LinkedList实例所需的数据。
GetType() 获取当前实例的类型。
MemberwiseClone() 创建当前对象的浅表副本。
OnDeserialization(Object) 当反序列化完成时, 实现ISerializable接口并引发反序列化事件。
Remove(LinkedListNode) 从LinkedList中删除指定的节点。
Remove(T) 从LinkedList中删除第一次出现的指定值。
RemoveFirst() 删除LinkedList开头的节点。
RemoveLast() 删除LinkedList末尾的节点。
ToString() 返回表示当前对象的字符串。

例子:

//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<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 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<int> myList = new LinkedList<int>();
  
         //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/en-us/dotnet/api/system.collections.generic.linkedlist-1?view=netframework-4.7.2

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: