进阶功能实现
1. 复制(实现ICollection.CopyTo)
1
public void CopyTo(Array array, int index)
2
{
3
if (array == null)
4
{
5
throw new NullReferenceException();
6
}
7
else if (index < 0)
8
{
9
throw new ArgumentOutOfRangeException();
10
}
11
else if (array.Rank != 1 || array.Length - index != this._nodeCount || array.Length <= index)
12
{
13
throw new ArgumentException();
14
}
15![]()
16
ListNode node = this._head;
17![]()
18
while (node.NextNode != null)
19
{
20
array[index] = node.NextNode.Data;
21
node = node.NextNode;
22
index++;
23
}
24
25
}
public void CopyTo(Array array, int index)2
{3
if (array == null)4
{5
throw new NullReferenceException();6
}7
else if (index < 0)8
{9
throw new ArgumentOutOfRangeException();10
}11
else if (array.Rank != 1 || array.Length - index != this._nodeCount || array.Length <= index)12
{13
throw new ArgumentException();14
}15

16
ListNode node = this._head;17

18
while (node.NextNode != null)19
{20
array[index] = node.NextNode.Data;21
node = node.NextNode;22
index++;23
}24
25
}
2. 对foreach的支持
VB中的For Each是个很强大的遍历方法,要实现这个功能,链表类必须实现IEnumerable接口,IList接口本身也是由该接口继承而来,所以这里不必要显示继承。C#语言从VB中吸取了这个非常实用的语句(写法为foreach)。对所有支持IEnumerable接口的类的实例,foreach语句使用统一的接口遍历其子项,使得以前冗长的for循环中繁琐的薄记工作完全由编译器自动完成。支持IEnumerable接口的类通常用一个内嵌类实现IEnumerator接口,并通过IEnumerable.GetEnumerator函数,允许类的使用者如foreach语句完成遍历工作。
为了对接口IEnumerable提供支持,我这里需要新建一个实现该接口的SingleLinkedListEnumerator类,定义如下:
1
public class SingleLinkedListEnumerator : IEnumerator
2
{
3
protected int _index;
4
protected SingleLinkedList _list;
5
6
public SingleLinkedListEnumerator(SingleLinkedList list)
7
{
8
this._list = list;
9
this._index = -1;
10
}
11![]()
12
#region IEnumerator 成员
13![]()
14
public void Reset()
15
{
16
this._list = -1;
17
}
18![]()
19
public object Current
20
{
21
get
22
{
23
// 验证索引的有效性。
24
if (this._index < -1 || this._index > this._list.Count - 1)
25
{
26
throw new ArgumentException("参数越界");
27
}
28
else if (this._index == -1)
29
{
30
throw new InvalidOperationException("在没有调用MoveNext前访问Current是无效的");
31
}
32
else if (this._index >= this._list.Count)
33
{
34
throw new InvalidOperationException("已到集合尾部,访问无效");
35
}
36![]()
37
return this._list[this._index];
38
}
39
}
40![]()
41
public bool MoveNext()
42
{
43
// 验证索引的有效性。
44
this._index ++;
45
if (this._index > this._list.Count - 1)
46
{
47
return false;
48
}
49
else
50
{
51
return true;
52
}
53
}
54![]()
55
#endregion
56
}
public class SingleLinkedListEnumerator : IEnumerator2
{3
protected int _index;4
protected SingleLinkedList _list;5
6
public SingleLinkedListEnumerator(SingleLinkedList list)7
{8
this._list = list;9
this._index = -1;10
}11

12
#region IEnumerator 成员13

14
public void Reset()15
{16
this._list = -1;17
}18

19
public object Current20
{21
get22
{23
// 验证索引的有效性。24
if (this._index < -1 || this._index > this._list.Count - 1)25
{26
throw new ArgumentException("参数越界");27
}28
else if (this._index == -1)29
{30
throw new InvalidOperationException("在没有调用MoveNext前访问Current是无效的");31
}32
else if (this._index >= this._list.Count)33
{34
throw new InvalidOperationException("已到集合尾部,访问无效");35
}36

37
return this._list[this._index];38
}39
}40

41
public bool MoveNext()42
{43
// 验证索引的有效性。44
this._index ++;45
if (this._index > this._list.Count - 1)46
{47
return false;48
}49
else50
{51
return true;52
}53
}54

55
#endregion56
}
实现IEnumerable.GetEnumerator
1
public IEnumerator GetEnumerator()
2
{
3
return new SingleLinkedListEnumerator(this);
4
}
public IEnumerator GetEnumerator()2
{3
return new SingleLinkedListEnumerator(this);4
}未完待续。。。


浙公网安备 33010602011771号