コード例 #1
0
ファイル: Program.cs プロジェクト: Artmageddon/DataStructures
 static void Main(string[] args)
 {
     DoublyLinkedList list = new DoublyLinkedList();
     list.InsertAtHead("Arthur");
     list.InsertAtHead("Katie");
     list.InsertAtPosition(2, "Shelley");
     list.PrintFromHead();
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: st-mrazik/POT
 private static void Main(string[] args)
 {
     var list = new DoublyLinkedList<int>();
     Random rnd = new Random();
     for (int i = 0; i < 20; i++)
     {
         list.Add(i);
     }
     //Console.WriteLine(list[7]);
     //list[3] = 6;
     foreach (var s in list)
     {
         Console.WriteLine(s);
     }
     //Console.WriteLine(list[3]);
     Console.ReadLine();
 }
コード例 #3
0
        static void Main(string[] args)
        {
            DoublyLinkedList <int> dList = new DoublyLinkedList <int>(1);

            for (int i = 0; i < 10; i++)
            {
                dList.InsertAtIndex(0, i);

                Console.WriteLine("Size: " + dList.GetSize());
            }

            for (int i = 0; i < dList.GetSize(); i++)
            {
                Console.Write(dList.GetNode(i) + " ");
            }
            Console.WriteLine();
            dList.Swap(0, 1);
            foreach (dNode <int> node in dList)
            {
                Console.Write(node + " ");
            }
            Console.WriteLine();
            dList.Sort();
            for (int i = 0; i < dList.GetSize(); i++)
            {
                Console.Write(dList.GetNode(i) + " ");
            }
            Console.WriteLine();

            for (int i = 0; i < 10; i++)
            {
                try
                {
                    dList.DeleteAtIndex(0);
                    Console.WriteLine("Size: " + dList.GetSize());
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }
            DoublyLinkedList <String> newList = new DoublyLinkedList <String>();

            for (int i = 0; i < 10; i++)
            {
                newList.InsertHead(i.ToString());
            }
            Console.WriteLine(newList.GetSize());
            newList.Sort();
            foreach (dNode <String> node in newList)
            {
                Console.Write(node + " ");
            }
            Console.WriteLine();
            var nodes = from list in newList where (list.GetValue().Equals("2")) select list;

            foreach (dNode <String> node in nodes)
            {
                Console.Write(node + " ");
            }
            Console.WriteLine();
        }
コード例 #4
0
 public MyEnumenator(DoublyLinkedList <T> list)
 {
     myList = list;
 }
コード例 #5
0
ファイル: Tester.cs プロジェクト: ocben1/DoublyLinkedList
        static void Main(string[] args)
        {
            DoublyLinkedList <int> list = null;
            string result = "";

            // test 1
            try
            {
                Console.WriteLine("\nTest A: Create a new list by calling 'DoublyLinkedList<int> vector = new DoublyLinkedList<int>( );'");
                list = new DoublyLinkedList <int>();
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                result = result + "A";
            }
            catch (Exception exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine(exception.ToString());
                result = result + "-";
            }

            // test 2
            try
            {
                Console.WriteLine("\nTest B: Add a sequence of numbers 2, 6, 8, 5, 1, 8, 5, 3, 5 with list.AddLast( )");
                list.AddLast(2); list.AddLast(6); list.AddLast(8); list.AddLast(5); list.AddLast(1); list.AddLast(8); list.AddLast(5); list.AddLast(3); list.AddLast(5);
                if (!CheckIntSequence(new int[] { 2, 6, 8, 5, 1, 8, 5, 3, 5 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                result = result + "B";
            }
            catch (Exception exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine(exception.ToString());
                result = result + "-";
            }

            // test 3
            try
            {
                Console.WriteLine("\nTest C: Remove sequentially 4 last numbers with list.RemoveLast( )");
                list.RemoveLast();
                list.RemoveLast();
                list.RemoveLast();
                list.RemoveLast();
                if (!CheckIntSequence(new int[] { 2, 6, 8, 5, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                result = result + "C";
            }
            catch (Exception exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine(exception.ToString());
                result = result + "-";
            }

            // test 4
            try
            {
                Console.WriteLine("\nTest D: Add a sequence of numbers 10, 20, 30, 40, 50 with list.AddFirst( )");
                list.AddFirst(10); list.AddFirst(20); list.AddFirst(30); list.AddFirst(40); list.AddFirst(50);
                if (!CheckIntSequence(new int[] { 50, 40, 30, 20, 10, 2, 6, 8, 5, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                result = result + "D";
            }
            catch (Exception exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine(exception.ToString());
                result = result + "-";
            }

            // test 5
            try
            {
                Console.WriteLine("\nTest E: Remove sequentially 3 last numbers with list.RemoveFirst( )");
                list.RemoveFirst();
                list.RemoveFirst();
                list.RemoveFirst();
                if (!CheckIntSequence(new int[] { 20, 10, 2, 6, 8, 5, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                result = result + "E";
            }
            catch (Exception exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine(exception.ToString());
                result = result + "-";
            }

            INode <int> node1 = null;

            // test 6
            try
            {
                Console.WriteLine("\nTest F: Run a sequence of operations: ");

                Console.WriteLine("list.Find(40);");
                if (list.Find(40) == null)
                {
                    Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                }
                else
                {
                    throw new Exception("40 must no longer be in the list");
                }

                Console.WriteLine("list.Find(0);");
                if (list.Find(0) == null)
                {
                    Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                }
                else
                {
                    throw new Exception("0 must not be in the list");
                }

                Console.WriteLine("list.Find(2);");
                node1 = list.Find(2);
                if (node1 != null && node1.Value == 2)
                {
                    Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                }
                else
                {
                    throw new Exception("2 must be in the list, but 'list.Find(2)' does not return the correct result");
                }

                result = result + "F";
            }
            catch (Exception exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine(exception.ToString());
                result = result + "-";
            }

            // test 7
            try
            {
                Console.WriteLine("\nTest G: Run a sequence of operations: ");

                Console.WriteLine("Add {1} before the node with {0} with list.AddBefore({0},{1})", node1.Value, 100);
                list.AddBefore(node1, 100);
                if (!CheckIntSequence(new int[] { 20, 10, 100, 2, 6, 8, 5, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                Console.WriteLine("Add {1} after the node with {0} with list.AddAfter({0},{1})", node1.Value, 200);
                list.AddAfter(node1, 200);
                if (!CheckIntSequence(new int[] { 20, 10, 100, 2, 200, 6, 8, 5, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                Console.WriteLine("Add {0} before node list.First with list.AddBefore(list.First,{0})", 300);
                list.AddBefore(list.First, 300);
                if (!CheckIntSequence(new int[] { 300, 20, 10, 100, 2, 200, 6, 8, 5, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                Console.WriteLine("Add {0} after node list.First with list.AddAfter(list.First,{0})", 400);
                list.AddAfter(list.First, 400);
                if (!CheckIntSequence(new int[] { 300, 400, 20, 10, 100, 2, 200, 6, 8, 5, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                Console.WriteLine("Add {0} before node list.First with list.AddBefore(list.Last,{0})", 500);
                list.AddBefore(list.Last, 500);
                if (!CheckIntSequence(new int[] { 300, 400, 20, 10, 100, 2, 200, 6, 8, 5, 500, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                Console.WriteLine("Add {0} after node list.First with list.AddAfter(list.Last,{0})", 600);
                list.AddAfter(list.Last, 600);
                if (!CheckIntSequence(new int[] { 300, 400, 20, 10, 100, 2, 200, 6, 8, 5, 500, 1, 600 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                result = result + "G";
            }
            catch (Exception exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine(exception.ToString());
                result = result + "-";
            }

            // test 8
            try
            {
                Console.WriteLine("\nTest H: Run a sequence of operations: ");

                Console.WriteLine("Remove the node list.First with list.Remove(list.First)");
                list.Remove(list.First);
                if (!CheckIntSequence(new int[] { 400, 20, 10, 100, 2, 200, 6, 8, 5, 500, 1, 600 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                Console.WriteLine("Remove the node list.Last with list.Remove(list.Last)");
                list.Remove(list.Last);
                if (!CheckIntSequence(new int[] { 400, 20, 10, 100, 2, 200, 6, 8, 5, 500, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                Console.WriteLine("Remove the node list.Before, which is before the node containing element {0}, with list.Remove(list.Before(...))", node1.Value);
                list.Remove(list.Before(node1));
                if (!CheckIntSequence(new int[] { 400, 20, 10, 2, 200, 6, 8, 5, 500, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                Console.WriteLine("Remove the node containing element {0} with list.Remove(...)", node1.Value);
                list.Remove(node1);
                if (!CheckIntSequence(new int[] { 400, 20, 10, 200, 6, 8, 5, 500, 1 }, list))
                {
                    throw new Exception("The list stores incorrect sequence of integers");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());

                result = result + "H";
            }
            catch (Exception exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine(exception.ToString());
                result = result + "-";
            }

            // test 9
            try
            {
                Console.WriteLine("\nTest I: Remove the node containing element {0}, which has been recently deleted, with list.Remove(...)", node1.Value);
                list.Remove(node1);
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine("Last operation is invalid and must throw InvalidOperationException. Your solution does not match specification.");
                result = result + "-";
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                result = result + "I";
            }
            catch (Exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine("Last operation is invalid and must throw InvalidOperationException. Your solution does not match specification.");
                result = result + "-";
            }

            // test 10
            try
            {
                Console.WriteLine("\nTest J: Clear the content of the vector via calling vector.Clear();");
                list.Clear();
                if (!CheckIntSequence(new int[] { }, list))
                {
                    throw new Exception("The list stores incorrect data. It must be empty.");
                }
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                result = result + "J";
            }
            catch (Exception exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine(exception.ToString());
                result = result + "-";
            }

            // test 11
            try
            {
                Console.WriteLine("\nTest K: Remove last element for the empty list with list.RemoveLast()");
                list.RemoveLast();
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine("Last operation is invalid and must throw InvalidOperationException. Your solution does not match specification.");
                result = result + "-";
            }
            catch (InvalidOperationException)
            {
                Console.WriteLine(" :: SUCCESS: list's state " + list.ToString());
                result = result + "K";
            }
            catch (Exception)
            {
                Console.WriteLine(" :: FAIL: list's state " + list.ToString());
                Console.WriteLine("Last operation is invalid and must throw InvalidOperationException. Your solution does not match specification.");
                result = result + "-";
            }


            Console.WriteLine("\n\n ------------------- SUMMARY ------------------- ");
            Console.WriteLine("Tests passed: " + result);
            Console.ReadKey();
        }