public MyLinkedListNode AddLists(MyLinkedListNode list1, MyLinkedListNode list2, int carry)
            {
                if (list1 == null && list2 == null && carry == 0)
                {
                    return(null);
                }

                var result = new MyLinkedListNodeDoubly();
                var value  = carry;

                if (list1 != null)
                {
                    value += (int)list1.Data;
                }
                if (list2 != null)
                {
                    value += (int)list2.Data;
                }

                result.Data = value % 10;

                if (list1 != null || list2 != null)
                {
                    var more = AddLists(list1 == null ? null : list1.Next,
                                        list2 == null ? null : list2.Next,
                                        value >= 10 ? 1 : 0);
                    result.SetNext(more);
                }

                return(result);
            }
        /// <summary>
        /// Delete dups using additional memory (e.g. Hash table or Dictionary)
        /// </summary>
        /// <param name="head"></param>
        public void DeleteDups_UsingDictionary(MyLinkedListNodeDoubly head)
        {
            Console.WriteLine($"{this.GetType().FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name}\n");
            head.PrintForward("List-Before:");
            var dictionaryTable             = new Dictionary <int, bool>();
            MyLinkedListNodeDoubly previous = null;

            while (head != null)
            {
                if (dictionaryTable.ContainsKey((int)head.Data))
                {
                    if (previous != null)
                    {
                        previous.Next = head.Next;
                    }
                }
                else
                {
                    dictionaryTable.Add((int)head.Data, true);
                    previous = head;
                }

                head = (MyLinkedListNodeDoubly)head.Next;
            }

            Console.WriteLine($"dictionaryTable =[{string.Join(", ", dictionaryTable.Keys)}]");

            if (head != null)
            {
                head.PrintForward("List-After\n");
            }

            Console.WriteLine("-------------------------------------------------------");
        }
            public void Run()
            {
                const int length = 10;
                var       nodes  = new MyLinkedListNodeDoubly[length];

                for (var i = 0; i < length; i++)
                {
                    nodes[i] = new MyLinkedListNodeDoubly(i >= length / 2 ? length - i - 1 : i, null, null);
                }

                for (var i = 0; i < length; i++)
                {
                    if (i < length - 1)
                    {
                        nodes[i].SetNext(nodes[i + 1]);
                    }

                    if (i > 0)
                    {
                        nodes[i].SetPrevious(nodes[i - 1]);
                    }
                }
                // nodes[length - 2].data = 9; // Uncomment to ruin palindrome

                var head = nodes[0];

                head.PrintForward("Original:");
                Console.WriteLine("\nIsPalindrome: " + IsPalindrome_UsingRecursionMain(head));
                Console.WriteLine("\nIsPalindrome2: " + IsPalindrome_UsingStack(head));
            }
            public MyLinkedListNode Partition3(MyLinkedListNode listHead, int pivot)
            {
                var leftList  = new MyLinkedListNodeDoubly(); // empty temp node to not have an IF inside the loop
                var rightList = new MyLinkedListNodeDoubly(pivot, null, null);

                var leftListHead  = leftList;  // Used at the end to remove the empty node.
                var rightListHead = rightList; // Used at the end to merge lists.

                var currentNode = listHead;

                while (currentNode != null)
                {
                    if ((int)currentNode.Data < pivot)
                    {
                        leftList = new MyLinkedListNodeDoubly(currentNode.Data, null, leftList);
                    }
                    else if ((int)currentNode.Data > pivot)
                    {
                        rightList = new MyLinkedListNodeDoubly(currentNode.Data, null, rightList);
                    }

                    currentNode = currentNode.Next;
                }

                leftList.Next = rightListHead;

                var finalList = leftListHead.Next;

                leftListHead.Next = null; // remove the temp node, GC will release the mem

                return(finalList);
            }
        public void DeleteDups_Run()
        {
            Console.WriteLine($"{this.GetType().FullName}.{System.Reflection.MethodBase.GetCurrentMethod().Name}");
            var first        = new MyLinkedListNodeDoubly(0, null, null);
            var originalList = first;
            var second       = first;

            for (var i = 1; i < 8; i++)
            {
                second = new MyLinkedListNodeDoubly(i % 2, null, null);
                first.SetNext(second);
                second.SetPrevious(first);
                first = second;
            }
            originalList.PrintForward("originalList");

            var list1 = originalList.CloneLinkedListNodeDoubly();
            var list2 = originalList.Clone();
            var list3 = originalList.Clone();
            var list4 = originalList.Clone();

            DeleteDups_UsingDictionary(list1);
            DeleteDups_UsingTwoPointer(list2);
            //DeleteDupsC_InProgress(list3);
            //DeleteDupsC2_InProgress(list4);
        }
            public void Run()
            {
                /* Create linked list */
                int[] vals    = { 1, 3, 7, 5, 2, 9, 4 };
                var   head    = new MyLinkedListNodeDoubly(vals[0], null, null);
                var   current = head;

                for (var i = 1; i < vals.Length; i++)
                {
                    current = new MyLinkedListNodeDoubly(vals[i], null, current);
                }
                head.PrintForward("Original:");

                var head2 = head.Clone();
                var head3 = head.Clone();
                var head4 = head.Clone();

                /* Partition */
                var h  = Partition(head, 5);
                var h2 = Partition2(head2, 5);
                var h3 = Partition3(head3, 5);
                var h4 = Partition4(head4, 5);

                /* Print Result */
                h.PrintForward("Partition:");
                h2.PrintForward("Partition2");
                h3.PrintForward("Partition3");
                h4.PrintForward("Partition4");
            }
Пример #7
0
 public void SetPrevious(MyLinkedListNodeDoubly p)
 {
     Prev = p;
     if (p != null && p.Next != this)
     {
         p.SetNext(this);
     }
 }
            public void Run()
            {
                #region First Part

                {
                    var lA1 = new MyLinkedListNodeDoubly(9, null, null);
                    var lA2 = new MyLinkedListNodeDoubly(9, null, lA1);
                    var lA3 = new MyLinkedListNodeDoubly(9, null, lA2);

                    var lB1 = new MyLinkedListNodeDoubly(1, null, null);
                    var lB2 = new MyLinkedListNodeDoubly(0, null, lB1);
                    var lB3 = new MyLinkedListNodeDoubly(0, null, lB2);

                    var list3 = AddLists(lA1, lB1, 0);

                    lA1.PrintForward("");
                    lB1.PrintForward("+");
                    list3.PrintForward("=");

                    var l1 = LinkedListToInt(lA1);
                    var l2 = LinkedListToInt(lB1);
                    var l3 = LinkedListToInt(list3);

                    Console.Write(l1 + " + " + l2 + " = " + l3 + "\n");
                    Console.WriteLine(l1 + " + " + l2 + " = " + (l1 + l2));
                }

                #endregion First Part

                #region Followup

                {
                    var lA1 = new MyLinkedListNodeDoubly(3, null, null);
                    var lA2 = new MyLinkedListNodeDoubly(1, null, lA1);
                    //LinkedListNode lA3 = new LinkedListNode(5, null, lA2);

                    var lB1 = new MyLinkedListNodeDoubly(5, null, null);
                    var lB2 = new MyLinkedListNodeDoubly(9, null, lB1);
                    var lB3 = new MyLinkedListNodeDoubly(1, null, lB2);

                    var list3 = AddLists2(lA1, lB1);

                    lA1.PrintForward("");
                    lB1.PrintForward("+");
                    list3.PrintForward("=");

                    var l1 = linkedListToInt(lA1);
                    var l2 = linkedListToInt(lB1);
                    var l3 = linkedListToInt(list3);

                    Console.Write(l1 + " + " + l2 + " = " + l3 + "\n");
                    Console.WriteLine(l1 + " + " + l2 + " = " + (l1 + l2));
                }

                #endregion Followup
            }
        public static MyLinkedListNode CreateLinkedListFromArray(int[] vals)
        {
            MyLinkedListNode       head    = new MyLinkedListNodeDoubly(vals[0], null, null);
            MyLinkedListNodeDoubly current = (MyLinkedListNodeDoubly)head;

            for (int i = 1; i < vals.Length; i++)
            {
                current = new MyLinkedListNodeDoubly(vals[i], null, current);
            }
            return(head);
        }
Пример #10
0
        public MyLinkedListNodeDoubly CloneLinkedListNodeDoubly()
        {
            MyLinkedListNodeDoubly next2 = null;

            if (Next != null)
            {
                next2 = (MyLinkedListNodeDoubly)((MyLinkedListNode)Next).Clone();
            }
            MyLinkedListNodeDoubly head2 = new MyLinkedListNodeDoubly(Data, next2, null);

            return(head2);
        }
            public MyLinkedListNode insertBefore(MyLinkedListNode list, int data)
            {
                var listD = (MyLinkedListNodeDoubly)list;
                var node  = new MyLinkedListNodeDoubly(data, null, null);

                if (listD != null)
                {
                    listD.Prev = node;
                    node.Next  = listD;
                }

                return(node);
            }
        public static MyLinkedListNode LinkedListWithValue(int N, int value)
        {
            MyLinkedListNode root = new MyLinkedListNodeDoubly(value, null, null);
            MyLinkedListNode prev = root;

            for (int i = 1; i < N; i++)
            {
                MyLinkedListNode next = new MyLinkedListNodeDoubly(value, null, null);
                prev.SetNext(next);
                prev = next;
            }
            return(root);
        }
Пример #13
0
        public override void SetNext(MyLinkedListNode n)
        {
            MyLinkedListNodeDoubly newNextNode = (MyLinkedListNodeDoubly)n;

            Next = newNextNode;
            if (this == CurrentNode)
            {
                CurrentNode = newNextNode;
            }
            if (n != null && newNextNode.Prev != this)
            {
                newNextNode.SetPrevious(this);
            }
        }
        public static MyLinkedListNode GetLinkedListDoubly_Random(int N, int min, int max)
        {
            MyLinkedListNode root = new MyLinkedListNodeDoubly(AssortedMethods.RandomIntInRange(min, max), null, null);
            MyLinkedListNode prev = root;

            for (int i = 1; i < N; i++)
            {
                int data = AssortedMethods.RandomIntInRange(min, max);
                MyLinkedListNode next = new MyLinkedListNodeDoubly(data, null, null);
                prev.SetNext(next);
                prev = next;
            }
            return(root);
        }
            public MyLinkedListNode PadList(MyLinkedListNode listNode, int padding)
            {
                var head = (MyLinkedListNodeDoubly)listNode;

                for (var i = 0; i < padding; i++)
                {
                    var n = new MyLinkedListNodeDoubly(0, null, null);
                    head.Prev = n;
                    n.Next    = head;
                    head      = n;
                }

                return(head);
            }
        public MyLinkedListNode Cycle_Create()
        {
            const int listLength = 10;
            const int k          = 3;

            // Create linked list
            var nodes = new MyLinkedListNode[listLength];

            for (var i = 1; i <= listLength; i++)
            {
                nodes[i - 1] = new MyLinkedListNodeDoubly(i, null, i - 1 > 0 ? (MyLinkedListNodeDoubly)nodes[i - 2] : null);
                Console.Write("{0} -> ", nodes[i - 1].Data);
            }
            // Create loop;
            Console.WriteLine($"\n Create loop: nodes[listLength - 1].Next = nodes[listLength - k - 1]");
            nodes[listLength - k - 1].PrintForward("nodes[listLength - k - 1]");
            nodes[listLength - 1].PrintForward("nodes[listLength - 1]");

            nodes[listLength - 1].Next = nodes[listLength - k - 1];
            Console.WriteLine("\n{0} -> {1}", nodes[listLength - 1].Data, nodes[listLength - k - 1].Data);
            Console.WriteLine("Nodes:\n");

            return(nodes[0]);
        }
Пример #17
0
 public MyLinkedListNodeDoubly(object d, MyLinkedListNodeDoubly n, MyLinkedListNodeDoubly p)
 {
     Data = d;
     SetNext(n);
     SetPrevious(p);
 }