LinkedList
LinkedList
是一个可变类型的泛型双向链表
LinkedList<int> linkedList = new LinkedList<int>();
//往链表尾部加
linkedList.Addlast(1);
//往链表头部加
linkedList.AddFirst(2);
//在某个节点之后加一个节点
LinkedListNode<int> n = linkedList.Find(1);
linkedList.AddAfter(n,15);
//在某个节点之前加一个节点
linkedList.AddBefore(n,10);
//移除头节点
linkedList.RemoveFirst();
//移除尾节点
linkedList.RemoveLast();
//移除指定节点
linkedList.Remove(1);
//清空
linkedList.Clear();
//查头节点
LinkedListNode<int> first = linkedList.First;
//查尾节点
LinkedListNode<int> last = linkedList.Last;
//查指定元素,找不到的话会返回null
LinkedListNode<int> node = linkedList.Find(1);
//判断存在
if (linkedList.Contains(1))
{
Console.WriteLine("存在1")
}
//要先得到节点才能改
linkedList.First.Value = 10;
//foreach遍历
foreach (int item in linkedList)
{
Console.WriteLine(item);
}
//从头到尾遍历
LinkedListNode<int> head = linkedList.First;
while(head != null)
{
Conosle.WriteLine(head.Value);
head =head.Next;
}
LinkedListNode<int> tail = linkedList.Last;
while(head != null)
{
Conosle.WriteLine(tail.Value);
tail = tail.Previous;
}
标签:head,LinkedList,LinkedListNode,WriteLine,节点,linkedList
From: https://www.cnblogs.com/cannedmint/p/18683260