-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedList.cs
221 lines (187 loc) · 5.22 KB
/
LinkedList.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/// <summary>
/// Simple Linked List Class
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Value"></param>
class LinkedList<T>(T Value) where T : IEquatable<T>
{
public Node<T> Head = new(Value);
public int Length = 1;
/// <summary>
/// Returns the value at a give index.
/// O(n)
/// </summary>
/// <param name="index"></param>
/// <returns>Value at index</returns>
public T Get(int index)
{
Node<T>? CurrentNode = Head;
while (CurrentNode.Next != null)
{
if (index == 0)
break;
index --;
CurrentNode = CurrentNode.Next;
}
return CurrentNode.Value;
}
/// <summary>
/// Finds and returns the index at which the specified value occurs first.
/// O(n)
/// </summary>
/// <param name="Value">The value that should be searched for</param>
/// <returns>Index of the value</returns>
public int Find(T Value)
{
Node<T>? CurrentNode = Head;
int Index = 0;
while (CurrentNode.Next != null)
{
if (CurrentNode.Value.Equals(Value))
break;
CurrentNode = CurrentNode.Next;
Index++;
}
if (CurrentNode.Next == null)
return -1;
return Index;
}
/// <summary>
/// Returns the first value in the list.
/// O(1)
/// </summary>
/// <returns>The first value in the list</returns>
public T First()
{
return Head.Value;
}
/// <summary>
/// Returns the last value in the list.
/// O(n)
/// </summary>
/// <returns>The last value in the list</returns>
public T Last()
{
Node<T>? CurrentNode = Head;
while (CurrentNode.Next != null)
{
CurrentNode = CurrentNode.Next;
}
return CurrentNode.Value;
}
/// <summary>
/// Concatinates the second linked list with the first one in place.
/// O(n)
/// </summary>
/// <param name="other">The list that should be concatinated</param>
public void Concat(LinkedList<T> other)
{
Node<T>? CurrentNode = other.Head;
while (CurrentNode != null)
{
Append(CurrentNode.Value);
CurrentNode = CurrentNode.Next;
}
}
/// <summary>
/// Checks if a value is in the list.
/// O(n)
/// </summary>
/// <param name="Value">The value that should be checked</param>
/// <returns>true if the value is found otherwise false</returns>
public bool Contains(T Value)
{
Node<T>? CurrentNode = Head;
while (CurrentNode != null)
{
if (CurrentNode.Value.Equals(Value))
return true;
CurrentNode = CurrentNode.Next;
}
return false;
}
/// <summary>
/// Adds the specified value to the end of the list.
/// O(n)
/// </summary>
/// <param name="Value">The value that should be appended</param>
public void Append(T Value)
{
Node<T>? CurrentNode = Head;
while (CurrentNode.Next != null)
{
CurrentNode = CurrentNode.Next;
}
CurrentNode.Next = new Node<T>(Value);
Length++;
}
/// <summary>
/// Inserts the specified value at the specified index.
/// O(n)
/// </summary>
/// <param name="Index">The index where the value should be inserted</param>
/// <param name="Value">The value that should be inserted</param>
public void Insert(int Index, T Value)
{
Node<T>? CurrentNode = Head;
if (Index == 0)
{
Head = new Node<T>(Value, CurrentNode);
return;
}
while (CurrentNode.Next != null && Index - 1 > 0)
{
CurrentNode = CurrentNode.Next;
Index -= 1;
}
Node<T>? NewNode = new(Value, CurrentNode.Next);
CurrentNode.Next = NewNode;
Length++;
}
/// <summary>
/// Deletes the first occurence of the value in the list.
/// O(n)
/// </summary>
/// <param name="Value">The value that must be deleted</param>
public void Delete(T Value)
{
Node<T>? CurrentNode = Head;
while (CurrentNode.Next != null)
{
if (CurrentNode.Next.Value.Equals(Value))
break;
else
CurrentNode = CurrentNode.Next;
}
if (CurrentNode.Next != null)
{
if (CurrentNode.Next.Next != null)
CurrentNode.Next = CurrentNode.Next.Next;
else
CurrentNode.Next = null;
}
Length--;
}
public override string ToString()
{
string result = "";
Node<T>? currentNode = Head;
while (currentNode != null)
{
result += $"{currentNode.Value} -> ";
currentNode = currentNode.Next;
}
return result + "_";
}
}
/// <summary>
/// Single Node of Linked list
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Value"></param>
/// <param name="Next"></param>
internal class Node<T>(T Value, Node<T>? Next=null)
{
public T Value { get; set; } = Value;
public Node<T>? Next { get; set; } = Next;
}