-
Notifications
You must be signed in to change notification settings - Fork 0
/
TokenList.cs
91 lines (82 loc) · 1.62 KB
/
TokenList.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
using System.Collections.Generic;
namespace Kento
{
internal class TokenList
{
private bool empty = true;
private TokenListNode first, last;
public TokenList() {}
public TokenList(TokenList List)
{
for (TokenListNode node = List.First; node != null; node = node.Next)
{
Add(node.Value);
}
}
public TokenListNode First
{
get { return first; }
set { first = value; }
}
public TokenListNode Last
{
get { return last; }
set { last = value; }
}
public void Add(Token Token)
{
if (empty)
{
last = new TokenListNode(Token);
first = last;
empty = false;
}
else
{
last.Next = new TokenListNode(Token);
TokenListNode temp = last;
last = last.Next;
last.Previous = temp;
}
}
public LinkedList<Token> ToLinkedList()
{
var newList = new LinkedList<Token>();
for (TokenListNode node = first; node != null; node = node.Next)
{
newList.AddLast(node.Value);
}
return newList;
}
public void FromLinkedList(LinkedList<Token> List)
{
for (LinkedListNode<Token> node = List.First; node != null; node = node.Next)
{
Add(node.Value);
}
}
/// <summary>
/// Insert a new node between 2 other ones
/// </summary>
/// <param name="Node"></param>
/// <param name="Left"></param>
/// <param name="Right"></param>
public void Insert(TokenListNode Node, TokenListNode Left, TokenListNode Right)
{
if (Left != null)
Left.Next = Node;
else
{
first = Node;
}
Node.Next = Right;
Node.Previous = Left;
if (Right != null)
Right.Previous = Node;
else
{
last = Node;
}
}
}
}