zoukankan      html  css  js  c++  java
  • 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类

  • 相关阅读:
    跟踪创建类的个数
    动手动脑3
    动手动脑:随机数发生器和函数重载
    统计英语文章中单词
    动手动脑(1)
    原码、反码、补码
    java测试ATM自助操作系统
    深入浅出 TCP/IP 协议栈
    十大经典排序算法(动图演示)
    深入浅出 Viewport 设计原理
  • 原文地址:https://www.cnblogs.com/flashicp/p/361337.html
Copyright © 2011-2022 走看看