示例#1
0
        public void DeleteItemByValue()
        {
            testListAsc.Add(12);
            testListAsc.Add(2);
            testListAsc.Add(0);
            testListAsc.Add(-6);
            testListAsc.Add(50);
            testListAsc.Add(-64);
            testListAsc.Add(-2);
            testListAsc.Add(0);
            testListAsc.Add(500);
            testListAsc.Add(500);
            testListAsc.Add(51);
            testListAsc.Add(-2);
            testListAsc.Add(5000);
            testListAsc.Add(5000);
            testListAsc.Add(-6422);

            testListDesc.Add(0);
            testListDesc.Add(12);
            testListDesc.Add(55);
            testListDesc.Add(-2113);
            testListDesc.Add(30);
            testListDesc.Add(0);
            testListDesc.Add(-1050);
            testListDesc.Add(50);
            testListDesc.Add(50);
            testListDesc.Add(50);
            testListDesc.Add(100);
            testListDesc.Add(230);
            testListDesc.Add(41);
            testListDesc.Add(-1234);
            testListDesc.Add(1200);

            Assert.AreEqual(15, testListAsc.Count());
            Assert.AreEqual(15, testListDesc.Count());

            testListAsc.Delete(13);
            testListDesc.Delete(13);

            Assert.AreEqual(15, testListAsc.Count());
            Assert.AreEqual(15, testListDesc.Count());

            testListAsc.Delete(5000);
            testListDesc.Delete(50);

            Assert.AreEqual(14, testListAsc.Count());
            Assert.AreEqual(14, testListDesc.Count());

            testListAsc.Delete(12);
            testListAsc.Delete(0);
            testListAsc.Delete(50);
            testListAsc.Delete(-2);
            testListAsc.Delete(500);
            testListAsc.Delete(51);
            testListAsc.Delete(5000);
            testListAsc.Delete(-6422);
            testListAsc.Delete(2);
            testListAsc.Delete(-6);
            testListAsc.Delete(-64);

            testListDesc.Delete(0);
            testListDesc.Delete(55);
            testListDesc.Delete(30);
            testListDesc.Delete(-1050);
            testListDesc.Delete(50);
            testListDesc.Delete(100);
            testListDesc.Delete(41);
            testListDesc.Delete(1200);
            testListDesc.Delete(12);
            testListDesc.Delete(-2113);
            testListDesc.Delete(0);

            Assert.AreEqual(3, testListAsc.Count());
            Assert.AreEqual(3, testListDesc.Count());

            testListAsc.Delete(-2);
            testListAsc.Delete(500);
            testListAsc.Delete(0);

            testListDesc.Delete(230);
            testListDesc.Delete(-1234);
            testListDesc.Delete(50);

            Assert.AreEqual(0, testListAsc.Count());
            Assert.AreEqual(0, testListDesc.Count());

            testListAsc.Delete(0);
            testListDesc.Delete(230);

            Assert.AreEqual(0, testListAsc.Count());
            Assert.AreEqual(0, testListDesc.Count());
        }
        static void TestAddSortedList(bool asc)
        {
            Console.WriteLine("test ordered list asc = " + asc);
            OrderedList <int> orderedList = new OrderedList <int>(asc);

            Console.WriteLine("test add ");
            Console.WriteLine("head before add = " + (orderedList.head == null ? "null" : orderedList.head.value.ToString()));
            Console.WriteLine("tail before add = " + (orderedList.tail == null ? "null" : orderedList.tail.value.ToString()));
            Console.WriteLine("size before add = " + orderedList.Count());

            for (int i = 1, val = 1; i <= 15; i++, val += 10)
            {
                orderedList.Add(val);
                Console.Write(val + " ");
            }

            Console.WriteLine();
            ShowListElements(orderedList);
            Console.WriteLine();
            ShowListElementsReverse(orderedList);
            Console.WriteLine("size after add = " + orderedList.Count());
            Console.WriteLine("head after add = " + (orderedList.head == null ? "null" : orderedList.head.value.ToString()));
            Console.WriteLine("tail after add = " + (orderedList.tail == null ? "null" : orderedList.tail.value.ToString()));


            Console.WriteLine();
            Console.WriteLine("test delete 91");
            Console.WriteLine("size before add = " + orderedList.Count());
            Console.WriteLine("head before add = " + (orderedList.head == null ? "null" : orderedList.head.value.ToString()));
            Console.WriteLine("tail before add = " + (orderedList.tail == null ? "null" : orderedList.tail.value.ToString()));
            orderedList.Delete(91);
            ShowListElements(orderedList);
            Console.WriteLine();
            ShowListElementsReverse(orderedList);
            Console.WriteLine();
            Console.WriteLine("size after add = " + orderedList.Count());
            Console.WriteLine("head after add = " + (orderedList.head == null ? "null" : orderedList.head.value.ToString()));
            Console.WriteLine("tail after add = " + (orderedList.tail == null ? "null" : orderedList.tail.value.ToString()));

            Console.WriteLine();
            Console.WriteLine("test delete 1");
            Console.WriteLine("size before add = " + orderedList.Count());
            Console.WriteLine("head before add = " + (orderedList.head == null ? "null" : orderedList.head.value.ToString()));
            Console.WriteLine("tail before add = " + (orderedList.tail == null ? "null" : orderedList.tail.value.ToString()));
            orderedList.Delete(1);
            ShowListElements(orderedList);
            Console.WriteLine();
            ShowListElementsReverse(orderedList);
            Console.WriteLine();
            Console.WriteLine("size after add = " + orderedList.Count());
            Console.WriteLine("head after add = " + (orderedList.head == null ? "null" : orderedList.head.value.ToString()));
            Console.WriteLine("tail after add = " + (orderedList.tail == null ? "null" : orderedList.tail.value.ToString()));

            Console.WriteLine();
            Console.WriteLine("test delete 141");
            Console.WriteLine("size before add = " + orderedList.Count());
            Console.WriteLine("head before add = " + (orderedList.head == null ? "null" : orderedList.head.value.ToString()));
            Console.WriteLine("tail before add = " + (orderedList.tail == null ? "null" : orderedList.tail.value.ToString()));
            orderedList.Delete(141);
            ShowListElements(orderedList);
            Console.WriteLine();
            ShowListElementsReverse(orderedList);
            Console.WriteLine();
            Console.WriteLine("size after add = " + orderedList.Count());
            Console.WriteLine("head after add = " + (orderedList.head == null ? "null" : orderedList.head.value.ToString()));
            Console.WriteLine("tail after add = " + (orderedList.tail == null ? "null" : orderedList.tail.value.ToString()));

            Console.WriteLine();
            Console.WriteLine("test find 71");
            var result = orderedList.Find(71);

            Console.WriteLine(result.value);

            Console.WriteLine();
            Console.WriteLine("test find 131");
            result = orderedList.Find(131);
            Console.WriteLine(result.value);

            Console.WriteLine();
            Console.WriteLine("test find 11");
            result = orderedList.Find(11);
            Console.WriteLine(result.value);

            Console.WriteLine();
            Console.WriteLine("test find 1000");
            result = orderedList.Find(1000);
            Console.WriteLine(result == null ? "null" : result.value.ToString());

            Console.WriteLine();
            Console.WriteLine("test clear");
            orderedList.Clear(asc);
            Console.WriteLine("size after add = " + orderedList.Count());
            Console.WriteLine("head after add = " + (orderedList.head == null ? "null" : orderedList.head.value.ToString()));
            Console.WriteLine("tail after add = " + (orderedList.tail == null ? "null" : orderedList.tail.value.ToString()));

            Console.WriteLine(new string('=', 50));
        }