加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# 实现单链表

发布时间:2020-12-15 22:39:19 所属栏目:百科 来源:网络整理
导读:单链表类 using System; using System.Collections.Generic; using System.Reflection.Metadata; using System.Text; namespace 单链表{ public class NodeT { public T Data { get ; set ; } public NodeT Next { get ; set ; } public int Last { get ; se

单链表类

using System;
using System.Collections.Generic;
using System.Reflection.Metadata;
using System.Text;

namespace 单链表
{
    public class Node<T>
    {
        public T Data { get; set; }
        public Node<T> Next { get; set; } 
        public int Last { get; set; } //最后节点的位置

        /// <summary>
        /// 1.构造函数进行初始化
        /// </summary>
        /// <param name="L"></param>
        public Node()
        {
            this.Data = default(T);
            this.Next = null;
            this.Last = 0;
        }

        /// <summary>
        /// 2.判断是否为空
        /// </summary>
        /// <param name="l"></param>
        /// <returns></returns>
        public bool IsEmpty()
        {
            if (this.Last==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 3.求长度
        /// </summary>
        /// <param name="l"></param>
        /// <returns></returns>
        public int Length()
        {
            return this.Last;
        }

        /// <summary>
        /// 4.插入元素
        /// </summary>
        /// <param name="l"></param>
        /// <param name="i"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public void InsertNode(int i,T item)
        {
            if (i <= 0)
            {
                Console.WriteLine("插入的位置错误");
                return ;
            }
            Node<T> nBefore = new Node<T>();
            Node<T> nCurrent = this;
            Node<T> s = new Node<T>();
            int j = 0;
            while (nCurrent != null && j < i)
            {
                j++;
                nBefore = nCurrent;
                nCurrent = nCurrent.Next;
            }

            if (nCurrent == null)
            {
                s.Data = item;
                s.Next = null;
                nBefore.Next = s;
            }
            else
            {
                s.Data = item;
                s.Next = nCurrent;
                nBefore.Next = s;
            }

            this.Last++;
        }

        /// <summary>
        /// 5.显示元素
        /// </summary>
        /// <param name="l"></param>
        public void DisplayNode()
        {
            Node<T> n = new Node<T>();
            n = this;
            while (n != null)
            {
                if (n.Data!=null)
                {
                    Console.WriteLine(n.Data.ToString());
                }
                n = n.Next;
            }
        }

        /// <summary>
        /// 6.删除i位置的元素
        /// </summary>
        /// <param name="l"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public void DeleteNode( int i)
        {
            if (i < 1 || Length()<i)
            {
                Console.WriteLine("删除的位置错误,删除失败");
                return;
            }
            Node<T> p = this;
            int j = 0;
            Node<T> x = new Node<T>();
            Node<T> q = new Node<T>();
            while (j < i && p != null)
            {
                j++;//1,2,6
                x = p;//0,1,5
                p = p.Next;//1,6
                q = p.Next;//2,3,7
            }

            if (q == null)
            {
                x.Next = q;
                p = null;
            }
            else
            {
                x.Next = q;
                p = null;
            }

            this.Last--;
        }

        /// <summary>
        /// 7.获得某个元素e,所在的位置
        /// </summary>
        /// <param name="l"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        public int IndexOf(T e)
        {
            int a = 0;
            Node<T> x = this;
            bool exist = false;
            while (x != null )
            {
               
                if (x.Data != null)
                {
                    if (x.Data.Equals(e))
                    {
                        exist = true;
                        break;
                    }
                    
                }
                x = x.Next;
                a++;
            }

            if (exist ==false)
            {
                return -1;
            }
            return a;
        }

        /// <summary>
        /// 8.获得某个位置的元素
        /// </summary>
        /// <param name=""></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public T GetElement(int i)
        {
            Node<T> x = this;
            T p = default(T);
            int a = 0;
            while (x!=null && a<i)
            {
                a++;
                x = x.Next;
            }

            if (x ==null)
            {
                return p;
            }
            else
            {
                return x.Data;
            }
        }

        /// <summary>
        ///9.元素反转
        /// </summary>
        /// <returns></returns>
        public Node<T> Reverse()
        {
            List<T> reverseList = new List<T>();
            Node<T> s = new Node<T>();
            s.Last = this.Last;
            Node<T> x = this;
            for (int i = 0; i < this.Last; i++)
            {
                x = x.Next;
                reverseList.Add(x.Data);
            }
            for (int i = 0; i < reverseList.Count; i++)
            {
                s.InsertNode(1,reverseList[i]);
            }
            return s;
        }

    }
}
View Code

测试

using System;

namespace 单链表
{
    class Program
    {
        static void Main(string[] args)
        {
            Node<string> N=new Node<string>();
            N.InsertNode( 1,"aa");
            N.InsertNode( 1,"bb"); 
            N.InsertNode( 1,"cc"); 
            N.InsertNode( 1,"dd");
            N.DisplayNode();
            Console.WriteLine("当前链表的长度为:{0}",N.Length());
            Console.WriteLine("判断当前是否为空:{0}",N.IsEmpty().ToString());
            Console.WriteLine("第{0}个元素是:{1}",3,N.GetElement(3));
            Console.WriteLine("元素{0}所在的位置是:{1}","dd",N.IndexOf("dd"));
            Console.WriteLine("--------元素反转--------");
            Node<string> x = N.Reverse();
            x.DisplayNode();
            Console.WriteLine("--------删除第2个节点--------");
            x.DeleteNode(2);
            x.DisplayNode();
            Console.ReadKey();
        }
    }
}
View Code

结果:

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读