LinkedList 概述
LinkedList<T>
通过节点(Node)来存储数据,每个节点包含数据和指向下一个节点的引用
LinkedList<T>
存储的元素是可重复的
LinkedList<T>
支持泛型,可以指定存储的元素的类型
LinkedList<T>
不支持索引,因为它通过节点(Node)来存储数据
LinkedList<T>
不是线程安全的,在多线程环境中需要谨慎使用
一、LinkedList 的定义
定义 LinkedList
LinkedList< int > nums = new LinkedList< int > ( ) ;
定义 LinkedList 并填充一些元素
LinkedList< int > nums = new LinkedList< int > ( new int [ ] { 1 , 2 , 3 , 4 , 5 } ) ;
二、LinkedList 结点的遍历
1、使用 foreach 循环遍历
LinkedList< int > nums = new LinkedList< int > ( new int [ ] { 1 , 2 , 3 , 4 , 5 } ) ; foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果1
2
3
4
5
2、使用 First 属性与 Next 属性遍历
LinkedList< int > nums = new LinkedList< int > ( new int [ ] { 1 , 2 , 3 , 4 , 5 } ) ; LinkedListNode< int > currentNode = nums. First;
while ( currentNode != null )
{ Console. WriteLine ( currentNode. Value) ; currentNode = currentNode. Next;
}
# 输出结果1
2
3
4
5
三、LinkedList 的常用方法
1、LinkedList 结点的添加(对于 LinkedList)
从 LinkedList 头部添加
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddLast ( 1 ) ;
nums. AddLast ( 2 ) ;
nums. AddLast ( 3 ) ; foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果1
2
3
从 LinkedList 尾部添加
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddFirst ( 1 ) ;
nums. AddFirst ( 2 ) ;
nums. AddFirst ( 3 ) ; foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果3
2
1
从 LinkedList 头部和尾部添加
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddLast ( 1 ) ;
nums. AddLast ( 2 ) ;
nums. AddLast ( 3 ) ; nums. AddFirst ( 1 ) ;
nums. AddFirst ( 2 ) ;
nums. AddFirst ( 3 ) ; foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果3
2
1
1
2
3
2、LinkedList 结点的添加(对于结点)
在某个结点之后添加
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddLast ( 1 ) ;
nums. AddLast ( 2 ) ;
nums. AddLast ( 3 ) ; LinkedListNode< int > secondNode = nums. First. Next;
nums. AddAfter ( secondNode, 10 ) ; foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果1
2
10
3
在某个结点之前添加
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddLast ( 1 ) ;
nums. AddLast ( 2 ) ;
nums. AddLast ( 3 ) ; LinkedListNode< int > secondNode = nums. First. Next;
nums. AddBefore ( secondNode, 10 ) ; foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果1
10
2
3
3、LinkedList 结点的删除
删除指定数据的结点
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddLast ( 1 ) ;
nums. AddLast ( 2 ) ;
nums. AddLast ( 3 ) ; Console. WriteLine ( "删除结点之前" ) ;
foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
} nums. Remove ( 2 ) ; Console. WriteLine ( "删除结点之后" ) ;
foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果删除元素之前
1
2
3
删除元素之后
1
3
删除第一个结点
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddLast ( 1 ) ;
nums. AddLast ( 2 ) ;
nums. AddLast ( 3 ) ; Console. WriteLine ( "删除结点之前" ) ;
foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
} nums. RemoveFirst ( ) ; Console. WriteLine ( "删除结点之后" ) ;
foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果删除元素之前
1
2
3
删除元素之后
2
3
删除最后一个结点
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddLast ( 1 ) ;
nums. AddLast ( 2 ) ;
nums. AddLast ( 3 ) ; Console. WriteLine ( "删除结点之前" ) ;
foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
} nums. RemoveLast ( ) ; Console. WriteLine ( "删除结点之后" ) ;
foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果删除结点之前
1
2
3
删除结点之后
1
2
4、LinkedList 结点的清空
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddLast ( 1 ) ;
nums. AddLast ( 2 ) ;
nums. AddLast ( 3 ) ; Console. WriteLine ( "清空结点之前" ) ;
foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
} nums. Clear ( ) ; Console. WriteLine ( "清空结点之后" ) ;
foreach ( int value in nums)
{ Console. WriteLine ( value ) ;
}
# 输出结果清空结点之前
1
2
3
清空结点之后
5、LinkedList 结点的查找
LinkedList< int > nums = new LinkedList< int > ( ) ; nums. AddLast ( 1 ) ;
nums. AddLast ( 2 ) ;
nums. AddLast ( 3 ) ; LinkedListNode< int > node = nums. Find ( 2 ) ; Console. WriteLine ( node. Value) ;
# 输出结果2