C#鏈表,特別是insert操作那,自己寫了好久才理清思路。

來源:互聯網
上載者:User

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號小於識別碼時進行定位.
        //    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;
        //    }

        //}
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.