Esempio n. 1
0
    static void Main(String[] args)
    {
        DLinkedList <String> L2 = new DLinkedList <String>();

        L2.Append("A");
        L2.Append("B");
        L2.Append("C");
        L2.Append("D");
        L2.Append("E");
        L2.Append("F");
        L2.Append("G");
        L2.Append("H");

        DLinkedList <String> L1 = new DLinkedList <String>();

        L1.Append("I");
        L1.Append("J");
        L1.Append("K");
        L1.Append("L");
        L1.Append("M");

        Console.WriteLine("Listas originales");
        Console.WriteLine("L1: " + L1);
        Console.WriteLine("L2: " + L2);
        Console.WriteLine();

        L1.Prepend(L2);
        Console.WriteLine("Despues de L1.prepend(L2)");
        Console.WriteLine("L1: " + L1);
        Console.WriteLine("L2: " + L2);
        Console.WriteLine();

        DLinkedList <String> L3 = L1.subList(3, 7);

        Console.WriteLine("Despues de L1.subList(3, 7)");
        Console.WriteLine("L1: " + L1);
        Console.WriteLine("L3: " + L3);
        Console.WriteLine();

        /*
         * Salida esperada:
         * Listas originales
         * L1: size = 5  elementos = ( I J K L M )
         * L2: size = 8  elementos = ( A B C D E F G H )
         *
         * Despues de L1.prepend(L2)
         * L1: size = 13  elementos = ( A B C D E F G H I J K L M )
         * L2: size = 0  elementos = (  )
         *
         * Despues de L1.subList(3, 7)
         * L1: size = 8  elementos = ( A B C I J K L M )
         * L3: size = 5  elementos = ( D E F G H )
         */
    }
Esempio n. 2
0
        public void GetAtTest()
        {
            DLinkedList <int> list = new DLinkedList <int>();

            list.Append(1);
            list.Prepend(8);
            list.Append(4);
            list.Prepend(9);

            Assert.Equal(1, list.GetAt(2));
        }
        public static void TestSelectionSort()
        {
            var listOfNumbers = new DLinkedList <int>();

            listOfNumbers.Append(23);
            listOfNumbers.Append(42);
            listOfNumbers.Append(4);
            listOfNumbers.Append(16);
            listOfNumbers.Append(8);
            listOfNumbers.Append(15);
            listOfNumbers.Append(9);
            listOfNumbers.Append(55);
            listOfNumbers.Append(0);

            listOfNumbers.SelectionSort();
            var intArray = listOfNumbers.ToArray();
            var i        = 0;

            while (i < intArray.Length - 1)
            {
                Assert.True(intArray[i] <= intArray[i + 1], "Wrong sorting at index: " + i);
                ++i;
            }
        }
Esempio n. 4
0
        /// <summary>
        ///     Returns the neighbours doubly-linked list for the specified vertex.
        /// </summary>
        /// <param name="vertex">Vertex.</param>
        public DLinkedList <T> Neighbours(T vertex)
        {
            var returnList = new DLinkedList <T>();

            foreach (var c in _cliques)
            {
                if (c.Contains(vertex))
                {
                    foreach (var item in c)
                    {
                        if (!returnList.Contains(item))
                        {
                            returnList.Append(item);
                        }
                    }
                }
            }

            return(returnList);
        }
Esempio n. 5
0
        public void RemoveAtTest()
        {
            DLinkedList <string> list = new DLinkedList <string>();

            list.Append("a");
            list.Append("b");
            list.Append("c");
            list.Append("d");
            list.Append("e");
            list.Append("f");

            // Remove 1st
            list.RemoveAt(0);
            Assert.True(list[0] == "b", "Wrong element.");

            //Remove new 3rd
            list.RemoveAt(3);
            Assert.True(list[3] == "f", "Wrong element.");
        }
        public static void DoTest()
        {
            int index = 0;
            DLinkedList <string> listOfStrings = new DLinkedList <string>();

            listOfStrings.Append("zero");
            listOfStrings.Append("fst");
            listOfStrings.Append("sec");
            listOfStrings.Append("trd");
            listOfStrings.Append("for");
            listOfStrings.Append("fft");
            listOfStrings.Append("sxt");
            listOfStrings.Append("svn");
            listOfStrings.Append("egt");

            // Print
            Console.WriteLine(listOfStrings.ToReadable());

            // Remove 1st
            listOfStrings.RemoveAt(0);
            Console.WriteLine("Remove At 0:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[0] == "fst", "Wrong first element.");

            // Remove 4th
            listOfStrings.RemoveAt(4);
            Console.WriteLine("Remove At 4:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[4] == "sxt", "Wrong 4th element.");

            // Remove 5th and 6th
            // Note that after removing 5th, the old element at index 6 becomes at index 5.
            listOfStrings.RemoveAt(5);
            listOfStrings.RemoveAt(5);
            Console.WriteLine("Remove At 5 & 6:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[4] == "sxt", "Wrong element at index 5.");
            Debug.Assert(listOfStrings.Count < 6, "Wrong element at index 6. There must be no element at index 5.");

            // Remove 3rd
            listOfStrings.RemoveAt(listOfStrings.Count - 1);
            Console.WriteLine("Removed last:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[3] == "for", "Wrong element at index 3.");

            // Remove 1st
            listOfStrings.RemoveAt(0);
            Console.WriteLine("Remove 0th:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[0] == "sec", "Wrong element at index 0.");

            listOfStrings.Prepend("semsem3");
            listOfStrings.Prepend("semsem2");
            listOfStrings.Prepend("semsem1");
            Console.WriteLine("Prepend 3 items:\r\n" + listOfStrings.ToReadable());
            Console.WriteLine("Count: " + listOfStrings.Count);

            listOfStrings.InsertAt("InsertedAtLast1", listOfStrings.Count);
            listOfStrings.InsertAt("InsertedAtLast2", listOfStrings.Count);
            listOfStrings.InsertAt("InsertedAtMiddle", (listOfStrings.Count / 2));
            listOfStrings.InsertAt("InsertedAt 4", 4);
            listOfStrings.InsertAt("InsertedAt 9", 9);
            listOfStrings.InsertAfter("InsertedAfter 11", 11);
            Console.WriteLine("Inserts 3 items At:\r\n" + listOfStrings.ToReadable());

            // Test the remove item method
            listOfStrings.Remove("trd");
            Console.WriteLine("Removed item 'trd':\r\n" + listOfStrings.ToReadable());

            listOfStrings.Remove("InsertedAt 9");
            Console.WriteLine("Removed item 'InsertedAt 9':\r\n" + listOfStrings.ToReadable());

            // Print count
            Console.WriteLine("Count: " + listOfStrings.Count);

            Console.WriteLine();

            index = 0;
            Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

            index = (listOfStrings.Count / 2) + 1;
            Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

            index = (listOfStrings.Count / 2) + 2;
            Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

            index = (listOfStrings.Count - 1);
            Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

            Console.WriteLine();

            var firstRange = listOfStrings.GetRange(4, 6);

            Console.WriteLine("GetRange(4, 6):\r\n" + firstRange.ToReadable());

            var secondRange = firstRange.GetRange(4, 10);

            Console.WriteLine("From Previous GetRange(4, 10):\r\n" + secondRange.ToReadable());

            var thirdRange = (new DLinkedList <string>()).GetRange(0, 10);

            Console.WriteLine("Empty List: GetRange(0, 10):\r\n" + thirdRange.ToReadable());

            var arrayVersion = listOfStrings.ToArray();

            Debug.Assert(arrayVersion.Length == listOfStrings.Count);



            /****************************************************************************************/

            var stringsIterators = listOfStrings.GetEnumerator();

            Debug.Assert(stringsIterators.Current == listOfStrings[0], "Wrong enumeration.");

            if (stringsIterators.MoveNext() == true)
            {
                Debug.Assert(stringsIterators.Current == listOfStrings[1], "Wrong enumeration.");
            }

            stringsIterators.Dispose();

            Debug.Assert(listOfStrings != null && listOfStrings.Count > 0, "Enumartor has side effects!");

            /****************************************************************************************/

            var listOfNumbers = new DLinkedList <int>();

            listOfNumbers.Append(23);
            listOfNumbers.Append(42);
            listOfNumbers.Append(4);
            listOfNumbers.Append(16);
            listOfNumbers.Append(8);
            listOfNumbers.Append(15);
            listOfNumbers.Append(9);
            listOfNumbers.Append(55);
            listOfNumbers.Append(0);
            listOfNumbers.Append(34);
            listOfNumbers.Append(12);
            listOfNumbers.Append(2);

            listOfNumbers.SelectionSort();

            var intArray = listOfNumbers.ToArray();

            Debug.Assert(intArray[0] == 0 && intArray[intArray.Length - 1] == 55, "Wrong sorting!");
        }
		public static void DoTest ()
		{
			int index = 0;
			DLinkedList<string> listOfStrings = new DLinkedList<string>();

            listOfStrings.Append("zero");
			listOfStrings.Append("fst");
			listOfStrings.Append("sec");
			listOfStrings.Append("trd");
			listOfStrings.Append("for");
			listOfStrings.Append("fft");
			listOfStrings.Append("sxt");
			listOfStrings.Append("svn");
			listOfStrings.Append("egt");

			// Print
			Console.WriteLine(listOfStrings.ToReadable());

			// Remove 1st
			listOfStrings.RemoveAt(0);
			Console.WriteLine("Remove At 0:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[0] == "fst", "Wrong first element.");

            // Remove 4th
            listOfStrings.RemoveAt(4);
            Console.WriteLine("Remove At 4:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[4] == "sxt", "Wrong 4th element.");

			// Remove 5th and 6th
            // Note that after removing 5th, the old element at index 6 becomes at index 5.
			listOfStrings.RemoveAt(5);
			listOfStrings.RemoveAt(5);
			Console.WriteLine("Remove At 5 & 6:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[4] == "sxt", "Wrong element at index 5.");
            Debug.Assert(listOfStrings.Count < 6, "Wrong element at index 6. There must be no element at index 5.");

			// Remove 3rd
            listOfStrings.RemoveAt(listOfStrings.Count - 1);
			Console.WriteLine("Removed last:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[3] == "for", "Wrong element at index 3.");

			// Remove 1st
			listOfStrings.RemoveAt(0);
			Console.WriteLine("Remove 0th:\r\n" + listOfStrings.ToReadable());
            Debug.Assert(listOfStrings[0] == "sec", "Wrong element at index 0.");

			listOfStrings.Prepend("semsem3");
			listOfStrings.Prepend("semsem2");
			listOfStrings.Prepend("semsem1");
			Console.WriteLine("Prepend 3 items:\r\n" + listOfStrings.ToReadable());
			Console.WriteLine("Count: " + listOfStrings.Count);

			listOfStrings.InsertAt("InsertedAtLast1", listOfStrings.Count);
			listOfStrings.InsertAt("InsertedAtLast2", listOfStrings.Count);
			listOfStrings.InsertAt("InsertedAtMiddle", (listOfStrings.Count / 2));
			listOfStrings.InsertAt("InsertedAt 4", 4);
			listOfStrings.InsertAt("InsertedAt 9", 9);
			listOfStrings.InsertAfter("InsertedAfter 11", 11);
			Console.WriteLine("Inserts 3 items At:\r\n" + listOfStrings.ToReadable());

			// Test the remove item method
			listOfStrings.Remove ("trd");
			Console.WriteLine("Removed item 'trd':\r\n" + listOfStrings.ToReadable());

			listOfStrings.Remove ("InsertedAt 9");
			Console.WriteLine("Removed item 'InsertedAt 9':\r\n" + listOfStrings.ToReadable());

			// Print count
			Console.WriteLine("Count: " + listOfStrings.Count);

			Console.WriteLine();

			index = 0;
			Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

			index = (listOfStrings.Count / 2) + 1;
			Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

			index = (listOfStrings.Count / 2) + 2;
			Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

			index = (listOfStrings.Count - 1);
			Console.WriteLine("Get At " + index + ": " + listOfStrings[index]);

			Console.WriteLine();

			var firstRange = listOfStrings.GetRange(4, 6);
			Console.WriteLine("GetRange(4, 6):\r\n" + firstRange.ToReadable());

			var secondRange = firstRange.GetRange(4, 10);
			Console.WriteLine("From Previous GetRange(4, 10):\r\n" + secondRange.ToReadable());

			var thirdRange = (new DLinkedList<string>()).GetRange(0, 10);
			Console.WriteLine("Empty List: GetRange(0, 10):\r\n" + thirdRange.ToReadable());

			var arrayVersion = listOfStrings.ToArray();
			Debug.Assert (arrayVersion.Length == listOfStrings.Count);



            /****************************************************************************************/

            var stringsIterators = listOfStrings.GetEnumerator();

            Debug.Assert(stringsIterators.Current == listOfStrings[0], "Wrong enumeration.");

            if (stringsIterators.MoveNext() == true)
            {
                Debug.Assert(stringsIterators.Current == listOfStrings[1], "Wrong enumeration.");
            }

            stringsIterators.Dispose();

            Debug.Assert(listOfStrings != null && listOfStrings.Count > 0, "Enumartor has side effects!");

            /****************************************************************************************/

            var listOfNumbers = new DLinkedList<int>();

            listOfNumbers.Append(23);
            listOfNumbers.Append(42);
            listOfNumbers.Append(4);
            listOfNumbers.Append(16);
            listOfNumbers.Append(8);
            listOfNumbers.Append(15);
            listOfNumbers.Append(9);
            listOfNumbers.Append(55);
            listOfNumbers.Append(0);
            listOfNumbers.Append(34);
            listOfNumbers.Append(12);
            listOfNumbers.Append(2);

            listOfNumbers.SelectionSort();

            var intArray = listOfNumbers.ToArray();

            Debug.Assert(intArray[0] == 0 && intArray[intArray.Length - 1] == 55, "Wrong sorting!");
		}