-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.cs
More file actions
35 lines (31 loc) · 875 Bytes
/
Copy pathSinglyLinkedList.cs
File metadata and controls
35 lines (31 loc) · 875 Bytes
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
using System.Collections;
using System.Collections.Generic;
namespace Dungeon
{
public class SinglyLinkedList<T> : IEnumerable<T>
{
public readonly T Value;
public readonly SinglyLinkedList<T> Previous;
public readonly int Length;
public SinglyLinkedList(T value, SinglyLinkedList<T> previous = null)
{
Value = value;
Previous = previous;
Length = previous?.Length + 1 ?? 1;
}
public IEnumerator<T> GetEnumerator()
{
yield return Value;
var pathItem = Previous;
while (pathItem != null)
{
yield return pathItem.Value;
pathItem = pathItem.Previous;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}