C#LinkedList链表

C#链表

一、基本概念

链表在很多语言中都有介绍,它是一种链状的数据结构。它本身带有的节点可以指向下一个或上一个节点,从而可实现轮询。
C#LinkedList<T>链表_第1张图片

二、链表的优缺点

优点:
一般数组都是需要一连串的内存空间来存储数据,但是链表结构不需要一连串的内存空间。此外,由于他具有的独特的结构,使得链表插入数据变得非常的快捷。因为它不需要移动后面的数据。List列表中间插入一个数据时,后面所有的数据都要进行移动。

缺点:
但是链表也有缺点,因为每个节点都是离散的,所以访问链表中数据只能一个接着一个访问,这需要较长的时间来查找位于链表中间或尾部的元素。访问数据效率就会变得低下。

三、LinkedList

是一个双向的链表,其元素指定前面和后面的元素。

public class LinkedList : ICollection, IEnumerable, ICollection, IEnumerable, ISerializable, IDeserializationCallback
{ 类体。。。}

上述可知,LinkedList继承了ICollection, IEnumerable, ICollection, IEnumerable, ISerializable, IDeserializationCallback接口。

四、LinkedList链表常用的方法

// 摘要: 
//     初始化为空的 System.Collections.Generic.LinkedList 类的新实例。
public LinkedList();
//
// 摘要: 
//     初始化 System.Collections.Generic.LinkedList 类的新实例,该实例包含从指定的 System.Collections.IEnumerable
//     中复制的元素并且其容量足以容纳所复制的元素数。
//
// 参数: 
//   collection:
//     System.Collections.IEnumerable,其元素被复制到新的 System.Collections.Generic.LinkedList
//     中。
//
// 异常: 
//   System.ArgumentNullException:
//     collection 为 null。
public LinkedList(IEnumerable<T> collection);
//
// 摘要: 
//     初始化 System.Collections.Generic.LinkedList 类的新实例,该实例可使用指定的 System.Runtime.Serialization.SerializationInfo
//     和 System.Runtime.Serialization.StreamingContext 进行序列化。
//
// 参数: 
//   info:
//     一个 System.Runtime.Serialization.SerializationInfo 对象,它包含序列化 System.Collections.Generic.LinkedList
//     所需的信息。
//
//   context:
//     System.Runtime.Serialization.StreamingContext 对象,该对象包含与 System.Collections.Generic.LinkedList
//     相关联的序列化流的源和目标。
protected LinkedList(SerializationInfo info, StreamingContext context);

// 摘要: 
//     获取 System.Collections.Generic.LinkedList 中实际包含的节点数。
//
// 返回结果: 
//     System.Collections.Generic.LinkedList 中实际包含的节点数。
public int Count {
    get; }
//
// 摘要: 
//     获取 System.Collections.Generic.LinkedList 的第一个节点。
//
// 返回结果: 
//     System.Collections.Generic.LinkedList 的第一个 System.Collections.Generic.LinkedListNode
public LinkedListNode<T> First {
    get; }
//
// 摘要: 
//     获取 System.Collections.Generic.LinkedList 的最后一个节点。
//
// 返回结果: 
//     System.Collections.Generic.LinkedList 的最后一个 System.Collections.Generic.LinkedListNode
public LinkedListNode<T> Last {
    get; }

// 摘要: 
//     在 System.Collections.Generic.LinkedList 中指定的现有节点后添加指定的新节点。
//
// 参数: 
//   node:
//     要在其后插入 newNode 的 System.Collections.Generic.LinkedListNode
//
//   newNode:
//     要添加到 System.Collections.Generic.LinkedList 的新 System.Collections.Generic.LinkedListNode
//
// 异常: 
//   System.ArgumentNullException:
//     node 为 null。 - 或 - newNode 为 null。
//
//   System.InvalidOperationException:
//     node 不在当前 System.Collections.Generic.LinkedList 中。 - 或 - newNode 属于另一个
//     System.Collections.Generic.LinkedList
public void AddAfter(LinkedListNode<T> node, LinkedListNode<T> newNode);
//
// 摘要: 
//     在 System.Collections.Generic.LinkedList 中指定的现有节点后添加包含指定值的新节点。
//
// 参数: 
//   node:
//     要在其后插入包含 value 的新 System.Collections.Generic.LinkedListNode 的 System.Collections.Generic.LinkedListNode
//
//   value:
//     要添加到 System.Collections.Generic.LinkedList 的值。
//
// 返回结果: 
//     包含 value 的新 System.Collections.Generic.LinkedListNode
//
// 异常: 
//   System.ArgumentNullException:
//     node 为 null。
//
//   System.InvalidOperationException:
//   

你可能感兴趣的:(C#宝典)