链表C#实现

    /// <summary>
    
/// 结点类
    
/// Summary description for ListNode.
    
/// </summary>
    public class ListNode
    {
        
public ListNode(int NewValue)
        {
            Value 
= NewValue;
        }
 
        
public ListNode Previous;// 前一个
        public ListNode Next;// 后一个
        public int Value;// 值
    }


    
/// <summary>
    
/// 链表类
    
/// -----------------------------------------------------------------
    
///   定义结点之后,开始类线性表的操作编程了.在LIST 类中,采用了,
    
/// Head ,Tail,Current,三个指针,使用Append,MoveFrist,MovePrevious,
    
/// MoveNext,MoveLast,Delete,InsertAscending,InsertUnAscending
    
/// ,Clear实现移动,添加,删除,升序插入,降序插入,
    
///  清空链表操作,GetCurrentValue() 方法取得当前的值。
    
/// </summary>
    public class Clist
    {
        
public Clist()
        {
            
//构造函数
            
//初始化
            ListCountValue = 0;
            Head 
= null;
            Tail 
= null;
        }

        
private ListNode Head;// 头指针
        private ListNode Tail;// 尾指针
        private ListNode Current;// 当前指针
        private int ListCountValue;// 链表数据的个数

        
/// <summary>
        
/// 尾部添加数据
        
/// </summary>
        public void Append(int DataValue)
        {
            ListNode NewNode 
= new ListNode(DataValue);

            
if (IsNull()) //如果头指针为空
            {
                Head 
= NewNode;
                Tail 
= NewNode;
            }
            
else
            {
                Tail.Next 
= NewNode;
                NewNode.Previous 
= Tail;
                Tail 
= NewNode;
            }
            Current 
= NewNode;
            ListCountValue 
+= 1;//链表数据个数加一
        }

        
/// <summary>
        
/// 删除当前的数据
        
/// </summary>
        public void Delete()
        {
            
if (!IsNull())//若为空链表
            {
                
if (IsBof())//若删除头
                {
                    Head 
= Current.Next;
                    Current 
= Head;
                    ListCountValue 
-= 1;
                    
return;
                }
                
if (IsEof())//若删除尾
                {
                    Tail 
= Current.Previous;
                    Current 
= Tail;
                    ListCountValue 
-= 1;
                    
return;
                }
                
//若删除中间数据
                Current.Previous.Next = Current.Next;
                Current 
= Current.Previous;
                ListCountValue 
-= 1;

                
return;
            }
        }

        
/// <summary>
        
/// 向后移动一个数据
        
/// </summary>
        public void MoveNext()
        {
            
if (!IsEof())
            {
                Current 
= Current.Next;
            }
        }

        
/// <summary>
        
/// 向前移动一个数据
        
/// </summary>
        public void MovePrevious()
        {
            
if (!IsBof())
            {
                Current 
= Current.Previous;
            }
        }

        
/// <summary>
        
/// 移动到第一个数据
        
/// </summary>
        public void MoveFrist()
        {
            Current 
= Head;
        }

        
/// <summary>
        
/// 移动到最后一个数据
        
/// </summary>
        public void MoveLast()
        {
            Current 
= Tail;
        }

        
/// <summary>
        
/// 判断是否为空链表
        
/// </summary>
        public bool IsNull()
        {
            
if (ListCountValue == 0)
            {
                
return true;
            }
            
else
            {
                
return false;
            }
        }

        
/// <summary>
        
/// 判断是否为到达尾部
        
/// </summary>
        public bool IsEof()
        {
            
if (Current == Tail)
            {
                
return true;
            }
            
else
            {
                
return false;
            }
        }

        
/// <summary>
        
/// 判断是否为到达头部
        
/// </summary>
        public bool IsBof()
        {
            
if (Current == Head)
            {
                
return true;
            }
            
else
            {
                
return false;
            }
        }

        
public int GetCurrentValue()
        {
            
return Current.Value;
        }

        
/// <summary>
        
/// 取得链表的数据个数
        
/// </summary>
        public int ListCount
        {
            
get
            {
                
return ListCountValue;
            }
        }

        
/// <summary>
        
/// 清空链表
        
/// </summary>
        public void Clear()
        {
            MoveFrist();
            
while (!IsNull())
            {
                Delete();
//若不为空链表,从尾部删除
            }
        }

        
/// <summary>
        
/// 在当前位置前插入数据
        
/// </summary>
        public void Insert(int DataValue)
        {
            ListNode NewNode 
= new ListNode(DataValue);
            
if (IsNull())
            {
                Append(DataValue);
//为空表,则添加
                return;
            }

            
if (IsBof())
            {
                
//为头部插入
                NewNode.Next = Head;
                Head.Previous 
= NewNode;
                Head 
= NewNode;
                Current 
= Head;
                ListCountValue 
+= 1;

                
return;
            }

            
//中间插入
            NewNode.Next = Current;
            NewNode.Previous 
= Current.Previous;
            Current.Previous.Next 
= NewNode;
            Current.Previous 
= NewNode;
            Current 
= NewNode;
            ListCountValue 
+= 1;

        }

        
/// <summary>
        
/// 进行升序插入
        
/// 参数:InsertValue 插入的数据
        
/// </summary>
        public void InsertAscending(int InsertValue)
        {
            
if (IsNull())//为空链表
            {
                Append(InsertValue);
//添加
                return;
            }

            MoveFrist();
//移动到头

            
if ((InsertValue < GetCurrentValue()))
            {
                Insert(InsertValue);
//满足条件,则插入,退出
                return;
            }

            
while (true)
            {
                
if (InsertValue < GetCurrentValue())
                {
                    Insert(InsertValue);
//满族条件,则插入,退出
                    break;
                }

                
if (IsEof())
                {
                    Append(InsertValue);
//尾部添加
                    break;
                }

                MoveNext();
//移动到下一个指针

            }
        }

        
/// <summary>
        
/// 进行降序插入
        
/// 参数:InsertValue 插入的数据
        
/// </summary>
        public void InsertUnAscending(int InsertValue)
        {
            
if (IsNull())//为空链表
            {
                Append(InsertValue);
//添加
                return;
            }

            MoveFrist();
//移动到头

            
if (InsertValue > GetCurrentValue())
            {
                Insert(InsertValue);
//满足条件,则插入,退出
                return;
            }

            
while (true)
            {
                
if (InsertValue > GetCurrentValue())
                {
                    Insert(InsertValue);
//满族条件,则插入,退出
                    break;
                }

                
if (IsEof())
                {
                    Append(InsertValue);
//尾部添加
                    break;
                }

                MoveNext();
//移动到下一个指针
            }
        }
    }
posted @ 2009-06-10 11:36  TONYBINLJ  阅读(700)  评论(1编辑  收藏  举报