示例#1
0
 static void TestListAddition()
 {
     LinkedList list1 = new LinkedList(new List<Node>{
         new Node(3),
         new Node(0),
         new Node(5)
     });
     list1.ToString();
     LinkedList list2 = new LinkedList(new List<Node>{
         new Node(7),
         //new Node(0),
         //new Node(3)
     });
     list2.ToString();
     var test1 = new ListAddition(list1, list2);
     test1.Run();
 }
示例#2
0
        static void TestRemoveNodeFromLinkedList()
        {
            Node head = new Node (1);
            LinkedList list = new LinkedList (head);
            list.AppendNewNode (new Node (2));
            list.AppendNewNode (new Node (3));
            list.AppendNewNode (new Node (4));
            Node five = new Node(5);
            list.AppendNewNode (five);
            list.AppendNewNode (new Node (2));
            list.AppendNewNode (new Node (7));
            list.AppendNewNode (new Node (8));
            list.ToString ();
            //Node newHead = head.DeleteSelfFromLinkedList ();
            //while (newHead!=null) {
            //	Console.Write(newHead.Data);
            //	Console.Write(" ");
            //	newHead = newHead.Next;
            //}
            //Console.WriteLine(" ");

            five.DeleteSelfFromLinkedList();
            list.ToString();

            //Test single node list
            Node one = new Node(2);
            LinkedList list2 = new LinkedList(one);
            list2.Head = one.DeleteSelfFromLinkedList();
            list2.ToString();
        }
示例#3
0
        //Chapter 2
        static void TestLinkedList()
        {
            Node head= new Node(1);
            LinkedList list = new LinkedList(head);
            list.AppendNewNode(new Node(2));
            list.AppendNewNode(new Node(3));
            list.AppendNewNode(new Node(4));
            list.AppendNewNode(new Node(5));
            list.AppendNewNode(new Node(2));
            list.AppendNewNode(new Node(7));
            list.AppendNewNode(new Node(8));
            list.ToString();

            list.RemoveDuplicateNode();
            list.ToString();

            list.AppendNewNode(new Node(2));
            list.AppendNewNode(new Node(7));
            list.ToString();

            list.RemoveDuplicateNodeWithoutHashTable();
            list.ToString();

            //Find the Node which is Nth to the last node
            Node result = list.FindTheNthToLastNode(4);
            Console.WriteLine(result.Data);
            result = list.FindTheNthToLastNode(14);
            Console.WriteLine(result == null);

            result = list.FindTheNthToLastNode(0);
            Console.WriteLine(result.Data);

            result = list.FindTheNthToLastNode(1);
            Console.WriteLine(result.Data);

            Node s = null;
            LinkedList list2 = new LinkedList(s);
            result = list2.FindTheNthToLastNode(2);
            Console.WriteLine(result == null);
        }