-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListExtension.cs
194 lines (175 loc) · 6.52 KB
/
LinkedListExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
namespace Cnoom.UnityTool.Extensions
{
public static class LinkedListExtension
{
/// <summary>
/// 遍历链表,直到满足条件后执行动作
/// </summary>
/// <param name="self"></param>
/// <param name="condition"></param>
/// <typeparam name="T"></typeparam>
public static void ForeachUntil<T>([NotNull] this LinkedList<T> self, [NotNull] Func<T, bool> condition,[NotNull] Action<T> action)
{
LinkedListNode<T> current = self.First;
while (current != null)
{
if(condition(current.Value))
{
// 满足条件,退出遍历
break;
}
current = current.Next;
}
if(current != null) action(current.Value);
}
/// <summary>
/// 从指定节点开始,遍历链表,直到满足条件后执行动作
/// </summary>
/// <param name="self"></param>
/// <param name="condition"></param>
/// <param name="action"></param>
/// <typeparam name="T"></typeparam>
public static bool ForeachUntil<T>([NotNull] this LinkedListNode<T> self, [NotNull] Func<T, bool> condition, [NotNull] Action<LinkedListNode<T>> action)
{
if (self == null)
{
throw new ArgumentNullException(nameof(self));
}
while (self!= null && !condition(self.Value))
{
self = self.Next;
}
if (self != null)
{
action(self);
}
return self != null;
}
#region Sort
/// <summary>
/// 使用快速排序进行排序
/// </summary>
/// <param name="self"></param>
/// <param name="comparer"></param>
/// <typeparam name="T"></typeparam>
public static void QuickSort<T>([NotNull] this LinkedList<T> self, [NotNull] Comparer<T> comparer)
{
QuickSort(self, comparer, self.First, self.Last);
}
private static void QuickSort<T>(LinkedList<T> list, Comparer<T> comparer, LinkedListNode<T> start, LinkedListNode<T> end)
{
if(start != null && end != null && start != end && start != end.Next)
{
// 选择最后一个节点作为基准值(也可以选择其他合适的选择方式来优化)
T pivot = end.Value;
LinkedListNode<T> i = start;
LinkedListNode<T> j = start;
while (j != end)
{
if(comparer.Compare(j.Value, pivot) <= 0)
{
// 交换i和j节点的值
T temp = i.Value;
i.Value = j.Value;
j.Value = temp;
i = i.Next;
}
j = j.Next;
}
// 将基准值放到正确的位置(i节点后面)
T tempPivot = i.Value;
i.Value = pivot;
end.Value = tempPivot;
// 对基准值左边的子链表进行排序
QuickSort(list, comparer, start, i.Previous);
// 对基准值右边的子链表进行排序
QuickSort(list, comparer, i.Next, end);
}
}
/// <summary>
/// 使用归并排序进行排序
/// </summary>
/// <param name="self"></param>
/// <param name="comparer"></param>
/// <typeparam name="T"></typeparam>
public static void Sort<T>([NotNull] this LinkedList<T> self, [NotNull] Comparer<T> comparer)
{
if(self.Count <= 1)
{
return;
}
LinkedList<T> leftHalf = new LinkedList<T>();
LinkedList<T> rightHalf = new LinkedList<T>();
// 均匀分割链表为两部分
SplitLinkedList(self, leftHalf, rightHalf);
// 递归地对左右子链表进行排序
Sort(leftHalf, comparer);
Sort(rightHalf, comparer);
// 合并已排序的左右子链表回原链表
MergeLinkedLists(leftHalf, rightHalf, self, comparer);
}
/// <summary>
/// 将链表均匀分割为两部分
/// </summary>
/// <param name="source"></param>
/// <param name="left"></param>
/// <param name="right"></param>
/// <typeparam name="T"></typeparam>
private static void SplitLinkedList<T>(LinkedList<T> source, LinkedList<T> left, LinkedList<T> right)
{
int middle = source.Count / 2;
LinkedListNode<T> current = source.First;
for(int i = 0; i < middle; i++)
{
left.AddLast(current.Value);
current = current.Next;
}
while (current != null)
{
right.AddLast(current.Value);
current = current.Next;
}
}
/// <summary>
/// 将两个已排序的链表合并为一个链表
/// </summary>
/// <param name="left"></param>
/// <param name="right"></param>
/// <param name="result"></param>
/// <param name="comparer"></param>
/// <typeparam name="T"></typeparam>
private static void MergeLinkedLists<T>(LinkedList<T> left, LinkedList<T> right, LinkedList<T> result, Comparer<T> comparer)
{
LinkedListNode<T> leftNode = left.First;
LinkedListNode<T> rightNode = right.First;
result.Clear();
while (leftNode != null && rightNode != null)
{
if(comparer.Compare(leftNode.Value, rightNode.Value) <= 0)
{
result.AddLast(leftNode.Value);
leftNode = leftNode.Next;
}
else
{
result.AddLast(rightNode.Value);
rightNode = rightNode.Next;
}
}
while (leftNode != null)
{
result.AddLast(leftNode.Value);
leftNode = leftNode.Next;
}
while (rightNode != null)
{
result.AddLast(rightNode.Value);
rightNode = rightNode.Next;
}
}
#endregion
}
}