涂雅[博客园]
最新文章请访问独立博客:http://iove.net
周未在家没事,封装了一个链表与队列操作的类,主要目的是实现对链表基本操作的封装。本人才疏学浅,如果有写得不好的地方,还望大家指正。类及方法属性如下图:
LinkedList类,链表操作的类,包括插入、删除、移动、获取节点数据等操作;ListNode类,封装在ListedList中的私用类,目的是不让外部访问,因为外部只需要操作数据即可;Queue类,队列操作,只有入队与出队两个方法,先进先出原则;NodeData类,这个类很重要,虽然只有有一个Dispose方法,这个类是一个抽象类,只允许继承。目的是扩展链表的数据功能而无需对链表的基本功能进行修改,面向对象一个很重要的思想就是增加功能不需要修改原来的代码。在本例中,我们就可以通过继承NodeData来扩展NodeData类。

 using System;   
 
 namespace Conis.Squirrel.FrameWork
 {
     #region 节点数据的基类
     public abstract class NodeData
     {
         public void Dispose()
         {
             GC.SuppressFinalize(this);
         }
     }
     #endregion  
 
     #region 链表的基本操作
     ///
     /// LinkedList 的摘要说明
     ///
     public class LinkedList
     {
         #region 链表节点基类
         private class ListNode
         {
             public ListNode Previous;
             public ListNode Next;
             public NodeData Data;   
 
             public void Dispose()
             {
                 GC.SuppressFinalize(this);
             }
         }
         #endregion  
 
         #region 属性
         private ListNode _Current;
         private int _Count;
         private ListNode _First;
         private ListNode _Last;   
 
         ///
         /// 第一个节点
         ///
         public NodeData First
         {
             get
             {
                 return _First.Data;
             }
         }   
 
         ///
         /// 获取最后一个节点
         ///
         public NodeData Last
         {
             get
             {
                 return _Last.Data;
             }
         }   
 
         ///
         /// 节点总数
         ///
         public int Count
         {
             get
             {
                 return _Count;
             }
         }   
 
         ///
         /// 获取当前节点的数据
         ///
         public NodeData Current
         {
             get
             {
                 return _Current.Data;
             }
         }
         #endregion   
 
         public LinkedList()
         {
             _First = null;
             _Last = null;
             _Current = null;
             _Count = 0;
         }   
 
         ///
         /// 检查是否有第一个节点
         ///
         ///
         public bool Bof()
         {
             return _Current == _First;
         }   
 
         ///
         /// 检查是否有最后一个节点
         ///
         ///
         public bool Eof()
         {
             return _Current == _Last;
         }
         ///
         /// 添加一个新节点
         ///
         ///
<span> </span>节点数据
         /// 返回新加入节点的索引
         public int Append(NodeData data)
         {
             ListNode node = new ListNode();
             node.Data = data;
             node.Next = null;
             //空链表
             if (_Count == 0)
             {
                 node.Previous = null;
                 _First = node;
             }
             else
             {
                 node.Previous = _Last;
                 _Last.Next = node;
             }
             _Last = node;
             //_Current = node;
             _Count++;
             return _Count;
         }   
 
         ///
         /// 在指定位置插入节点
         ///
         ///
<span> </span>节点数据
         ///
<span> </span>要插入的位置
         public void Insert(NodeData data, int index)
         {
             //超出范围
             if (index &lt; 0 || index &gt; _Count)
             {
                 throw new Exception("Index out range");
             }   
 
             //索引在最后位置,实际就是附�
             if (index == _Count)
             {
                 this.Append(data);
                 return;
             }   
 
             //插入到第一个位置或者中间的位置
             ListNode node = new ListNode();
             node.Data = data;
             if (index == 0)
             {
                 //插到第一个位置
                 node.Next = _First;
                 node.Previous = null;
                 _First.Previous = node;
                 _First = node;
             }
             else
             {
                 ListNode cur = _Current;
                 this.Move(index);
                 node.Previous = _Current.Previous;
                 _Current.Previous.Next = node;
                 node.Next = _Current;
                 _Current.Previous = node;
                 _Current = cur;
             }
             _Count++;
         }   
 
         ///
         /// 删除指定位置的节点
         ///
         ///
要删除的位置
         public void Delete(int index)
         {
             //超出范围
             if (index &lt; 0 || index &gt; _Count)
             {
                 throw new Exception("Index out range");
             }   
 
             if (index == 0)                                 //删除第一个链表节点
             {
                 if (this.Bof())                             //如果当前节点是第一个节点则设置Currrent设置为null
                 {
                     _Current = null;
                 }
                 _First = _First.Next;
                 _First.Previous = null;
             }
             else if (index == _Count - 1)                  //当前是链表尾
             {
                 if (this.Eof())                             //如果当前节点是最后一个节点则设置Currrent设置为null
                 {
                     _Current = null;
                 }
                 _Last = _Last.Previous;
                 _Last.Next = null;
             }
             else
             {
                 ListNode cur = _Current;
                 this.Move(index);
                 _Current.Previous.Next = _Current.Next;
                 _Current.Next.Previous = _Current.Previous;
                 if (cur == _Current)                            //当前节点点就是要删除的节点
                 {
                     _Current = null;
                 }
                 else
                 {
                     _Current = cur;
                 }
             }
             _Count--;
         }   
 
         ///
         /// 移至下一个节点
         ///
         /// 返回新移动的节点数据
         public NodeData MoveNext()
         {
             //当前已经是最后一个节点
             if (this.Eof())
             {
                 return null;
             }
             else
             {
                 _Current = _Current.Next;
                 return _Current.Data;
             }
         }   
 
         ///
         /// 移至上一个节点
         ///
         /// 返回新移动的节点数据
         public NodeData MovePrevious()
         {
             //当前已经是最前节眯
             if (this.Bof())
             {
                 return null;
             }
             else
             {
                 _Current = _Current.Previous;
                 return _Current.Data;
             }   
 
         }   
 
         ///
         /// 移动到第一个节点
         ///
         /// 返回新移动的节点数据
         public NodeData MoveFirst()
         {
             _Current = _First;
             return _Current == null ? null : _Current.Data;
         }   
 
         ///
         /// 移动第最后一个节点
         ///
         ///
         public NodeData MoveLast()
         {
             _Current = _Last;
             return _Current == null ? null : _Current.Data;
         }   
 
         ///
         /// 移动到指定位置
         ///
         ///
移动位置
         /// 返回新移动的节点数据
         public NodeData Move(int index)
         {
             //索引超出范围,返回null
             if (index &lt; 0 || index &gt;= _Count)
             {
                 return null;
             }   
 
             //移动到第一个节点
             if (index == 0)
             {
                 this.MoveFirst();
                 return _Current.Data;
             }   
 
             //移动到最后一个节点
             if (index == _Count - 1)
             {
                 this.MoveLast();
                 return _Current.Data;
             }   
 
             this.MoveFirst();
             ///移动到中间的节点
             for (int iLoop = 0; iLoop &lt; _Count; iLoop++)
             {
                 ListNode a = _Current;
                 if (iLoop == index)
                 {
                     //_Current = _Current.Next;
                     return _Current.Data;
                 }
                 this.MoveNext();        //向下移动
             }
             return null;
         }   
 
         ///
         /// 获取指定位置的节点
         ///
         ///
要获取的位置
         /// 返回获取的节点数据,如果没有这个位置,由返回null
         public NodeData GetNodeData(int index)
         {
             ListNode cur = _Current;
             NodeData result = this.Move(index);
             _Current = cur;
             return result;
         }   
 
     }
     #endregion   
 
     ///
     /// 队列操作,先进先出原则
     ///
     public class Queue
     {
         private LinkedList _List;
         private Queue() { }   
 
         public int Count
         {
             get
             {
                 return _List == null ? 0 : _List.Count;
             }
         }   
 
         ///
         /// 入队
         ///
         ///
 
         public void Push(NodeData data)
         {
             if (_List == null)
             {
                 _List = new LinkedList();
             }
             _List.Append(data);
         }   
 
         ///
         /// 出队
         ///
         ///
         public NodeData Pop()
         {
             //空链表,返回null
             if (_List == null || _List.Count == 0)
             {
                 return null;
             }   
 
             NodeData data = _List.First;
             _List.Delete(0);
             return data;
         }
     }
 }

Note: There is a file embedded within this post, please visit this post to download the file.

  

注意:本文为我的独立博客镜像博客,自发表不再更新,原文可能随时被更新,敬请访问原文。同时,请大家不要在此评论,如果有什么看法,请点击这里:http://iove.net/1705/

本文来自http://iove.net,欢迎转载,转载敬请保留相关链接,否则视为侵权,原文链接:http://iove.net/1705/

posted on 2007-09-10 10:44  Conis  阅读(221)  评论(0)    收藏  举报