纳金网

标题: C#之LinkedList使用以及常用容器类 [打印本页]

作者: may    时间: 2018-8-23 20:41
标题: C#之LinkedList使用以及常用容器类

LinkedList是很好用的一个工具,可以方便做出前后关系
  1. using System;
  2. using System.Collections.Generic;

  3. namespace ConsoleApplication
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             LinkedList<int> a = new LinkedList<int>();
  10.             a.AddFirst(3);
  11.             a.AddLast(1);
  12.             a.AddLast(4);

  13.             foreach (int i in a)
  14.                 Console.Write(i + " ");
  15.             Console.WriteLine();

  16.             LinkedListNode<int> cur = a.Find(3);  //cur对应3所在的位置
  17.             a.AddBefore(cur, 2);                //在3前面添加2

  18.             foreach (int i in a)
  19.                 Console.Write(i + " ");

  20.             a.Remove(3);
  21.             a.Clear();
  22.             Console.Read();
  23.         }      
  24.     }
  25. }
复制代码
插入:O(1) (在头尾部),O(N) (在其他位置)
删除:O(1) (在头尾部),O(N) (在其他位置)
按照索引器访问:没有索引器(因为没有实现IList<T>
查找:O(N)

除此之外,还有SortedList<K,T>和SortedDictionary<K,T>这两个,是用于排序频繁发生的时候,排序成本O(ln(N))
一些可能会用到的容器类:
线性表和链表(使用最多的对象):

栈和队列(只有在模拟栈和队列时才考虑):
哈希(需要大规模查找):
集合(保存一组唯一的值/模拟集合运算):

來自:cony138






欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5