新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     >>计算机科学论坛<<     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论.NET,C#,ASP,VB技术
    [返回] 计算机科学论坛计算机技术与应用『 Dot NET,C#,ASP,VB 』 → 双向链表的C#实现(改进版本) 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 3495 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 双向链表的C#实现(改进版本) 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     admin 帅哥哟,离线,有人找我吗?
      
      
      
      威望:9
      头衔:W3China站长
      等级:计算机硕士学位(管理员)
      文章:5255
      积分:18407
      门派:W3CHINA.ORG
      注册:2003/10/5

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给admin发送一个短消息 把admin加入好友 查看admin的个人资料 搜索admin在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给admin  访问admin的主页 引用回复这个贴子 回复这个贴子 查看admin的博客楼主
    发贴心情 双向链表的C#实现(改进版本)


    发信人: Jobs (温少), 信区: DotNET        
    标  题: 双向链表的C#实现(改进版本)
    发信站: BBS 水木清华站 (Sun Jan  5 13:57:15 2003)


    /**
    *  
    *  

        @author     &lt;a href=&quot;wen">mailto:szuJobs@hotmail.com">wenshaojin&lt;/a&gt;
        @created    January 2, 2003
        @version    1

    *
    **/
    using System;
    using System.Collections;
    using System.IO;

    using Matrix.Common.Utils;

    namespace Matrix.Common.Collections
    {
        /// &lt;summary&gt;
        /// 双向链表
        /// &lt;/summary&gt;
        /// &lt;remarks&gt;
        /// 在需要经常中间插入和删除的数据列表,使用双向链表比ArrayList要快
        /// &lt;/remarks&gt;
        public class DoublyLinkedList : IEnumerable
        {
            private DoublyLinkedListNode _first;
            private int _count;

            public DoublyLinkedList()
            {
                _count = 0;
                _first = null;
            }

            /// &lt;summary&gt;
            /// 链表中节点的数量
            /// &lt;/summary&gt;
            public int Count
            {
                get
                {
                    return _count;
                }
            }

            public object this[int pos]
            {
                get
                {
                    if(pos &lt; 0 || pos &gt;= this._count)
                    {
                        throw new ArgumentOutOfRangeException(&quot;pos&quot;);
                    }

                    DoublyLinkedListNode current = this._first;

                    int index = 0;

                    while(index &lt; pos &amp;&amp; current != null)
                    {
                        index ++;
                        current = current.Right;
                    }

                    return current.Data;
                }
                set
                {
                    if(pos &lt; 0 || pos &gt;= this._count)
                    {
                        throw new ArgumentOutOfRangeException(&quot;pos&quot;);
                    }

                    DoublyLinkedListNode current = this._first;

                    int index = 0;

                    while(index &lt; pos &amp;&amp; current != null)
                    {
                        index ++;
                        current = current.Right;
                    }

                    current.Data = value;
                }
            }

            private DoublyLinkedListNode FindNode(int pos)
            {
                //p will eventually point to k'th node
                DoublyLinkedListNode p = this._first;
                for(int i=0; i&lt;pos &amp;&amp; p != null; i++) // move p to pos'th
                {
                    p = p.Right;
                }

                if(pos &gt; 0 &amp;&amp; p == null) // no pos'th
                {
                    throw new ArgumentOutOfRangeException(&quot;pos&quot;);
                }

                return p;
            }

            private void InsertBefore(DoublyLinkedListNode p, object data)
            {
                DoublyLinkedListNode y = new DoublyLinkedListNode();
                y.Data = data;

                y.Left = p.Left;
                y.Right = p;
                p.Left.Right = y;
                p.Left = y;
            }

            private void Remove(DoublyLinkedListNode p)
            {
                if(p == this._first)
                {
                    this._first = this._first.Right;
                    this._first.Left = null;
                }
                else if(p.Right == null)
                {
                    p.Left.Right = null;
                }
                else
                {
                    p.Left.Right = p.Right;
                    p.Right.Left = p.Left;
                }
            }

            /// &lt;summary&gt;
            /// 从链表中删除指定元素
            /// &lt;/summary&gt;
            /// &lt;param name=&quot;pos&quot;&gt;&lt;/param&gt;
            public void RemoveAt(int pos)
            {
                if(pos &lt; 0)
                {
                    throw new ArgumentOutOfRangeException(&quot;pos&quot;);
                }

                DoublyLinkedListNode p = FindNode(pos);

                if(p == null)
                {
                    throw new ArgumentOutOfRangeException(&quot;pos&quot;);
                }

                this.Remove(p);

                _count --;
            }

            /// &lt;summary&gt;
            /// 查找删除
            /// &lt;/summary&gt;
            /// &lt;param name=&quot;obj&quot;&gt;&lt;/param&gt;
            /// &lt;returns&gt;返回删除节点数量&lt;/returns&gt;
            public int Remove(object item)
            {
                int itemCount = 0;
                DoublyLinkedListNode p = this._first;

                while(p != null)
                {
                    object pData = p.Data;
                    if(item == pData  
                        || pData!= null &amp;&amp; pData.Equals(item)
                        )
                    {
                        if(p == this._first)
                        {
                            this._first = this._first.Right;
                            this._first.Left = null;
                        }
                        else if(p.Right == null)
                        {
                            p.Left.Right = null;
                        }
                        else
                        {
                            p.Left.Right = p.Right;
                            p.Right.Left = p.Left;
                        }

                        this._count --;
                        itemCount ++;
                    }

                    p = p.Right;
                }

                return itemCount;
            }


            public bool Contains(object obj)
            {
                DoublyLinkedListNode p = this._first;

                while(p != null)
                {
                    object pData = p.Data;
                    if(obj == pData  
                        || pData!= null &amp;&amp; pData.Equals(obj)
                        )
                    {
                        return true;
                    }

                    p = p.Right;
                }

                return false;
            }


            public int IndexOf(object obj)
            {
                int itemIndex = 0;
                DoublyLinkedListNode p = this._first;

                while(p != null)
                {
                    object pData = p.Data;
                    if(obj == pData  
                        || pData!= null &amp;&amp; pData.Equals(obj)
                        )
                    {
                        return itemIndex;
                    }

                    p = p.Right;
                    itemIndex ++;
                }

                return -1;
            }

            public void Insert(int pos, object data)
            {
                if(pos &lt; 0)
                {
                    throw new ArgumentOutOfRangeException(&quot;pos&quot;);
                }

                if(pos == 0)
                {
                    DoublyLinkedListNode y = new DoublyLinkedListNode();
                    y.Data = data;

                    if(this._first == null)
                    {
                        this._first = y;
                    }
                    else
                    {
                        y.Right = this._first;
                        this._first.Left = y;
                        this._first = y;
                    }
                }
                else
                {
                    DoublyLinkedListNode p = FindNode(pos);

                    InsertBefore(p, data);
                }

                _count ++;
            }

            public void MoveToFirst(int pos)
            {
                if(pos &lt; 0 || this._first == null)
                {
                    throw new ArgumentOutOfRangeException(&quot;pos&quot;); //// no pos'th
                }

                if(pos == 0)
                {
                    return;
                }

                DoublyLinkedListNode q = FindNode(pos);

                if(q == null)
                {
                    throw new ArgumentOutOfRangeException(&quot;pos&quot;);
                }

                this.Remove(q);

                this._first.Left = q;
                q.Right = this._first;
                q.Left = null;
                this._first = q;
            }

            public void Swap(int posA, int posB)
            {
                if(posA &lt;0 || posA &gt;= this._count)
                {
                    throw new ArgumentOutOfRangeException(&quot;posA&quot;);
                }

                if(posB &lt;0 || posB &gt;= this._count)
                {
                    throw new ArgumentOutOfRangeException(&quot;posB&quot;);
                }

                DoublyLinkedListNode p = FindNode(posA);
                DoublyLinkedListNode q = FindNode(posB);

                this.Swap(p, q);
            }

            private void Swap(DoublyLinkedListNode p , DoublyLinkedListNode q)
            {
                if(p == q)
                {
                    return;
                }

                DoublyLinkedListNode pLeft = p.Left;
                DoublyLinkedListNode pRight = p.Right;
                DoublyLinkedListNode qLeft = q.Left;
                DoublyLinkedListNode qRight = q.Right;

                if(qRight == p)
                {
                    p.Left = qLeft;
                    if(qLeft != null)
                    {
                        qLeft.Right = p;
                    }

                    if(pRight != null)
                    {
                        pRight.Left = q;
                    }
                    q.Right = pRight;

                    q.Left = p;
                    p.Right = q;

                }
                else if(pRight == q)
                {
                    q.Left = pLeft;
                    if(pLeft != null)
                    {
                        pLeft.Right = q;
                    }

                    if(qRight != null)
                    {
                        qRight.Left = p;
                    }
                    p.Right = qRight;

                    p.Left = q;
                    q.Right = p;
                }
                else
                {
                    q.Left = pLeft;
                    if(pLeft != null)
                    {
                        pLeft.Right = q;
                    }

                    q.Right = pRight;
                    if(pRight != null)
                    {
                        pRight.Left = q;
                    }

                    p.Left = qLeft;
                    if(qLeft != null)
                    {
                        qLeft.Right = p;
                    }

                    p.Right = qRight;
                    if(qRight != null)
                    {
                        qRight.Left = p;
                    }

                }

                if(this._first == p)
                {
                    this._first = q;
                }
                else if(this._first == q)
                {
                    this._first = p;
                }
            }

            public void Add(object data)
            {
                DoublyLinkedListNode y = new DoublyLinkedListNode();
                y.Data = data;

                if(this._first == null)
                {
                    this._first = y;
                    _count ++;
                    return;
                }

                DoublyLinkedListNode current = this._first;

                while(current.Right != null)
                {
                    current = current.Right;
                }

                current.Right = y;
                y.Left = current;

                _count ++;
            }

            /// &lt;summary&gt;
            ///  
            /// &lt;/summary&gt;
            /// &lt;param name=&quot;c&quot;&gt;&lt;/param&gt;
            public void AddRange(ICollection c)
            {
                foreach(object obj in c)
                {
                    this.Add(obj);
                }
            }

            public void Sort()
            {
                object[] array = new object[this._count];
                this.CopyTo(array);

                Array.Sort(array);
                 
                this.SetRange(0, array);
            }

            public void Sort(IComparer comparer)
            {
                object[] array = new object[this._count];
                this.CopyTo(array);

                Array.Sort(array, comparer);
                 
                this.SetRange(0, array);
            }

            public void SetRange(int index, ICollection c)
            {
                if(index &lt; 0 || index &gt;= this._count)
                {
                    throw new ArgumentOutOfRangeException(&quot;index&quot;);
                }

                DoublyLinkedListNode p = this._first;
                int itemIndex = 0;
                while(p != null)
                {
                    if(itemIndex &gt;= index)
                    {
                        break;
                    }

                    p = p.Right;
                    itemIndex ++;
                }

                if(itemIndex &lt; index || p == null)
                {
                    return;
                }

                IEnumerator e = c.GetEnumerator();

                while(e.MoveNext() &amp;&amp; p != null)
                {
                    p.Data = e.Current;
                    p = p.Right;
                }
            }

            public void CopyTo(Array array)
            {
                DoublyLinkedListNode p = this._first;

                int itemIndex = 0;
                while(p != null)
                {
                    array.SetValue(p.Data, itemIndex);

                    p = p.Right;
                    itemIndex ++;
                }
            }

            public object[] ToArray()
            {
                object[] array = new object[this._count];
                this.CopyTo(array);
                return array;
            }

            public Array ToArray(Type type)
            {
                if(type == null)
                {
                    throw new ArgumentNullException(&quot;type&quot;);
                }

                Array array = Array.CreateInstance(type, this._count);
                this.CopyTo(array);
                return array;
            }

            public void Reverse()
            {
                object[] objArray = new object[this._count];

                this.CopyTo(objArray);

                Array.Reverse(objArray);

                this.SetRange(0, objArray);
            }

            /// &lt;summary&gt;
            /// 清除链表
            /// &lt;/summary&gt;
            public void Clear()
            {
                this._first = null;
                _count = 0;
            }

            IEnumerator IEnumerable.GetEnumerator()
            {
                return this.GetEnumerator();
            }

            public DoublyLinkedListEnumerator GetEnumerator()
            {
                return new DoublyLinkedListEnumerator(this);
            }

            private class DoublyLinkedListNode
            {
                public object Data;
                public DoublyLinkedListNode Left;
                public DoublyLinkedListNode Right;
            }

            public class DoublyLinkedListEnumerator : IEnumerator
            {
                private DoublyLinkedList _list;
                private DoublyLinkedListNode _location;

                public DoublyLinkedListEnumerator(DoublyLinkedList list)
                {
                    this._list = list;
                }

                public bool MoveNext()
                {
                    if(this._location == null)
                    {
                        this._location = _list._first;
                    }
                    else
                    {
                        this._location = this._location.Right;
                    }

                    return this._location != null;
                }

                public bool MovePrevious
                {
                    get
                    {
                        if(this._location == null)
                        {
                            this._location = _list._first;
                        }
                        else
                        {
                            this._location = this._location.Left;
                        }

                        return this._location != null;
                    }
                }

                public void Reset()
                {
                    this._location = _list._first;
                }

                public object Current
                {
                    get
                    {
                        return this._location.Data;
                    }
                }
            }
        }
    }

    --

    ※ 来源:·BBS 水木清华站 smth.org·[FROM: 218.17.72.63]
    上一篇
    返回上一页
    回到目录
    回到页首
    下一篇


       收藏   分享  
    顶(0)
      




    ----------------------------------------------

    -----------------------------------------------

    第十二章第一节《用ROR创建面向资源的服务》
    第十二章第二节《用Restlet创建面向资源的服务》
    第三章《REST式服务有什么不同》
    InfoQ SOA首席编辑胡键评《RESTful Web Services中文版》
    [InfoQ文章]解答有关REST的十点疑惑

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2004/11/9 2:25:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Dot NET,C#,ASP,VB 』的所有贴子 点击这里发送电邮给Google AdSense  访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2025/7/23 0:39:17

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    156.006ms