为了优化我写的c#根据biff8生成excel,学一下数据结构,写了下面这个排序的双链表(可能应叫三元链表)
//
双链节点类
public class TwoLinkNode < T >
... {
public TwoLinkNode<T> NextNode; //指向下一个结点的引用
public TwoLinkNode<T> PreNode; //指向前一个结点的引用
public T Data; //用泛类型来声明数据
public ushort Index; //指针的索引用,用于行号或列号
//作为链的中间结点的结点
public TwoLinkNode(ushort index, T data, TwoLinkNode<T> nextNode, TwoLinkNode<T> preNode)
...{
this.Data = data;
this.Index = index;
this.NextNode = nextNode;
this.PreNode = preNode;
}
//作为尾结点的结点
public TwoLinkNode(ushort index, T data)
: this(index, data, null, null)
...{
}
}
public class TwoLinkNode < T >
... {
public TwoLinkNode<T> NextNode; //指向下一个结点的引用
public TwoLinkNode<T> PreNode; //指向前一个结点的引用
public T Data; //用泛类型来声明数据
public ushort Index; //指针的索引用,用于行号或列号
//作为链的中间结点的结点
public TwoLinkNode(ushort index, T data, TwoLinkNode<T> nextNode, TwoLinkNode<T> preNode)
...{
this.Data = data;
this.Index = index;
this.NextNode = nextNode;
this.PreNode = preNode;
}
//作为尾结点的结点
public TwoLinkNode(ushort index, T data)
: this(index, data, null, null)
...{
}
}
双链表类
/**/
/// <summary>
/// 找到节点的位置
/// </summary>
public enum nodeLocation
... {
/**//// <summary>
/// 找到节点
/// </summary>
current,
/**//// <summary>
/// 找到节点的前一个
/// </summary>
before,
/**//// <summary>
/// 找到节点的后一个
/// </summary>
after,
/**//// <summary>
/// 什么也没找到
/// </summary>
none,
}
/**/ /// <summary>
/// 实现一个升序排序的双链表
/// 用于记录ExcelRows或ExcelCells链的值
/// 所有key的值实际rows(cols)号(1,2,3,4,5,6......)此格无值则跳过
/// </summary>
/// <typeparam name="T">ExcelCellLink或ExcelCell</typeparam>
public class BaseTwoLinkList < T > : IDictionary < ushort , T >
... {
public TwoLinkNode<T> current;
internal TwoLinkNode<T> head;
internal TwoLinkNode<T> tail;
internal int nodeCount; //保存节点数目,通过Count方法返回
internal int version; //记录链表变化的版本号
public BaseTwoLinkList()
...{
head = tail = current = null; //初始化头结点
nodeCount = 0;
version = 0;
}
IEnumerator>#region IEnumerator<KeyValuePair<ushort, T>>
/**//// <summary>
/// 用于foreach循环
/// </summary>
private class ExcelCellEnumerator : IEnumerator<KeyValuePair<ushort, T>>
...{
//嵌套在ExcelCellEnumerator内部的计数器类
public BaseTwoLinkList<T> list;
public KeyValuePair<ushort, T> currentElement;
internal TwoLinkNode<T> currentNode; //嵌套在内部才可以使用这个类
public int version; //内部版本记录,用于比对链表是否已有改变
public ExcelCellEnumerator(BaseTwoLinkList<T> list)
...{
this.list = list;
this.version = list.version;
this.currentNode = list.head;
currentElement = default(KeyValuePair<ushort, T>);
}
protected void VerifyListIsUnchanged()
...{
//判断版本是否在枚举器创建后还发生了改变
if (version != list.version)
throw new InvalidOperationException("该链表在枚举器创建后发生了改变");
}
IEnumerator> 成员#region IEnumerator<KeyValuePair<int,object>> 成员
public KeyValuePair<ushort, T> Current
...{
//返回当前记录
get
...{
//判断是否已经到了链表尾或是否调用了MoveNext查找记录
if (currentElement.Value == null)
...{
if (currentNode == list.head)
throw new InvalidOperationException("Current方法在MoveNext被调用前是无效的");
else
throw new InvalidOperationException("已到达集合尾,因此Current方法无效");
}
return currentElement;
}
}
#endregion
IDisposable 成员#region IDisposable 成员
public void Dispose()
...{
list = null;
currentNode = null;
version = 0;
}
#endregion
IEnumerator 成员#region IEnumerator 成员
object IEnumerator.Current
...{
//返回当前记录
get
...{
//判断是否已经到了链表尾或是否调用了MoveNext查找记录
if (currentElement.Value == null)
...{
if (currentNode == list.head)
throw new InvalidOperationException("Current方法在MoveNext被调用前是无效的");
else
throw new InvalidOperationException("已到达集合尾,因此Current方法无效");
}
return currentElement;
}
}
public bool MoveNext()
...{
//将枚举器继续移到下一个对象
VerifyListIsUnchanged();
if (currentNode != null)
...{
currentElement = new KeyValuePair<ushort, T>(currentNode.Index, currentNode.Data);
currentNode = currentNode.NextNode;
return true;
}
else
...{
currentElement = default(KeyValuePair<ushort, T>);
currentNode = list.head;
return false;
}
}
public void Reset()
...{
//将枚举器重置为其初始位置
VerifyListIsUnchanged();
currentNode = list.head;
currentElement = default(KeyValuePair<ushort, T>);
}
#endregion
}
#endregion
protected void Validate(ushort index)
...{
//验证index是否在可用范围内
if (index < 0 )
throw new ArgumentOutOfRangeException("索引越界.");
}
protected void Validate(object value)
...{
//验证输入的数据是否存在
if (value == null)
throw new ArgumentNullException();
}
protected void Validate(ushort index, object value)
...{
//同时验证索引与数据元素
Validate(index);
Validate(value);
}
public bool IsNull()
...{
if (nodeCount == 0)
return true;
return false;
}
/**//// <summary>
/// 判断是否为到达尾部
/// </summary>
public bool IsEof()
...{
if (current == tail)
return true;
return false;
}
/**//// <summary>
/// 判断是否为到达头部
/// </summary>
public bool IsBof()
...{
if ( current == head)
return true;
return false;
}
public nodeLocation FindByIndex(int index, out TwoLinkNode<T> twolinknode)
...{
//通过index来查找链表中的结点
twolinknode = current;
if (IsNull()) //不是空或结尾
...{
twolinknode = null;
return nodeLocation.none;
}
else if (current.Index == index) //正好是要找的值
...{
return nodeLocation.current;
}
else if (current.Index > index) //当前值比要找的值大向前找
...{
while (twolinknode.PreNode != null)
...{
twolinknode = twolinknode.PreNode;
// 找到比当前值小就结束,返回找到的节点为了Insert
if (twolinknode.Index < index)
...{
current = twolinknode;
return nodeLocation.before;
}
else if (twolinknode.Index == index)
...{
current = twolinknode; //将当前节点设为找到的节点
return nodeLocation.current;
}
}
current = head;
return nodeLocation.after;
}
else if (current.Index < index) //当前值比要找的值小向后找
...{
while (twolinknode.NextNode != null)
...{
twolinknode = twolinknode.NextNode;
// 找到比当前值大就结束,返回找到的节点为了Insert
if (twolinknode.Index > index)
...{
current = twolinknode;
return nodeLocation.after;
}
else if (twolinknode.Index == index)
...{
current = twolinknode; //将当前节点设为找到的节点
return nodeLocation.current;
}
}
current = tail;
return nodeLocation.before;
}
else
throw new Exception("未处理的情况");
}
public void Insert(ushort index, T value, nodeLocation nl)
...{
TwoLinkNode<T> newNode;
switch (nl)
...{
case nodeLocation.current:
current.Data = value;
break;
case nodeLocation.after:
//是头部则在头前加一个
if (IsBof())
...{
head.PreNode = new TwoLinkNode<T>(index, value, head, null);
head = head.PreNode;
current = head;
}
else
...{
current = new TwoLinkNode<T>(index, value, current, current.PreNode);
current.NextNode.PreNode = current;
current.PreNode.NextNode = current;
}
nodeCount++;
break;
case nodeLocation.before:
//在尾部则加一个
if (IsEof())
...{
tail.NextNode = new TwoLinkNode<T>(index, value, null, tail);
tail = tail.NextNode;
current = tail;
}
else
...{
current.NextNode = new TwoLinkNode<T>(index, value, current.NextNode, current);
current = current.NextNode;
current.NextNode.PreNode = current;
}
nodeCount++;
break;
case nodeLocation.none:
Add(index, value);
return;
default:
throw new Exception("未处理的nodeLocation类别");
}
version++;
}
public void Insert(ushort index, T value)
...{
TwoLinkNode<T> newNode;
nodeLocation nl = FindByIndex(index, out newNode);
Insert(index, value, nl);
}
/**//// <summary>
/// 删除当前的数据
/// </summary>
public void Delete()
...{
//若为空链表
if (!IsNull())
...{
//若删除头
if (IsBof())
...{
head = current.NextNode;
current = head;
if (head != null)
head.PreNode = null;
}
//若删除尾
else if (IsEof())
...{
current = current.PreNode;
tail = current;
if (tail != null)
tail.NextNode = null;
}
else
...{
//若删除中间数据
current.PreNode.NextNode = current.NextNode;
current.NextNode.PreNode = current.PreNode;
current = current.PreNode;
}
version++;
nodeCount--;
}
}
IDictionary 成员#region IDictionary<int,T> 成员
public void Add(ushort key, T value)
...{
Validate(key, value); //先验证值
TwoLinkNode<T> newNode ;
nodeLocation nl = FindByIndex(key, out newNode);
if (IsEof() || nl== nodeLocation.none)
...{
newNode = new TwoLinkNode<T>(key, value);
if (IsNull())
...{
head = newNode;
tail = newNode;
}
else
...{
tail.NextNode = newNode;
newNode.PreNode = tail;
tail = newNode;
}
current = newNode;
nodeCount++; //添加链表计数
version++;
return;
}
Insert(key, value, nl);
}
public bool ContainsKey(ushort key)
...{
TwoLinkNode<T> tlp;
if (FindByIndex(key, out tlp) == nodeLocation.current)
return true;
return false;
}
public ICollection<ushort> Keys
...{
get
...{
ushort[] keys = new ushort[nodeCount];
if (IsNull())
return null;
TwoLinkNode<T> tp = head;
int n = 0;
while (tp.NextNode != null)
...{
keys[n] = tp.Index;
n++;
tp = tp.NextNode;
}
return keys;
}
}
public bool Remove(ushort key)
...{
TwoLinkNode<T> tp;
if (FindByIndex(key, out tp) == nodeLocation.current)
...{
Delete();
return true;
}
return false;
}
public bool TryGetValue(ushort key, out T value)
...{
value = default(T);
TwoLinkNode<T> tlp;
if (FindByIndex(key, out tlp) == nodeLocation.current)
...{
value = tlp.Data;
return true;
}
return false;
}
public ICollection<T> Values
...{
get
...{
T[] values = new T[nodeCount];
if (IsNull())
return null;
TwoLinkNode<T> tp = head;
int n = 0;
while (tp.NextNode != null)
...{
values[n] = tp.Data;
n++;
tp = tp.NextNode;
}
return values;
}
}
public T this[ushort key]
...{
get
...{
Validate(key);
TwoLinkNode<T> tlp;
if (FindByIndex(key, out tlp) == nodeLocation.current)
return tlp.Data;
return default(T);
}
set
...{
Validate(key, value);
TwoLinkNode<T> tlp;
if (FindByIndex(key, out tlp) == nodeLocation.current)
current.Data = value;
else
Insert(key, value);
}
}
#endregion
ICollection> 成员#region ICollection<KeyValuePair<int,T>> 成员
public void Add(KeyValuePair<ushort, T> item)
...{
Add(item.Key, item.Value);
}
public void Clear()
...{
//清空链表
head.NextNode = null;
tail = head;
current = head;
nodeCount = 0;
version++;
}
public bool Contains(KeyValuePair<ushort, T> item)
...{
return ContainsKey(item.Key);
}
public void CopyTo(KeyValuePair<ushort, T>[] array, int arrayIndex)
...{
//从链表的索引处开始将元素复制到列表中去
if (array == null)
throw new ArgumentNullException();
else if (arrayIndex < 0)
throw new ArgumentOutOfRangeException("索引越界");
else if (arrayIndex >= array.Length || (array.Length - arrayIndex - 1) > nodeCount || array.Rank != 1)
throw new ArgumentException();
TwoLinkNode<T> tp = head;
int position = arrayIndex;
//循环复制
while (tp.NextNode != null)
...{
array.SetValue(new KeyValuePair<int, T>(tp.Index, tp.Data), position);
tp = tp.NextNode;
position++;
}
}
public int Count
...{
get ...{ return nodeCount; }
}
public bool IsReadOnly
...{
get ...{ return false; }
}
public bool Remove(KeyValuePair<ushort, T> item)
...{
return Remove(item.Key);
}
#endregion
IEnumerable> 成员#region IEnumerable<KeyValuePair<ushort,T>> 成员
public IEnumerator<KeyValuePair<ushort, T>> GetEnumerator()
...{
return new ExcelCellEnumerator(this);
}
#endregion
IEnumerable 成员#region IEnumerable 成员
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
...{
return new ExcelCellEnumerator(this);
}
#endregion
}
/// 找到节点的位置
/// </summary>
public enum nodeLocation
... {
/**//// <summary>
/// 找到节点
/// </summary>
current,
/**//// <summary>
/// 找到节点的前一个
/// </summary>
before,
/**//// <summary>
/// 找到节点的后一个
/// </summary>
after,
/**//// <summary>
/// 什么也没找到
/// </summary>
none,
}
/**/ /// <summary>
/// 实现一个升序排序的双链表
/// 用于记录ExcelRows或ExcelCells链的值
/// 所有key的值实际rows(cols)号(1,2,3,4,5,6......)此格无值则跳过
/// </summary>
/// <typeparam name="T">ExcelCellLink或ExcelCell</typeparam>
public class BaseTwoLinkList < T > : IDictionary < ushort , T >
... {
public TwoLinkNode<T> current;
internal TwoLinkNode<T> head;
internal TwoLinkNode<T> tail;
internal int nodeCount; //保存节点数目,通过Count方法返回
internal int version; //记录链表变化的版本号
public BaseTwoLinkList()
...{
head = tail = current = null; //初始化头结点
nodeCount = 0;
version = 0;
}
IEnumerator>#region IEnumerator<KeyValuePair<ushort, T>>
/**//// <summary>
/// 用于foreach循环
/// </summary>
private class ExcelCellEnumerator : IEnumerator<KeyValuePair<ushort, T>>
...{
//嵌套在ExcelCellEnumerator内部的计数器类
public BaseTwoLinkList<T> list;
public KeyValuePair<ushort, T> currentElement;
internal TwoLinkNode<T> currentNode; //嵌套在内部才可以使用这个类
public int version; //内部版本记录,用于比对链表是否已有改变
public ExcelCellEnumerator(BaseTwoLinkList<T> list)
...{
this.list = list;
this.version = list.version;
this.currentNode = list.head;
currentElement = default(KeyValuePair<ushort, T>);
}
protected void VerifyListIsUnchanged()
...{
//判断版本是否在枚举器创建后还发生了改变
if (version != list.version)
throw new InvalidOperationException("该链表在枚举器创建后发生了改变");
}
IEnumerator> 成员#region IEnumerator<KeyValuePair<int,object>> 成员
public KeyValuePair<ushort, T> Current
...{
//返回当前记录
get
...{
//判断是否已经到了链表尾或是否调用了MoveNext查找记录
if (currentElement.Value == null)
...{
if (currentNode == list.head)
throw new InvalidOperationException("Current方法在MoveNext被调用前是无效的");
else
throw new InvalidOperationException("已到达集合尾,因此Current方法无效");
}
return currentElement;
}
}
#endregion
IDisposable 成员#region IDisposable 成员
public void Dispose()
...{
list = null;
currentNode = null;
version = 0;
}
#endregion
IEnumerator 成员#region IEnumerator 成员
object IEnumerator.Current
...{
//返回当前记录
get
...{
//判断是否已经到了链表尾或是否调用了MoveNext查找记录
if (currentElement.Value == null)
...{
if (currentNode == list.head)
throw new InvalidOperationException("Current方法在MoveNext被调用前是无效的");
else
throw new InvalidOperationException("已到达集合尾,因此Current方法无效");
}
return currentElement;
}
}
public bool MoveNext()
...{
//将枚举器继续移到下一个对象
VerifyListIsUnchanged();
if (currentNode != null)
...{
currentElement = new KeyValuePair<ushort, T>(currentNode.Index, currentNode.Data);
currentNode = currentNode.NextNode;
return true;
}
else
...{
currentElement = default(KeyValuePair<ushort, T>);
currentNode = list.head;
return false;
}
}
public void Reset()
...{
//将枚举器重置为其初始位置
VerifyListIsUnchanged();
currentNode = list.head;
currentElement = default(KeyValuePair<ushort, T>);
}
#endregion
}
#endregion
protected void Validate(ushort index)
...{
//验证index是否在可用范围内
if (index < 0 )
throw new ArgumentOutOfRangeException("索引越界.");
}
protected void Validate(object value)
...{
//验证输入的数据是否存在
if (value == null)
throw new ArgumentNullException();
}
protected void Validate(ushort index, object value)
...{
//同时验证索引与数据元素
Validate(index);
Validate(value);
}
public bool IsNull()
...{
if (nodeCount == 0)
return true;
return false;
}
/**//// <summary>
/// 判断是否为到达尾部
/// </summary>
public bool IsEof()
...{
if (current == tail)
return true;
return false;
}
/**//// <summary>
/// 判断是否为到达头部
/// </summary>
public bool IsBof()
...{
if ( current == head)
return true;
return false;
}
public nodeLocation FindByIndex(int index, out TwoLinkNode<T> twolinknode)
...{
//通过index来查找链表中的结点
twolinknode = current;
if (IsNull()) //不是空或结尾
...{
twolinknode = null;
return nodeLocation.none;
}
else if (current.Index == index) //正好是要找的值
...{
return nodeLocation.current;
}
else if (current.Index > index) //当前值比要找的值大向前找
...{
while (twolinknode.PreNode != null)
...{
twolinknode = twolinknode.PreNode;
// 找到比当前值小就结束,返回找到的节点为了Insert
if (twolinknode.Index < index)
...{
current = twolinknode;
return nodeLocation.before;
}
else if (twolinknode.Index == index)
...{
current = twolinknode; //将当前节点设为找到的节点
return nodeLocation.current;
}
}
current = head;
return nodeLocation.after;
}
else if (current.Index < index) //当前值比要找的值小向后找
...{
while (twolinknode.NextNode != null)
...{
twolinknode = twolinknode.NextNode;
// 找到比当前值大就结束,返回找到的节点为了Insert
if (twolinknode.Index > index)
...{
current = twolinknode;
return nodeLocation.after;
}
else if (twolinknode.Index == index)
...{
current = twolinknode; //将当前节点设为找到的节点
return nodeLocation.current;
}
}
current = tail;
return nodeLocation.before;
}
else
throw new Exception("未处理的情况");
}
public void Insert(ushort index, T value, nodeLocation nl)
...{
TwoLinkNode<T> newNode;
switch (nl)
...{
case nodeLocation.current:
current.Data = value;
break;
case nodeLocation.after:
//是头部则在头前加一个
if (IsBof())
...{
head.PreNode = new TwoLinkNode<T>(index, value, head, null);
head = head.PreNode;
current = head;
}
else
...{
current = new TwoLinkNode<T>(index, value, current, current.PreNode);
current.NextNode.PreNode = current;
current.PreNode.NextNode = current;
}
nodeCount++;
break;
case nodeLocation.before:
//在尾部则加一个
if (IsEof())
...{
tail.NextNode = new TwoLinkNode<T>(index, value, null, tail);
tail = tail.NextNode;
current = tail;
}
else
...{
current.NextNode = new TwoLinkNode<T>(index, value, current.NextNode, current);
current = current.NextNode;
current.NextNode.PreNode = current;
}
nodeCount++;
break;
case nodeLocation.none:
Add(index, value);
return;
default:
throw new Exception("未处理的nodeLocation类别");
}
version++;
}
public void Insert(ushort index, T value)
...{
TwoLinkNode<T> newNode;
nodeLocation nl = FindByIndex(index, out newNode);
Insert(index, value, nl);
}
/**//// <summary>
/// 删除当前的数据
/// </summary>
public void Delete()
...{
//若为空链表
if (!IsNull())
...{
//若删除头
if (IsBof())
...{
head = current.NextNode;
current = head;
if (head != null)
head.PreNode = null;
}
//若删除尾
else if (IsEof())
...{
current = current.PreNode;
tail = current;
if (tail != null)
tail.NextNode = null;
}
else
...{
//若删除中间数据
current.PreNode.NextNode = current.NextNode;
current.NextNode.PreNode = current.PreNode;
current = current.PreNode;
}
version++;
nodeCount--;
}
}
IDictionary 成员#region IDictionary<int,T> 成员
public void Add(ushort key, T value)
...{
Validate(key, value); //先验证值
TwoLinkNode<T> newNode ;
nodeLocation nl = FindByIndex(key, out newNode);
if (IsEof() || nl== nodeLocation.none)
...{
newNode = new TwoLinkNode<T>(key, value);
if (IsNull())
...{
head = newNode;
tail = newNode;
}
else
...{
tail.NextNode = newNode;
newNode.PreNode = tail;
tail = newNode;
}
current = newNode;
nodeCount++; //添加链表计数
version++;
return;
}
Insert(key, value, nl);
}
public bool ContainsKey(ushort key)
...{
TwoLinkNode<T> tlp;
if (FindByIndex(key, out tlp) == nodeLocation.current)
return true;
return false;
}
public ICollection<ushort> Keys
...{
get
...{
ushort[] keys = new ushort[nodeCount];
if (IsNull())
return null;
TwoLinkNode<T> tp = head;
int n = 0;
while (tp.NextNode != null)
...{
keys[n] = tp.Index;
n++;
tp = tp.NextNode;
}
return keys;
}
}
public bool Remove(ushort key)
...{
TwoLinkNode<T> tp;
if (FindByIndex(key, out tp) == nodeLocation.current)
...{
Delete();
return true;
}
return false;
}
public bool TryGetValue(ushort key, out T value)
...{
value = default(T);
TwoLinkNode<T> tlp;
if (FindByIndex(key, out tlp) == nodeLocation.current)
...{
value = tlp.Data;
return true;
}
return false;
}
public ICollection<T> Values
...{
get
...{
T[] values = new T[nodeCount];
if (IsNull())
return null;
TwoLinkNode<T> tp = head;
int n = 0;
while (tp.NextNode != null)
...{
values[n] = tp.Data;
n++;
tp = tp.NextNode;
}
return values;
}
}
public T this[ushort key]
...{
get
...{
Validate(key);
TwoLinkNode<T> tlp;
if (FindByIndex(key, out tlp) == nodeLocation.current)
return tlp.Data;
return default(T);
}
set
...{
Validate(key, value);
TwoLinkNode<T> tlp;
if (FindByIndex(key, out tlp) == nodeLocation.current)
current.Data = value;
else
Insert(key, value);
}
}
#endregion
ICollection> 成员#region ICollection<KeyValuePair<int,T>> 成员
public void Add(KeyValuePair<ushort, T> item)
...{
Add(item.Key, item.Value);
}
public void Clear()
...{
//清空链表
head.NextNode = null;
tail = head;
current = head;
nodeCount = 0;
version++;
}
public bool Contains(KeyValuePair<ushort, T> item)
...{
return ContainsKey(item.Key);
}
public void CopyTo(KeyValuePair<ushort, T>[] array, int arrayIndex)
...{
//从链表的索引处开始将元素复制到列表中去
if (array == null)
throw new ArgumentNullException();
else if (arrayIndex < 0)
throw new ArgumentOutOfRangeException("索引越界");
else if (arrayIndex >= array.Length || (array.Length - arrayIndex - 1) > nodeCount || array.Rank != 1)
throw new ArgumentException();
TwoLinkNode<T> tp = head;
int position = arrayIndex;
//循环复制
while (tp.NextNode != null)
...{
array.SetValue(new KeyValuePair<int, T>(tp.Index, tp.Data), position);
tp = tp.NextNode;
position++;
}
}
public int Count
...{
get ...{ return nodeCount; }
}
public bool IsReadOnly
...{
get ...{ return false; }
}
public bool Remove(KeyValuePair<ushort, T> item)
...{
return Remove(item.Key);
}
#endregion
IEnumerable> 成员#region IEnumerable<KeyValuePair<ushort,T>> 成员
public IEnumerator<KeyValuePair<ushort, T>> GetEnumerator()
...{
return new ExcelCellEnumerator(this);
}
#endregion
IEnumerable 成员#region IEnumerable 成员
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
...{
return new ExcelCellEnumerator(this);
}
#endregion
}
继承双链类生成行链和列链的类
public
class
cellLink : BaseTwoLinkList
<
string
>
... {
}
public class rowLink : BaseTwoLinkList < cellLink >
... {
cellLink arrCells;
/**//// <summary>
/// 为指定单元格赋值
/// </summary>
/// <param name="iRow">行</param>
/// <param name="iColumn">列</param>
/// <param name="oCell">单元格的内容</param>
public void AddCell(ushort iRow, ushort iColumn, string oCell)
...{
TwoLinkNode<cellLink> tp;
nodeLocation nl = FindByIndex(iRow, out tp);
if (nl != nodeLocation.current)
...{
arrCells = new cellLink();
base.Insert(iRow, arrCells, nl);
}
current.Data.Insert(iColumn, oCell);
}
}
... {
}
public class rowLink : BaseTwoLinkList < cellLink >
... {
cellLink arrCells;
/**//// <summary>
/// 为指定单元格赋值
/// </summary>
/// <param name="iRow">行</param>
/// <param name="iColumn">列</param>
/// <param name="oCell">单元格的内容</param>
public void AddCell(ushort iRow, ushort iColumn, string oCell)
...{
TwoLinkNode<cellLink> tp;
nodeLocation nl = FindByIndex(iRow, out tp);
if (nl != nodeLocation.current)
...{
arrCells = new cellLink();
base.Insert(iRow, arrCells, nl);
}
current.Data.Insert(iColumn, oCell);
}
}
测试方法
static
void
test()
... {
rowLink rowlink = new rowLink();
cellLink celllink = new cellLink();
ushort i, j;
Random r = new Random();
//随机写和10行10格数据
for (ushort k = 0; k < 10; k++)
...{
i = (ushort)r.Next(0, ushort.MaxValue - 1);
for (ushort l = 0; l < 10; l++)
...{
j = (ushort)r.Next(0, ushort.MaxValue - 1);
rowlink.AddCell(i, j, i.ToString() + "," + j.ToString());
}
}
bool t = true; // 是否顺序排列
int row, col;
row = col = 0;
//读出10行10列数据是否为升序排列
foreach (KeyValuePair<ushort, cellLink> de in rowlink)
...{
if (row > de.Key)
...{
t = false;
Console.WriteLine(string.Format("row:{0} ,dekey:{1}", row, de.Key));
break;
}
row = de.Key;
foreach (KeyValuePair<ushort, string> dec in de.Value)
...{
if (col > dec.Key)
...{
t = false;
Console.WriteLine(string.Format("col:{0}, deckey{2}", col, dec.Key));
break;
}
col = dec.Key;
Console.WriteLine(dec.Value);
}
col = 0;
}
Console.WriteLine(t);
}
... {
rowLink rowlink = new rowLink();
cellLink celllink = new cellLink();
ushort i, j;
Random r = new Random();
//随机写和10行10格数据
for (ushort k = 0; k < 10; k++)
...{
i = (ushort)r.Next(0, ushort.MaxValue - 1);
for (ushort l = 0; l < 10; l++)
...{
j = (ushort)r.Next(0, ushort.MaxValue - 1);
rowlink.AddCell(i, j, i.ToString() + "," + j.ToString());
}
}
bool t = true; // 是否顺序排列
int row, col;
row = col = 0;
//读出10行10列数据是否为升序排列
foreach (KeyValuePair<ushort, cellLink> de in rowlink)
...{
if (row > de.Key)
...{
t = false;
Console.WriteLine(string.Format("row:{0} ,dekey:{1}", row, de.Key));
break;
}
row = de.Key;
foreach (KeyValuePair<ushort, string> dec in de.Value)
...{
if (col > dec.Key)
...{
t = false;
Console.WriteLine(string.Format("col:{0}, deckey{2}", col, dec.Key));
break;
}
col = dec.Key;
Console.WriteLine(dec.Value);
}
col = 0;
}
Console.WriteLine(t);
}