-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListExtensions.cs
More file actions
69 lines (59 loc) · 2.13 KB
/
LinkedListExtensions.cs
File metadata and controls
69 lines (59 loc) · 2.13 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace SharedTools
{
public static class LinkedListExtensions
{
public static int RemoveAll<T>(this LinkedList<T> liste, Predicate<T> match)
{
if (liste == null) throw new ArgumentNullException("list");
if (match == null) throw new ArgumentNullException("match");
int gelöschteElemente = 0;
LinkedListNode<T> aktuellerKnoten = liste.First;
while (aktuellerKnoten != null)
{
LinkedListNode<T> nächsterKnoten = aktuellerKnoten.Next;
if (match(aktuellerKnoten.Value))
{
liste.Remove(aktuellerKnoten);
gelöschteElemente++;
}
aktuellerKnoten = nächsterKnoten;
}
return gelöschteElemente;
}
static List<string> x = new List<string>();
public static void AddRange<T>(this LinkedList<T> liste, IEnumerable<T> collection)
{
foreach (T element in collection) liste.AddLast(element);
}
public static void AddRange<T>(this LinkedList<T> liste, IEnumerable<LinkedListNode<T>> collection)
{
foreach (LinkedListNode<T> element in collection) liste.AddLast(element);
}
public static void RemoveAt<T>(this LinkedList<T> liste, int pos)
{
if (pos < 0 || pos >= liste.Count) throw new ArgumentOutOfRangeException("pos");
if (pos == 0)
{
liste.RemoveFirst();
return;
}
if (pos == liste.Count - 1)
{
liste.RemoveLast();
return;
}
LinkedListNode<T> aktuellerKnoten = liste.First;
if (aktuellerKnoten == null) return;
LinkedListNode<T> nächsterKnoten;
for (int i = 0; i < pos; ++i)
{
nächsterKnoten = aktuellerKnoten.Next;
aktuellerKnoten = nächsterKnoten;
}
liste.Remove(aktuellerKnoten);
}
}
}