C#實現–單鏈表(鏈式)

來源:互聯網
上載者:User
 

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

namespace SingleLinkedList
{
    class Program
    {
        static void Main(string[] args)
        {

             //執行個體調用

        }
    }

    //定義單鏈表的結點
    //結點儲存資料和下一個結點的地址(引用).這裡用一個類表示.
    public class Node<T>
    {
        private T data;  //資料
        private Node<T> next; //引用

        //構造器
        public Node(T val, Node<T> p)
        {
            data = val;
            next = p;
        }
       
        //構造器2
        public Node(Node<T> p)
        {
            next = p;
        }

        //構造器3
        public Node(T val)
        {
            data = val;
            next = null;
        }

        //構造器4
        public Node()
        {
            data = default(T);
            next = null;
        }

        //資料域屬性
        public T Data
        {
            get
            {
                return data;
            }
            set
            {
                data = value;
            }
        }

        //參考網域屬性
        public Node<T> Next
        {
            get { return next; }
            set { next = value; }
        }
    }

   
    //單鏈表類--定義操作結點的一些方法(如刪除, 插入等).
    //IList<T>是.net內建的一個介面. 這裡實現了這個介面.
    public class LinkList<T> : IList<T>
    {
        private Node<T> head;  //單鏈表的頭引用

        //頭引用 屬性
        public Node<T> Head
        {
            get { return head; }
            set { head = value; }
        }

        //構造器
        public LinkList()
        {
            head = null;
        }

        ///<summary>
        ///求單鏈表的長度
        ///需要從表頭開始, 一個結點一個結點遍曆,直到表的末尾.
        ///</summary>
      
        public int GetLength()
        {
            Node<T> p = head;

            int len = 0;
            while (p != null)
            {
                ++len;
                p = p.Next;
            }

            return len;
        }

        ///<summary>
        /// 清空單鏈表
        /// head=null即可.
        /// 單鏈表清空後,原來結點所佔用的空間不會一直保留, 而由記憶體回收行程進行回收.
        ///</summary>
        public void Clear()
        {
            head = null;
        }

        ///<summary>
        /// 判斷單鏈表是否為空白
        /// head==null,即為空白
        ///</summary>
        public bool IsEmpty()
        {
            if (head == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        ///<summary>
        ///附加操作
        ///在單鏈表的末尾添加新元素
        /// </summary>
        public void Append(T item)
        {
            Node<T> q = new Node<T>(item);
            Node<T> p = new Node<T>();

            if (head == null)
            {
                head = q;
                return;
            }

            p = head;
            while (p.Next != null)
            {
                p = p.Next;
            }

            p.Next = q;
        }

        //在單鏈表的第 i 個結點位置前插入一個值為 item 的結點.
        public void Insert(T item, int i)
        {
            if (IsEmpty() || i < 1)
            {
                Console.WriteLine("List is empty or Position is error!");
                return;
            }

            //就一個head元素.(插入到head前即可)
            if (i == 1)
            {
                Node<T> q = new Node<T>(item);
                q.Next = head;
                return;
            }

            //非head(中間某一元素前插入P)
            Node<T> p = head;
            Node<T> r = new Node<T>();
            int j = 1;

            while (p.Next != null && j < i)
            {
                r = p;
                p = p.Next;
                ++j;
            }

            if (j == i)
            {
                Node<T> q = new Node<T>(item);
                q.Next = p;
                r.Next = q;
            }

        }

        //在單鏈表的第 i 個結點的位置後插入一個值為 item的結點.
        public void InsertPost(T item, int i)
        {
            if (IsEmpty() || i < 1)
            {
                Console.WriteLine("List is empty or Position is error!");
                return;
            }

            if (i == 1)
            {
                Node<T> q = new Node<T>(item);
                q.Next = head.Next;
                head.Next = q;
                return;
            }

            Node<T> p = head;
            int j = 1;

            while (p != null && j < i)
            {
                p = p.Next;
                ++j;
            }

            if (j == i)
            {
                Node<T> q = new Node<T>(item);
                q.Next = p.Next;
                p.Next = q;
            }
        }

        //刪除單鏈表的第 i 個結點
        public T Delete(int i)
        {
            if (IsEmpty() || i < 0)
            {
                Console.WriteLine("Link is empty or Position is error!");
                return default(T);
            }

            Node<T> q = new Node<T>();
            if (i == 1)
            {
                q = head;
                head = head.Next;
                return q.Data;
            }

            Node<T> p = head;
            int j = 1;

            //從頭一直找到 i 所在的位置
            //條件是: (1).單鏈表沒有到末尾, (2).還沒有到 i 所在的位置 ( j< i).
            while (p.Next != null && j < i)
            {
                ++j;
                q = p;
                p = p.Next;
            }

            if (j == i)
            {
                q.Next = p.Next;
                return p.Data;
            }
            else
            {
                Console.WriteLine("The item node is not exist!");
                return default(T);
            }
        }

        //獲得單鏈表的第 i 個資料元素
        public T GetElem(int i)
        {
            if (IsEmpty())
            {
                Console.WriteLine("List is empty!");
                return default(T);
            }

            Node<T> p = new Node<T>();
            p = head;
            int j = 1;

            while (p.Next != null & j < i)
            {
                ++j;
                p = p.Next;
            }

            if (j == i)
            {
                return p.Data;  //找到了.
            }
            else
            {
                Console.WriteLine("The item node is not exist!");
                return default(T);
            }
        }

        ///<summary>
        ///在單鏈表中尋找值為 value 的結點
        ///</summary>
        public int Locate(T value)
        {
            if (IsEmpty())
            {
                Console.WriteLine("List is Empty!");
                return -1;
            }

            Node<T> p = new Node<T>();
            p = head;
            int i = 1;
            while (!p.Data.Equals(value) && p.Next != null)
            {
                p = p.Next;
                ++i;
            }

            return i;
        }

 

    }

   
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.