public void InsertsNodeBeforeSpecifiedNode()
 {
     LinkedList<int> list = new LinkedList<int>() { 1, 2, 3 };
     list.InsertBefore(3, 4);
     int[] result = new int[] { 1, 2, 4, 3 };
     Assert.Equal(result, list);
 }
        static void Main(string[] args)
        {
            LinkedList myList = new LinkedList();

            try
            {
                int[] nums = { 42, 5, 9999, 777, 911 };
                foreach (int num in nums)
                {
                    myList.Insert(num);
                    Console.WriteLine($"{num} inserted to head of linked list.");
                }
                Console.WriteLine();

                Console.WriteLine($"Does 99 exist on the linked list?   {(myList.Includes(99) ? "Yes" : "No")}");
                Console.WriteLine($"Does 42 exist on the linked list?   {(myList.Includes(42) ? "Yes" : "No")}");
                Console.WriteLine($"Does 777 exist on the linked list?   {(myList.Includes(777) ? "Yes" : "No")}");
                Console.WriteLine();

                int[] moarNums = { 8, 16, -3 };
                foreach (int num in moarNums)
                {
                    myList.Insert(num);
                    Console.WriteLine($"{num} appended to linked list.");
                }
                Console.WriteLine();

                int[] manyNums       = { 25, 50, 75 };
                int[] manyNumsTarget = { 5, 911, 16 };
                for (int i = 0; i < manyNums.Length; i++)
                {
                    myList.InsertBefore(manyNumsTarget[i], manyNums[i]);
                    Console.WriteLine($"{manyNums[i]} added to list before {manyNumsTarget[i]}.");
                }
                Console.WriteLine();

                int[] suchNums       = { 15, 45, 60 };
                int[] suchNumsTarget = { 9999, -3, 75 };
                for (int i = 0; i < manyNums.Length; i++)
                {
                    myList.InsertAfter(suchNumsTarget[i], suchNums[i]);
                    Console.WriteLine($"{suchNums[i]} added to list after {suchNumsTarget[i]}.");
                }
                Console.WriteLine();

                myList.Print();
                Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                Console.WriteLine("End of test.");
            }
        }
Пример #3
0
        /// <summary>
        /// First create an empty LinkedList named linkedlist
        /// Then use Append method to add new nodes to linkedlist
        /// Finally, use InsertBefore method to insert a new node with a value of 10 before the node with a value of 5
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            LinkedList linkedlist = new LinkedList();

            linkedlist.Append(1);
            linkedlist.Append(2);
            linkedlist.Append(3);
            linkedlist.Append(4);
            linkedlist.Append(5);
            linkedlist.Append(6);
            linkedlist.InsertBefore(5, 10);
        }