コード例 #1
0
        public void LLCanAppend(object value)
        {
            Node node1 = new Node(0, 7);
            Node node2 = new Node(0, "cat");
            Node node3 = new Node(0, "dog");
            Node node4 = new Node(0, value);

            LinkdList list = new LinkdList(node1);

            list.Append(node2);
            list.Append(node3);
            list.Append(node4);

            Assert.Equal(value, list.Find(value).Value);
        }
コード例 #2
0
        public void CannotFindNthNumberFromEnd(int value)
        {
            Node      node1 = new Node(10);
            Node      node2 = new Node(20);
            Node      node3 = new Node(30);
            Node      node4 = new Node(40);
            Node      node5 = new Node(50);
            LinkdList list  = new LinkdList(node1);

            list.Append(node2);
            list.Append(node3);
            list.Append(node4);
            list.Append(node5);

            Assert.Equal("Your input is bad", NthNodeFromEnd(value));
        }
コード例 #3
0
        public void CanFindNthNumberFromEnd()
        {
            Node      node1 = new Node(10);
            Node      node2 = new Node(20);
            Node      node3 = new Node(30);
            Node      node4 = new Node(40);
            Node      node5 = new Node(50);
            LinkdList list  = new LinkdList(node1);

            list.Append(node2);
            list.Append(node3);
            list.Append(node4);
            list.Append(node5);

            Assert.Equal(20, NthNodeFromEnd(3));
        }
コード例 #4
0
        /// <summary>
        /// Merge two link lists by zippering them together. Every other node will be a node from the second given LL
        /// </summary>
        /// <param name="LL1">The LL that will be traversed and operated on</param>
        /// <param name="LL2">The LL that will be zippered into the first LL</param>
        /// <returns>returns the head node of the merged LL</returns>
        public static Node MergeLists(LinkdList LL1, LinkdList LL2)
        {
            Node temp = LL2.Head.Next;

            LL1.Current = LL1.Head;

            while (LL1.Current.Next.Next != null)
            {
                LL1.AddAfter(LL2.Head, LL1.Current);
                if (temp == null)
                {
                    break;
                }
                LL2.Head    = temp;
                temp        = LL2.Head.Next;
                LL1.Current = LL1.Current.Next.Next;
            }

            LL1.AddAfter(LL2.Head, LL1.Current);
            LL2.Head = temp;
            LL1.Append(LL2.Head);
            LL2.Head = null;

            return(LL1.Head);
        }
コード例 #5
0
        static void Main(string[] args)
        {
            Node      node1 = new Node("A");
            Node      node2 = new Node("B");
            Node      node3 = new Node("C");
            Node      node4 = new Node("D");
            LinkdList list  = new LinkdList(node1);

            list.Append(node2);
            list.Append(node3);
            list.Append(node4);

            Console.WriteLine("Original Linked List:");
            list.Print();

            Console.WriteLine("\nReversed Linked List:");
            ReverseLL(list).Print();
        }
コード例 #6
0
        public void CanMergeListsIfLL2IsLongerThanLL1()
        {
            Node      node1 = new Node(10);
            Node      node2 = new Node(20);
            Node      node3 = new Node(30);
            Node      node4 = new Node(40);
            Node      node5 = new Node(50);
            LinkdList LL1   = new LinkdList(node1);

            LL1.Append(node2);
            LL1.Append(node3);
            LL1.Append(node4);
            LL1.Append(node5);

            Node      node6  = new Node(60);
            Node      node7  = new Node(70);
            Node      node8  = new Node(80);
            Node      node9  = new Node(90);
            Node      node10 = new Node(100);
            Node      node11 = new Node(110);
            Node      node12 = new Node(120);
            LinkdList LL2    = new LinkdList(node6);

            LL2.Append(node7);
            LL2.Append(node8);
            LL2.Append(node9);
            LL2.Append(node10);
            LL2.Append(node11);
            LL2.Append(node12);

            Assert.Equal(LL1.Head, MergeLists(LL1, LL2));
        }
コード例 #7
0
        public static void Main(string[] args)
        {
            Node      node1 = new Node(10);
            Node      node2 = new Node(20);
            Node      node3 = new Node(30);
            Node      node4 = new Node(40);
            Node      node5 = new Node(50);
            LinkdList LL1   = new LinkdList(node1);

            LL1.Append(node2);
            LL1.Append(node3);
            LL1.Append(node4);
            LL1.Append(node5);

            Node      node6  = new Node(60);
            Node      node7  = new Node(70);
            Node      node8  = new Node(80);
            Node      node9  = new Node(90);
            Node      node10 = new Node(100);
            Node      node11 = new Node(110);
            Node      node12 = new Node(120);
            LinkdList LL2    = new LinkdList(node6);

            LL2.Append(node7);
            LL2.Append(node8);
            LL2.Append(node9);
            LL2.Append(node10);
            LL2.Append(node11);
            LL2.Append(node12);

            Console.WriteLine("Linked List 1:");
            LL1.Print();

            Console.WriteLine("\nLinked List 2:");
            LL2.Print();

            Console.WriteLine("\nMerged Link Lists:");
            MergeLists(LL1, LL2);
            LL1.Print();
        }
コード例 #8
0
        /// <summary>
        /// Finds the value of the nth node from the end of a LL
        /// </summary>
        /// <param name="n">nth number</param>
        /// <returns>returns the value of the nth node from the end of the LL</returns>
        public static object NthNodeFromEnd(int n)
        {
            Node      node1 = new Node(10);
            Node      node2 = new Node(20);
            Node      node3 = new Node(30);
            Node      node4 = new Node(40);
            Node      node5 = new Node(50);
            LinkdList list  = new LinkdList(node1);

            list.Append(node2);
            list.Append(node3);
            list.Append(node4);
            list.Append(node5);

            int counter = 0;

            list.Current = list.Head;
            while (list.Current.Next != null)
            {
                list.Current = list.Current.Next;
                counter++;
            }

            if (n < 0 || n > counter)
            {
                Exception ex = new Exception("Your input is bad");
                return(ex.Message);
            }

            list.Current = list.Head;
            for (int i = 0; i < (counter - n); i++)
            {
                list.Current = list.Current.Next;
            }

            return(list.Current.Value);
        }
コード例 #9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!\n");

            Node node1 = new Node(0, 7);
            Node node2 = new Node(0, "cat");
            Node node3 = new Node(0, "dog");
            Node node4 = new Node(0, 14);
            Node node5 = new Node(0, "bird");

            LinkdList list = new LinkdList(node1);

            list.Add(node2);
            list.Append(node3);
            list.AddBefore(node4, node1);
            list.AddAfter(node5, node3);
            list.Print();
            object searchTerm = 14;
            Node   foundNode  = list.Find(searchTerm);

            Console.WriteLine($"You searched for {searchTerm} and found {foundNode.Value}");
        }