排序的双链表

为了优化我写的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, nullnull)
        
{
        }

    }

双链表类

     /// <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>

        
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 成员

        
ICollection> 成员

        
IEnumerable> 成员

        
IEnumerable 成员
    }

继承双链类生成行链和列链的类

     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);
        }

    }

测试方法

         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(0ushort.MaxValue - 1);
                
for (ushort l = 0; l < 10; l++)
                
{
                    j 
= (ushort)r.Next(0ushort.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<ushortstring> 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);
        }

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值