Exemplo n.º 1
0
        public static void TestSingleList()
        {
            SingleList <int> linkList = new SingleList <int>();

            linkList.InsertFirst(new SingleLinkNode <int>(1));
            linkList.InsertFirst(new SingleLinkNode <int>(2));

            linkList.print();

            SingleList <int> linkList2 = new SingleList <int>();

            linkList2.InsertEnd(new SingleLinkNode <int>(1));
            linkList2.InsertEnd(new SingleLinkNode <int>(2));

            linkList2.print();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 测试单链表中寻找第K个节点
        /// </summary>
        public static void TestFindKthElement()
        {
            SingleList <int> linklist = new SingleList <int>();

            linklist.InsertEnd(new SingleLinkNode <int>(1));
            linklist.InsertEnd(new SingleLinkNode <int>(2));
            linklist.InsertEnd(new SingleLinkNode <int>(3));
            linklist.InsertEnd(new SingleLinkNode <int>(4));
            linklist.InsertEnd(new SingleLinkNode <int>(5));
            linklist.InsertEnd(new SingleLinkNode <int>(6));
            linklist.InsertEnd(new SingleLinkNode <int>(7));
            linklist.InsertEnd(new SingleLinkNode <int>(8));

            SingleLinkNode <int> kthElement = linklist.FindKth(5);

            if (kthElement != null)
            {
                Console.WriteLine(string.Format("第K个节点的Data值是:{0}", kthElement.Data));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 测试寻找单链表中倒数第K个节点
        /// </summary>
        public static void TestFindLastKthElement()
        {
            SingleList <int> linklist = new SingleList <int>();

            linklist.InsertEnd(new SingleLinkNode <int>(1));
            linklist.InsertEnd(new SingleLinkNode <int>(2));
            linklist.InsertEnd(new SingleLinkNode <int>(3));
            linklist.InsertEnd(new SingleLinkNode <int>(4));
            linklist.InsertEnd(new SingleLinkNode <int>(5));
            linklist.InsertEnd(new SingleLinkNode <int>(6));
            linklist.InsertEnd(new SingleLinkNode <int>(7));
            linklist.InsertEnd(new SingleLinkNode <int>(8));

            SingleLinkNode <int> kthElement = linklist.FindLastKth(1);

            if (kthElement != null)
            {
                Console.WriteLine(string.Format("倒数第1个节点的Data值是:{0}", kthElement.Data));
            }

            kthElement = linklist.FindLastKth(3);

            if (kthElement != null)
            {
                Console.WriteLine(string.Format("倒数第3个节点的Data值是:{0}", kthElement.Data));
            }

            kthElement = linklist.FindLastKth(10);

            if (kthElement != null)
            {
                Console.WriteLine(string.Format("倒数第10个节点的Data值是:{0}", kthElement.Data));
            }
            else
            {
                Console.WriteLine("没有找到倒数第10个节点");
            }
        }