-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedList.cs
More file actions
176 lines (145 loc) · 3.83 KB
/
Copy pathLinkedList.cs
File metadata and controls
176 lines (145 loc) · 3.83 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace DaLinkedList
{
public class LinkedObject<T>
{
public LinkedObject<T> pPrev = null;
public LinkedObject<T> pNext = null;
public T _object;
public LinkedObject(T obj)
{
_object = obj;
}
public void Unlink()
{
if (pPrev != null)
pPrev.pNext = pNext;
if (pNext != null)
pNext.pPrev = pPrev;
pNext = null;
pPrev = null;
_object = default;
}
}
public class LinkedList<T> : IEnumerable<T>
{
private LinkedObject<T> pFirst = null;
private LinkedObject<T> pLast = null;
private int objectCount = 0;
public LinkedList()
{
}
public LinkedObject<T> GetFirst()
{
return pFirst;
}
public void Add(T obj)
{
LinkedObject<T> lobj = new LinkedObject<T>(obj);
if (pFirst == null)
{
// first time, list was empty.
pFirst = lobj;
pLast = lobj;
}
else
{
// add last
pLast.pNext = lobj;
lobj.pPrev = pLast;
pLast = lobj;
}
objectCount++;
}
public bool Remove(T obj)
{
LinkedObject<T> lo = pFirst;
while (lo != null)
{
if(lo._object.Equals(obj))
{
if(pFirst.Equals(lo))
{
pFirst = pFirst.pNext;
}
if(pLast.Equals(lo))
{
pLast = pLast.pPrev;
}
lo.Unlink();
objectCount--;
return true;
}
lo = lo.pNext;
}
return false;
}
// Find and return the value if present in list, otherwise default (null) value.
public T Find(T lookFor)
{
LinkedObject<T> lo = pFirst;
while(lo != null)
{
if (lo._object.Equals(lookFor))
return lo._object;
lo = lo.pNext;
}
// not in list, so return 'default'. (normally null)
return default;
}
public IEnumerator<T> GetEnumerator()
{
return new LinkedListEnumerator<T>(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
}
public class LinkedListEnumerator<T> : IEnumerator<T>
{
LinkedList<T> linkedList = null;
LinkedObject<T> linkedObject = null;
public T Current
{
get
{
if (linkedObject == null)
return default;
return linkedObject._object;
}
}
object IEnumerator.Current
{
get
{
return this.Current;
}
}
public LinkedListEnumerator(LinkedList<T> ll)
{
linkedList = ll;
}
public void Dispose()
{
// nada. called as soon as the foreach() ends.
}
public bool MoveNext()
{
if (linkedObject == null)
linkedObject = linkedList.GetFirst();
else
linkedObject = linkedObject.pNext;
if (linkedObject == null)
return false;
return true;
}
public void Reset()
{
linkedObject = null;
}
}
}