C# 链表结构

using System;

namespace UnilateralismChainTable
{

    public class ListNode   // 结点类
    {
        public ListNode(int NewValue)
        {
            Value = NewValue;
        }
        public ListNode Previous;      //前一个
        public ListNode Next;      //后一个
        public int Value;              //
    }
    public class Clist
    {
        public Clist()
        {
            ListCountValue = 0;//初始化
            Head = null;
            Tail = null;
        }

        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 MovePrevious()
        {
            if (!IsBof()) Current = Current.Previous;//向前移动一个数据
        }
        public void MoveFrist()
        {
            Current = Head;//移动到第一个数据
        }
        public void MoveLast()
        {
            Current = Tail;//移动到最后一个数据
        }
        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 MoveNext()
        {
            if (!IsEof()) Current = Current.Next;//向后移动一个数据
        }

        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();//移动到下一个指针
            }
        }
    }
 
}

说实话 没大懂  还需要慢慢研究

参照了一波

https://blog.csdn.net/qq_36981814/article/details/80604607?utm_medium=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-1.test_version_3&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-1.test_version_3

posted @ 2021-08-10 10:19  心中Where_True循环  阅读(318)  评论(0编辑  收藏  举报