C#链表,特别是insert操作那,自己写了好久才理清思路。

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

namespace 链表
{
    class Program
    {
        static void Main(string[] args)
        {
            Note<int> temp = new Note<int>();
            ILinkList<int> list = new ILinkList<int>(temp);
            list.Append(10);
            list.Append(30);
            list.Append(50);


            list.Insert(20, 2);


            Console.WriteLine(list.GetLength());

            list.GetAll();








        }
    }
    /// <summary>
    /// 数据模型Node 链表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class Note<T>
    {
        public T _data;
        public Note<T> next;

        public Note<T> Next
        {
            get { return next; }
            set { next = value; }
        }
        public Note()
        {
            _data = default(T);
            next = null;
        }


        public Note(T data)
        {
            this._data = data;
            this.next = null;

        }
        public Note(Note<T> next)
        {
            this.next = next;


        }
        public Note(T data, Note<T> next)
        {
            this._data = data;
            this.next = next;
        }

        //数据域属性

        public T data
        {
            get
            {
                return _data;
            }
            set
            {
                _data = value;
            }

        }




    }
    /// <summary>
    /// 对链表进行操作
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ILinkList<T>
    {
        public Note<T> head;

        public ILinkList()
        {
            head = null;
        }
        public ILinkList(Note<T> head)
        {
            this.head = head;

        }
        //是否为空
        public bool IsEmpty()
        {
            if (head == null)
            {
                return true;
            }
            else return false;
        }

        //获取长度
        public int GetLength()
        {
            if (IsEmpty())
            {
                return 0;
            }
            int i = 0;
            Note<T> temp = head;
            while (temp != null)
            {
                i++;
                temp = temp.next;

            }
            return i;
        }
        //追加
        public void Append(T item)
        {
            if (GetLength() >= 1) ///如果不是在头。就在最尾添加
            {
                Note<T> temp = head;
                while (temp.next != null) ///如果是最尾是代表temp.next!=null
                {
                    ///如果链表的下个不为空那么继续循环
                    ///
                    temp = temp.next;




                }
                temp.next = new Note<T>(item);


            }
            else
            {
                head = new Note<T>(item);




            }


        }
        public void Insert(T item, int id)
        {


            //Note<T> temp = head;
            Note<T> ID_Before = head;
            Note<T> ID_Last = null;
            Note<T> ID_Current = null;

            if (this.IsEmpty() || id < 1 || id > GetLength())
            {
                Console.WriteLine("List为空或者ID号不正确!");
                return;
            }

            //执行的操作,在ID号插入一个新的元素,那么他的next 是指向 id+1的数

            int i = 1;
            //如果要在第十例插入一个项·现在是到第九·第九的next指向新的.新的Next该指向第九的Next
            for (; i < id; i++)
            {

                ID_Last = ID_Before;  //保存第九个
                ID_Before = ID_Before.next; //取第九个的Next







            }
            //


            if (id == i)
            {
                //ID_Last = ID_Before.next;
                //ID_Last = ID_Before;

                //ID_Last = new Note<T>(item);
                //ID_Last.Next = ID_Before;

                //ID_Current.next = ID_Last;
                //ID_Before = ID_Current;

                ID_Current = new Note<T>(item);
                ID_Current.next = ID_Before;  //新项的next 为第九个的next


                ID_Last.next = ID_Current;  //第九个的next指向当前

            }











        }

        public void Delete(int id)
        {
            if (this.IsEmpty() || id < 1)
            {
                Console.WriteLine("List为空或者ID号不正确!");
                return;
            }
            Note<T> temp = head;
           

        }
        public void GetAll()
        {
            if (this.IsEmpty())
            {
                Console.WriteLine("List为空或者ID号不正确!");
                return;
            }
            Note<T> temp = head;
            for (int i = 0; temp != null; i++)
            {
                Console.WriteLine(temp._data.ToString());
                temp = temp.next;


            }

        }

        //public void Insert(T item, int id)
        //{

        //    ///判断坐标是不是空的或者ID号无效
        //    if (IsEmpty() || id < 1)
        //    {
        //        Console.WriteLine("List是空的或者坐标是错的.");
        //        return;
        //    }
        //    ///如果是在头上就直接添加,步聚.他自身是item,next指向head;
        //    if (id == 1)
        //    {
        //        Note<T> q = new Note<T>(item);

        //        q.next = head;
        //        head = q;
        //        return;

        //    }


        //    Note<T> p = head;
        //    Note<T> r = null;
        //    int j = 1;

        //    //如果数组不为空而且J号小于ID 号时进行定位.
        //    while (p.next != null && j < id)
        //    {
        //        r = p;
        //        p = p.next;
        //        j++;

        //    }

        //    if (j == id)
        //    {
        //        Note<T> q = new Note<T>(item);
        //        q.next = p;
        //        r.next = q;
        //    }


        //}
    }
}

原文地址:https://www.cnblogs.com/fat_li/p/1941050.html