单链表类
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
结果:
总结
以上是内存溢出为你收集整理的C# 实现单链表全部内容,希望文章能够帮你解决C# 实现单链表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)