C# 線性表之單鏈表

來源:互聯網
上載者:User
資料類:
namespace 單鏈表{    /// <summary>    /// 結點    /// </summary>    /// <typeparam name="T"></typeparam>    public class Node<T>    {        private T data; //資料域        private Node<T> next; //參考網域(指標域)        public Node(T val, Node<T> p)        {            data = val;            next = p;        }        public Node(Node<T> p)        {            next = p;        }        public Node(T val)        {            data = val;        }        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; }        }     }}
單鏈表具體實現代碼:
using System;using 線性表;namespace 單鏈表{    public class LinkList<T>:IListDS<T>    {        private Node<T> head;        public Node<T> Head        {            get { return head; }            set { head = value; }        }        public LinkList()        {            head = null;        }         public int GetLength()        {            Node<T> p = head;            int _len = 0;            while (p!=null)            {                _len++;                p = p.Next;            }            return _len;        }        public void Clear()        {            head = null;        }        public bool IsEmpty()        {            bool _isEmpty = head == null ? true : false;            return _isEmpty;        }        public bool IsFull()        {            return false;        }        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;        }        /// <summary>        /// 在單鏈表前插入        /// </summary>        /// <param name="item"></param>        /// <param name="index"></param>        public void Insert(T item, int index)        {            if (IsEmpty() || index < 0 || index >= GetLength())            {                Console.WriteLine("List is empty or Position is error!");                return;            }            Node<T> p = head;            Node<T> r = new Node<T>();            int j = 0;            if (index == 0)            {                Node<T> q = new Node<T>(item);                q.Next = p;                head = q;                return;            }            while (p.Next != null && j < index)            {                r = p;                p = p.Next;                j++;            }            if (j == index)            {                Node<T> q = new Node<T>(item);                q.Next = p;                r.Next = q;            }        }        /// <summary>        /// 後插入        /// </summary>        /// <param name="item"></param>        /// <param name="index"></param>        public void InsertPost(T item, int index)        {            if (IsEmpty() || index < 0 || index >= GetLength())            {                Console.WriteLine("List is empty or Position is error!");                return;            }            Node<T> p = head;            int j = 0;            while (p.Next != null && j < index)            {                p = p.Next;                j++;            }            if (j == index)            {                Node<T> q = new Node<T>(item);                q.Next = p.Next;                p.Next = q;            }        }        public T Delete(int index)        {            if (IsEmpty() || index < 0 || index >= GetLength())            {                Console.WriteLine("List is empty or Position is error!");                return default(T);            }            Node<T> q = new Node<T>();            Node<T> p = head;            int j = 0;            if (index == 0)            {                q = head;                head = head.Next;                return q.Data;            }            while (p.Next != null && j < index)            {                q = p;                p = p.Next;                j++;            }            if (j == index)            {                q.Next = p.Next;                return p.Data;            }            return default(T);        }        public T GetElem(int index)        {            Node<T> p = new Node<T>();            p = head;            int j = 0;            while (p.Next != null && j < index)            {                p = p.Next;                j++;            }            if (j == index)                return p.Data;            return default(T);        }        public int Locate(T value)        {            Node<T> p = new Node<T>();            p = head;            int j = 0;            if (p.Data.Equals(value)) //要尋找的值在索引0,                return j;            while (p.Next != null)            {                p = p.Next;                j++;                if (p.Data.Equals(value))                    return j;            }            Console.WriteLine("Position is error!");            return j;        }        public void Reverse()        {            Node<T> p = head;            Node<T> q = new Node<T>();            head = null;            while (p != null)            {                q = p;                p = p.Next;                q.Next = head;                head = q;                Console.WriteLine(head.Data);            }        }    }}
測試代碼:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 單鏈表{    class LinkListOperator    {        static void Main(string[] args)        {            LinkList<int> linkLists = new LinkList<int>();            linkLists.Append(1);            linkLists.Append(2);            linkLists.Append(3);            linkLists.Append(4);            for (int i = 0; i < linkLists.GetLength(); i++)            {                Console.WriteLine(linkLists.GetElem(i));            }            Console.WriteLine("===== Insert =====");            linkLists.Insert(888, 3);            for (int i = 0; i < linkLists.GetLength(); i++)            {                Console.WriteLine(linkLists.GetElem(i));            }            Console.WriteLine("===== InsertPost =====");            linkLists.InsertPost(777, linkLists.GetLength() - 1);            for (int i = 0; i < linkLists.GetLength(); i++)            {                Console.WriteLine(linkLists.GetElem(i));            }            Console.WriteLine("===== Delete =====");            linkLists.Delete(0);            for (int i = 0; i < linkLists.GetLength(); i++)            {                Console.WriteLine(linkLists.GetElem(i));            }            Console.WriteLine("===== Locate =====");            Console.WriteLine(linkLists.Locate(888));            //Console.WriteLine("===== CreateListHead 輸入要添加的值 =====");            //LinkList<int> L = CreateListHead();            //for (int i = 0; i < L.GetLength(); i++)            //{            //    Console.WriteLine(L.GetElem(i));            //}            //Console.WriteLine("===== CreateListTail 輸入要添加的值 =====");            //LinkList<int> L1 = CreateListTail();            //for (int i = 0; i < L1.GetLength(); i++)            //{            //    Console.WriteLine(L1.GetElem(i));            //}            //Console.WriteLine("===== Reverse  =====");            //L1.Reverse();            //for (int i = 0; i < L1.GetLength(); i++)            //{            //    Console.WriteLine(L1.GetElem(i));            //}            //Console.WriteLine("===== Merge  =====");            //LinkList<int>ha=new LinkList<int>();            //LinkList<int>hb=new LinkList<int>();            //ha.Append(1);            //ha.Append(3);            //ha.Append(5);            //ha.Append(7);            //hb.Append(2);            //hb.Append(4);            //hb.Append(8);            //hb.Append(9);            //LinkList<int> hc = Merge(ha, hb);            //for (int i = 0; i < hc.GetLength(); i++)            //{            //    Console.WriteLine(hc.GetElem(i));            //}            Console.WriteLine("===== Purge  =====");            LinkList<int> ha = new LinkList<int>();            ha.Append(1);            ha.Append(3);            ha.Append(3);            ha.Append(5);            ha.Append(5);            ha.Append(7);            ha.Append(7);            ha.Append(89);            LinkList<int> hb = Purge(ha);            for (int i = 0; i < hb.GetLength(); i++)            {                Console.WriteLine(hb.GetElem(i));            }            Console.ReadKey();        }        /// <summary>        /// 在頭部插入結點建立單鏈表的演算法        /// </summary>        /// <returns></returns>        static LinkList<int> CreateListHead()        {            int d;            LinkList<int> L = new LinkList<int>();            d = Int32.Parse(Console.ReadLine());            while (d != -1)            {                Node<int> q = new Node<int>(d);                q.Next = L.Head;                L.Head = q;                d = Int32.Parse(Console.ReadLine());            }            return L;        }        /// <summary>        /// 在尾部插入結點建立單鏈表的演算法        /// </summary>        /// <returns></returns>        static LinkList<int> CreateListTail()        {            int d;            LinkList<int> L = new LinkList<int>();            d = Int32.Parse(Console.ReadLine());            Node<int> R = new Node<int>();            R = L.Head;            while (d != -1)            {                Node<int> q = new Node<int>(d);                if (L.Head == null)                    L.Head = q;                else                    R.Next = q;                R = q;                d = Int32.Parse(Console.ReadLine());            }            if (R != null)                R.Next = null;            return L;        }        /// <summary>        /// 合并已經排序的單鏈表,並且再進行排序        /// </summary>        /// <param name="Ha"></param>        /// <param name="Hb"></param>        /// <returns></returns>        static LinkList<int> Merge(LinkList<int> Ha, LinkList<int> Hb)        {            LinkList<int> Hc = new LinkList<int>();            Node<int> p = Ha.Head.Next;            Node<int> q = Hb.Head;            Node<int> s = new Node<int>();            Hc = Ha;            Hc.Head.Next = null; //Hc為1            while (p != null && q != null)            {                if (p.Data < q.Data)                {                    s = p;                    p = p.Next;                }                else                {                    s = q;                    q = q.Next;                }                Hc.Append(s.Data);            }            if (p == null)            {                p = q;            } while (p != null)            {                s = p;                p = p.Next;                Hc.Append(s.Data);            }            return Hc;        }        static LinkList<int> Merge1(LinkList<int> Ha, LinkList<int> Hb)        {            LinkList<int> Hc = new LinkList<int>();            Node<int> p = Ha.Head.Next;            Node<int> q = Hb.Head;            Node<int> s = new Node<int>();            Hc = Ha;            Hc.Head.Next = null; //Hc為1            while (p != null && q != null)            {                if (p.Data < q.Data)                {                    s = p;                    p = p.Next;                }                else                {                    s = q;                    q = q.Next;                }                s.Next = Hc.Head.Next;                Hc.Head.Next = s;            }            if (p == null)            {                p = q;            } while (p != null)            {                s = p;                p = p.Next;                s.Next = Hc.Head.Next;                Hc.Head.Next = s;            }            return Hc;        }        static LinkList<int> Purge(LinkList<int> ha)        {            for (int i = 0; i < ha.GetLength(); i++)            {                Console.WriteLine(ha.GetElem(i));            }            LinkList<int> Hb = new LinkList<int>();            Node<int> p = ha.Head;            Node<int> q = new Node<int>();            Node<int> s = new Node<int>();            s = p;            p = p.Next;            s.Next = null;            Hb.Head = s;            Console.WriteLine(ha.Head.Next);            Hb.Head.Next = ha.Head.Next;            while (p.Next != null)            {                s = p;                p = p.Next;                q = Hb.Head.Next;                while (q.Next != null && q.Data != s.Data)                {                    q = q.Next;                }                if (q == null)                {                    s.Next = Hb.Head.Next;                    Hb.Head.Next = s;                }            }            return Hb;        }    }}

聯繫我們

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