void bll(Code3_6_LL <int> ll) { //遍历链表 Code3_6_Node <int> p = ll.Head; int i = 1; while (p != null) { System.Console.WriteLine("链表第{0}个结点为:{1}", i, p.Data); p = p.Next; i++; } }
/// <summary> /// ……这里解决了一个问题,未能找到类型或命名空间T,是否缺少using指令或程序集引用……在定义的方法名cLH后面也加上<T>即可 /// 因为最开始没有加所以报错cLH(Code3_6_LL<T> ll, int n)是不对的,应该为cLH><T>(Code3_6_LL<T> ll, int n) /// create list by head 头插法创建一个链表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="ll"></param> /// <param name="n"></param> void cLH(Code3_6_LL <int> ll, int n) { Random rd = new Random(); ll.Head = new Code3_6_Node <int>(rd.Next(0, 100)); int i; for (i = 0; i < n - 1; i++) { //Code3_6_Node<int> p = new Code3_6_Node<int>(rd.Next(0,100)); //p.Next = ll.Head.Next; //ll.Head.Next = p; ll.insert(rd.Next(0, 100), 1); } }
/// <summary> /// create List by Tail,尾插法创建一个链表 /// </summary> /// <param name="ll"></param> /// <param name="n"></param> void cLT(Code3_6_LL <int> ll, int n) { Random rd = new Random(); ll.Head = new Code3_6_Node <int>(rd.Next(0, 100)); Code3_6_Node <int> p = ll.Head; int i; for (i = 0; i < n - 1; i++) { Code3_6_Node <int> q = new Code3_6_Node <int>(rd.Next(0, 100)); q.Next = p.Next; p.Next = q; p = q; } }