日新阁

取日三省之意,记生活珠玑,每日清新,谓日新阁.

 

C#数据结构链表的实现

下面是实现的代码
using System;
namespace dataStrut
{
    class Program
    {
        static void Main(string[] args)
        {
          //输出链表
            ListClass lc = new ListClass();
            lc.AppendNode(1);
            lc.AppendNode(2);
            lc.MoveFrist();
            for (int i = 0; i < lc.ListCount; i++)
            {
               
                Console.WriteLine(lc.GetCurrentValue());
                lc.MoveNextNode();
            }
        }
    }
    //定义节点
    public class ListNode
    {
        public ListNode(int NewValue)
        {
            Value = NewValue;
        }
        /// <summary>
        /// PreviousNode node
        /// </summary>
        public ListNode PreviousNode;
        /// <summary>
        /// NextNodeNode
        /// </summary>
        public ListNode NextNode;
        /// <summary>
        /// Value
        /// </summary>
        public int Value;

    }
    public class ListClass
    {
        /// <summary>
        /// Init Data
        /// </summary>
        public ListClass()
        {
            ListCountValue = 0;
            Head = null;
            Last = null;
        }
        /// <summary>
        /// Head
        /// </summary>
        private ListNode Head;
        /// <summary>
        /// last
        /// </summary>
        private ListNode Last;
        /// <summary>
        /// current
        /// </summary>
        private ListNode Current;
        /// <summary>
        /// list number
        /// </summary>
        private int ListCountValue;
        /// <summary>
        /// add number to last
        /// </summary>
        public void AppendNode(int DataValue)
        {
            ListNode NewNode = new ListNode(DataValue);
            if (IsNull())
            //first null
            {
                Head = NewNode;
                Last = NewNode;
            }
            else
            {
                Last.NextNode = NewNode;
                NewNode.PreviousNode = Last;
                Last = NewNode;
            }
            Current = NewNode;
            //list add one
            ListCountValue += 1;
        }
        /// <summary>
        /// delete now
        /// </summary>
        public void Delete()
        {
            //Null
            if (!IsNull())
            {
                //delFirst
                if (IsBof())
                {
                    Head = Current.NextNode;
                    Current = Head;
                    ListCountValue -= 1;
                    return;
                }
                //delLast
                if (IsEof())
                {
                    Last = Current.PreviousNode;
                    Current = Last;
                    ListCountValue -= 1;
                    return;
                }
                //delMiddle
                Current.PreviousNode.NextNode = Current.NextNode;
                Current = Current.PreviousNode;
                ListCountValue -= 1;
                return;
            }
        }
        /// <summary>
        /// moveNext
        /// </summary>
        public void MoveNextNode()
        {
            if (!IsEof()) Current = Current.NextNode;
        }
        /// <summary>
        ///movePreviouse
        /// </summary>
        public void MovePreviousNode()
        {
            if (!IsBof()) Current = Current.PreviousNode;
        }
        /// <summary>
        /// movefirst
        /// </summary>
        public void MoveFrist()
        {
            Current = Head;
        }
        /// <summary>
        /// moveLast
        /// </summary>
        public void MoveLast()
        {
            Current = Last;
        }
        /// <summary>
        /// check null
        /// </summary>
        public bool IsNull()
        {
            if (ListCountValue == 0)
                return true;
            return false;
        }
        /// <summary>
        /// is last
        /// </summary>
        public bool IsEof()
        {
            if (Current == Last)
                return true;
            return false;
        }
        /// <summary>
        /// is head
        /// </summary>
        public bool IsBof()
        {
            if (Current == Head)
                return true;
            return false;
        }
        public int GetCurrentValue()
        {
            return Current.Value;
        }
        /// <summary>
        /// get list node number
        /// </summary>
        public int ListCount
        {
            get
            {
                return ListCountValue;
            }
        }
        /// <summary>
        /// clear list
        /// </summary>
        public void Clear()
        {
            MoveFrist();
            while (!IsNull())
            {
              
                Delete();
            }
        }
        /// <summary>
        /// 在当前位置前插入数据
        /// </summary>
        public void Insert(int DataValue)
        {
            ListNode NewNode = new ListNode(DataValue);
            if (IsNull())
            {
               
                AppendNode(DataValue);
                return;
            }
            if (IsBof())
            {
               
                NewNode.NextNode = Head;
                Head.PreviousNode = NewNode;
                Head = NewNode;
                Current = Head;
                ListCountValue += 1;
                return;
            }
          
            NewNode.NextNode = Current;
            NewNode.PreviousNode = Current.PreviousNode;
            Current.PreviousNode.NextNode = NewNode;
            Current.PreviousNode = NewNode;
            Current = NewNode;
            ListCountValue += 1;
        }
        /// <summary>
        /// ads insert
        /// </summary>
        public void InsertAscending(int InsertValue)
        {
        
            if (IsNull())
            {
               
                AppendNode(InsertValue);
                return;
            }
          
            MoveFrist();
            if ((InsertValue < GetCurrentValue()))
            {
             
                Insert(InsertValue);
                return;
            }
            while (true)
            {
                if (InsertValue < GetCurrentValue())
                {
                  
                    Insert(InsertValue);
                    break;
                }
                if (IsEof())
                {
                   
                    AppendNode(InsertValue);
                    break;
                }
            
                MoveNextNode();
            }
        }
        /// <summary>
        /// desc insert
        /// </summary>
        public void InsertUnAscending(int InsertValue)
        {
            if (IsNull())
            {
              
                AppendNode(InsertValue);
                return;
            }
          
            MoveFrist();
            if (InsertValue > GetCurrentValue())
            {
              
                Insert(InsertValue);
                return;
            }
            while (true)
            {
                if (InsertValue > GetCurrentValue())
                {
                   
                    Insert(InsertValue);
                    break;
                }
                if (IsEof())
                {
                  
                    AppendNode(InsertValue);
                    break;
                }
              
                MoveNextNode();
            }


        }
    }
}

也可以用NET下的List类

posted on 2006-03-28 23:27  flashicp  阅读(3236)  评论(0编辑  收藏  举报

导航