コード例 #1
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);
        }
コード例 #2
0
        /// <summary>
        /// This method is used to test the Linked Lists in the console.
        /// </summary>
        static void TestLL()
        {
            LinkdList ll = new LinkdList(new Node(10));

            ll.Add(new Node(15));
            ll.Add(new Node(20));

            ll.Print();
            // 20 -> 15 -> 10 -> NULL;
            Console.WriteLine();

            Console.WriteLine("Let's find Node 10");

            Node found = ll.Find(10);

            Console.WriteLine(found.Value);

            Node n3 = new Node(27);
            Node n6 = new Node(12);
            Node n7 = new Node(24);

            ll.AddBefore(n3, ll.Find(15));
            ll.AddAfter(n6, ll.Find(27));
            ll.AddLast(n7);

            ll.Print();
            Console.WriteLine();
            // 20 -> 27 -> 12 -> 15 -> 10 -> 24 -> NULL;
        }
コード例 #3
0
        public void LLCanAddAfter(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.AddAfter(node4, node2);

            Assert.Equal(value, list.Find(value).Value);
        }
コード例 #4
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}");
        }