我们一般只是学过C/C++的数据结构,想必学C#的学生会疑惑,为什么C#没有数据结构呢,那我来告诉你,其实C#也是有数据结构的,只不过我们不了解罢了,经过我半天的编程,终于把C#的数据结构写出来了,也大大增加了我对数据结构的理解,在这里提供出来,共享学习,共同进步!

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 控制台程序
{
    class CSharp数据结构
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello");
            //ListNode list = new ListNode(10);
            Creastlist list = new Creastlist();
            list.Append(1);
            list.Append(2);
            list.Delete();//从队尾删除一个数据
            Console.WriteLine(list.ListCount);
            list.Clear();
            Console.WriteLine(list.ListCount);
            list.Append(1);
            list.Append(2);
            list.Append(4);
            list.Append(3);
            list.MovePrevious();
            list.Append(5);
            list.MovePrevious();
            Console.WriteLine(list.GetCurrentValue());
            //打印输出当前链表中的数据
            list.ShowList(list);
            list.Clear();
            Console.WriteLine("\n"+"清空后的链表节点个数为:"+list.ListCount);
            Console.WriteLine("新建一个初始化好的链表:");
            String str = Console.ReadLine();
            Creastlist NewList=new Creastlist(str);
            Console.Write("当前初试换好的链表为:\n");
            NewList.ShowList(NewList);
            Console.Write("\n链表中节点的个数为:");
            Console.WriteLine(NewList.ListCount);
            Console.Read();
        }
    }
    //  结点类
    public class ListNode
    {
        public ListNode(int NewValue)
        {
            Value = NewValue;
        }

        ///


        /// 前一个节点
        ///

        public ListNode Previous;

        ///


        /// 后一个节点
        ///

 
        public ListNode Next;

        ///


        /// 值
        ///

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

        /// 初始化的字符串,并以逗号或者空格隔开
        public Creastlist(string str)
        {
            //构造函数
            //初始化
            char[] separator = { ',',' '};
            string[] s = str.Split(separator);
            Creastlist list = new Creastlist();
            foreach (string i in s)
            {
                int j=Convert.ToInt32(i);
                list.Append(j);
            }
            ListCountValue = s.Length;
            Tail = list.Current;
            Head = list.Head;
        }

        ///


        /// 头指针
        ///

        private ListNode Head;

        ///


        ///  尾指针
        ///

        private ListNode Tail;

        ///


        /// 当前指针
        ///

        private ListNode Current;

        ///


        /// 链表数据的个数
        ///

        private int ListCountValue;

        ///


        /// 尾部添加数据
        ///

        ///
        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;
        }
        ///


        /// 删除当前的数据
        ///

        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;
            }
        }

        ///


        /// 向后移动一个数据
        ///

        public void MoveNext()
        {
            if (!IsEof()) Current = Current.Next;
        }

        ///


        /// 向前移动一个数据
        ///

        public void MovePrevious()
        {
            if (!IsBof()) Current = Current.Previous;
        }

        ///


        /// 移动到第一个数据
        ///

        public void MoveFrist()
        {
            Current = Head;
        }


        ///


        ///  移动到最后一个数据
        ///

        public void MoveLast()
        {
            Current = Tail;
        }

        ///


        /// 判断是否为空链表
        ///

        /// 返回bool值
        public bool IsNull()
        {
            if (ListCountValue == 0)
                return true;

            return false;
        }

        ///


        /// 判断是否为到达尾
        ///

        ///
        public bool IsEof()
        {
            if (Current == Tail)
                return true;
            return false;
        }

        ///


        /// 判断是否为到达头部
        ///

        ///
        public bool IsBof()
        {
            if (Current == Head)
                return true;

            return false;
        }
       
        ///


        /// 获取当前数据
        ///

        /// 返回当前节点的值
        public int GetCurrentValue()
        {
            return Current.Value;
        }

        ///


        ///  取得链表的数据个数
        ///

        public int ListCount
        {
            get
            {
                return ListCountValue;
            }
        }


        ///


        /// 清空链表
        ///

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


        ///


        ///  在当前位置前插入数据
        ///

        /// 要插入的数据
        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;
        }

        ///


        /// 进行升序插入
        ///

        ///
 
        public void InsertAscending(int InsertValue)
        {
            //参数: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();
            }
        }

        //进行降序插入
        public void InsertUnAscending(int InsertValue)
        {
            //参数: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();
            }
        }

        ///


        /// 打印链表中全部的数
        ///

        /// 链表参数
        public void ShowList(Creastlist list)
        {
            Console.WriteLine("当前链表中数据为:");
            list.MoveFrist();
            for (int i = 0; i < list.ListCount;i++ )
            {
                Console.Write(list.GetCurrentValue() + "  ");
                list.MoveNext();
            }
        }

    }

}


如有不当之处,请指正!QQ:1830916711