asp.net双向链表类

using System;  

using System.Data;  

using System.Configuration;  

using System.Web;  

using System.Web.Security;  

using System.Web.UI;  

using System.Web.UI.WebControls;  

using System.Web.UI.WebControls.WebParts;  

using System.Web.UI.HtmlControls;  

  

/// <summary>  

/// DListNode 的摘要说明  

/// </summary>  

public class DListNode  

{  

    private int _data;  

    private DListNode _preNode;  

    private DListNode _nextNode;  

  

    public int data  

    {  

        get { return _data; }  

        set { _data = value; }  

    }  

  

    public DListNode preNode  

    {  

        get { return _preNode; }  

        set { _preNode = value; }  

    }  

  

    public DListNode nextNode  

    {  

        get { return _nextNode; }  

        set { _nextNode = value; }  

    }  

}  

  

public class DList  

{  

    private DListNode _firstNode;  

    private int _count;  

  

    public int count  

    {  

        get { return _count; }  

        set { _count = value; }  

    }  

  

    public DListNode firstNode  

    {  

        get { return _firstNode; }  

        set { _firstNode = value; }  

    }  

  

    /// <summary>  

    /// 将对象添加到链表的结尾处  

    /// </summary>  

    /// <param name="element"></param>  

    public void Add(int element)  

    {  

        if (firstNode == null)//判断第一个节点是否为空  

        {  

            firstNode = new DListNode();//如果为空,新添加的就是第一个节点  

            firstNode.data = element;  

        }  

        else  

        {  

            DListNode dlTmp = firstNode;  

            //通过循环找到最后一个节点  

            while(true)  

            {  

                if (dlTmp.nextNode == null)//插入新的节点  

                {  

                    dlTmp.nextNode = new DListNode();  

                    dlTmp.nextNode.data = element;  

                    dlTmp.nextNode.preNode = dlTmp;  

                    break;  

                }  

                else  

                {  

                    dlTmp = dlTmp.nextNode;  

                }  

            }  

        }  

        this.count++;//链表长度加1  

    }  

  

    /// <summary>  

    /// 移除所有元素  

    /// </summary>  

    public void Clear()  

    {  

        this.firstNode = null;  

    }  

  

    /// <summary>  

    /// 搜索指定的对象的索引  

    /// </summary>  

    /// <param name="temp"></param>  

    /// <returns></returns>  

    public int indexOf(int temp)  

    {  

        DListNode dlTmp = this.firstNode;  

        int index = 0;  

        while (true)  

        {  

            //如果没有找到,则返回-1  

            if (dlTmp == null)  

            {  

                return -1;  

            }  

            else  

            {  

                if (dlTmp.data == temp)//找到则返回索引  

                {  

                    return index;  

                }  

  

                dlTmp = dlTmp.nextNode;  

                index++;  

            }  

        }  

    }  

  

    /// <summary>  

    /// 从链表中删除指定的工序  

    /// </summary>  

    /// <param name="temp"></param>  

    public void Delete(int temp)  

    {  

        DListNode dlTm = this.firstNode;  

        while (true)  

        {  

            if (dlTm == null)  

            {  

                return;  

            }  

            else  

            {  

                if (dlTm.data == temp)  

                {  

                    DListNode dlPre = new DListNode();  

                    if (dlTm.preNode != null)  

                    {  

                        dlPre = dlTm.preNode;  

                        dlPre.nextNode = dlTm.nextNode;  

                        dlTm.nextNode.preNode = dlPre;  

                        dlTm = null;  

                    }  

                    else  

                    {  

                        this.firstNode = dlTm.nextNode;  

                        dlTm = null;  

                    }  

                    this.count--;  

                    break;  

                }  

                else  

                {  

                    dlTm = dlTm.nextNode;  

                }  

            }  

        }  

    }  

  

    /// <summary>  

    /// 向链表中添加指定的工序  

    /// </summary>  

    /// <param name="temp"></param>  

    public void Insert(int temp)  

    {  

        DListNode node = this.firstNode;  

        while (true)  

        {  

            if (node == null)  

            {  

                return;  

            }  

            else  

            {  

                DListNode newNode = new DListNode();  

                newNode.data = temp;  

                if (temp < node.data)  

                {  

                    if (node.preNode == null)  

                    {  

                        node.preNode = newNode;  

                        newNode.nextNode = node;  

                        newNode.preNode = null;  

                        this.firstNode = newNode;  

                    }  

                    else  

                    {  

                        newNode.preNode = node.preNode;  

                        newNode.nextNode = node;  

                        node.preNode.nextNode = newNode;  

                        node.preNode = newNode;  

                    }  

                    this.count++;  

                    break;  

                }  

                else  

                {  

                    node = node.nextNode;  

                }  

            }  

        }  

    }  

}  

  转自:http://blog.csdn.net/bydxyj/article/details/4394734

你可能感兴趣的:(asp.net)