C# 实现单链表

Posted xifengmo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# 实现单链表相关的知识,希望对你有一定的参考价值。

技术图片
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)
            
                Console.WriteLine(n.Data);
                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,2,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;
            while (x != null && x.Data.Equals(e)==false)
            
                x = x.Next;
                a++;
            

            if (x ==null)
            
                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

 

以上是关于C# 实现单链表的主要内容,如果未能解决你的问题,请参考以下文章

数据结构学习笔记(单链表单循环链表带头双向循环链表)的增删查改排序等)

单链表

[Data Structure]线性表Linear List2

C#单链表

单向链表(单链表)的Java实现

数据结构与算法学习笔记 线性表Ⅱ