private void button2_Click(object sender, EventArgs e)
        {
            KarthicLinkedList linkedlist = NodeHelper.GetLinkedListByString(this.textBox6.Text);
            int value = Convert.ToInt16(this.textBox7.Text);

            this.textBox5.Text = NodeHelper.GetStringByNode(partitionlinkedlist(linkedlist.headnode, value));
        }
Exemple #2
0
        public Node AddTwoLinkedListFromBackwards(KarthicLinkedList list1, KarthicLinkedList list2)
        {
            int  length1 = NodeHelper.GetLength(list1);
            int  length2 = NodeHelper.GetLength(list2);
            Node result  = new Node();
            Node node1   = new Node();
            Node node2   = new Node();

            if (length1 > length2)
            {
                //Add padding to the shortest list
                node2 = NodeHelper.AddPadding(list2.headnode, (length1 - length2));
                node1 = list1.headnode;
            }
            else
            {
                node1 = NodeHelper.AddPadding(list1.headnode, (length2 - length1));
                node2 = list2.headnode;
            }

            //here the two nodes will be of same size
            NodeAdditionResult resultnode = AddTwoLinkedListNodesFromTail(node1, node2);

            //After adding two list, if there are any carry over add it on the top
            if (resultnode.carry > 0)
            {
                Node node = NodeHelper.AddNodeToFirst(resultnode.resultnode, resultnode.carry);
                return(node);
            }

            return(resultnode.resultnode);
        }
Exemple #3
0
        private void button6_Click(object sender, EventArgs e)
        {
            //we don't have access to the list or the head
            //give the node and we need to delete the node

            //Implemented my own node :)


            string input = this.textBox8.Text;

            //LinkedList<int> numberlist = new LinkedList<int>();
            KarthicLinkedList numberlist = new KarthicLinkedList();

            foreach (string s in input.Split(','))
            {
                Node node = new Node();
                node.Data = Convert.ToInt32(s);
                numberlist.AddNode(node);
            }


            StringBuilder sb = new StringBuilder();

            foreach (var item in numberlist)
            {
                sb.Append(item.Data.ToString()).Append(",");
            }



            this.textBox7.Text = sb.ToString();

            //  this.textBox7.Text = Deletenode(numberlist, Convert.ToInt32(this.textBox6.Text));
        }
        private void button4_Click(object sender, EventArgs e)
        {
            KarthicLinkedList list1 = NodeHelper.GetLinkedListByString(this.textBox6.Text);
            KarthicLinkedList list2 = NodeHelper.GetLinkedListByString(this.textBox4.Text);
            Node result             = FindIntersectionofTwoList(list1, list2);

            this.textBox5.Text = result.Data.ToString();
        }
Exemple #5
0
        private void button13_Click(object sender, EventArgs e)
        {
            string input = this.textBox1.Text;

            KarthicLinkedList list = NodeHelper.GetLinkedListByString(input);

            this.textBox2.Text = NodeHelper.GetStringByNode(RemoveDuplicatesWithoutExtraBuffer(list.headnode));
        }
Exemple #6
0
        private void button12_Click(object sender, EventArgs e)
        {
            string input = this.textBox1.Text;

            KarthicLinkedList list = NodeHelper.GetLinkedListByString(input);

            this.textBox2.Text = NodeHelper.GetStringByNode(AvoidDuplicateswithhead(list.headnode));
        }
        public Node FindIntersectionofTwoList(KarthicLinkedList list1, KarthicLinkedList list2)
        {
            //logic
            //since both the list are intersecting at a node. The length after the intersecting point will be the same
            //so calculate the height and then traverse the longer list to the difference
            //and the traverse both to find the common point

            int list1length = NodeHelper.GetLength(list1);
            int list2length = NodeHelper.GetLength(list2);

            KarthicLinkedList longer  = null;
            KarthicLinkedList shorter = null;

            if (list1length > list2length)
            {
                longer  = list1;
                shorter = list2;
            }
            else
            {
                longer  = list2;
                shorter = list1;
            }

            int difference = Math.Abs(list1length - list2length);

            //Traverse through the longer list for the difference

            Node node1 = longer.headnode;
            Node node2 = shorter.headnode;

            while (difference != 0)
            {
                node1 = node1.Next;
                difference--;
            }

            //here both will be at a same height now traverse to find the intersection point

            while (node1 != null && node2 != null & node1.Data != node2.Data)
            {
                node1 = node1.Next;
                node2 = node2.Next;
            }

            if (node1 == null || node2 == null)
            {
                return(null);
            }
            else
            {
                //both will be at the same node
                return(node1);
            }
        }
        public static KarthicLinkedList GetLinkedListByString(string input)
        {
            KarthicLinkedList numberlist = new KarthicLinkedList();

            foreach (string s in input.Split(','))
            {
                numberlist.AddNode(new Node(Convert.ToInt32(s)));
            }

            return(numberlist);
        }
Exemple #9
0
        private string ConvertLinkedListToString(KarthicLinkedList numberlist)
        {
            StringBuilder sb = new StringBuilder();

            foreach (var item in numberlist)
            {
                sb.Append(item.Data.ToString()).Append(",");
            }

            return(sb.ToString());
        }
        private void button3_Click(object sender, EventArgs e)
        {
            KarthicLinkedList linkedlist1 = NodeHelper.GetLinkedListByString(this.textBox1.Text);
            KarthicLinkedList linkedlist2 = NodeHelper.GetLinkedListByString(this.textBox3.Text);

            linkedlist1.headnode = KarthicLinkedListHelper.MergeSort(linkedlist1.headnode);
            linkedlist2.headnode = KarthicLinkedListHelper.MergeSort(linkedlist2.headnode);

            Node result = FindCommonElements(linkedlist1.headnode, linkedlist2.headnode);

            this.textBox2.Text = NodeHelper.GetStringByNode(result);

            //linkedlist1.headnode = KarthicLinkedListHelper.
        }
Exemple #11
0
        private void button14_Click(object sender, EventArgs e)
        {
            string input = this.textBox8.Text;

            //LinkedList<int> numberlist = new LinkedList<int>();
            KarthicLinkedList numberlist = new KarthicLinkedList();

            foreach (string s in input.Split(','))
            {
                numberlist.AddNode(new Node(Convert.ToInt32(s)));
            }

            this.textBox7.Text = Iteratefromhead(DeleteReduntactNode(numberlist.headnode, Convert.ToInt32(this.textBox6.Text)));
        }
Exemple #12
0
        private void button5_Click(object sender, EventArgs e)
        {
            string input = this.textBox1.Text;

            KarthicLinkedList       node = NodeHelper.GetLinkedListByString(input);
            KarthicBinaryTree <int> bt   = new KarthicBinaryTree <int>();

            bt.Root = new KarthicBTNode <int>(node.headnode.Data);

            //set 1st level
            bt.Root.Left  = new KarthicBTNode <int>(node.headnode.Next.Data);
            bt.Root.Right = new KarthicBTNode <int>(node.headnode.Next.Next.Data);

            //set 2nd level
            bt.Root.Left.Left   = new KarthicBTNode <int>(node.headnode.Next.Next.Next.Data);
            bt.Root.Left.Right  = new KarthicBTNode <int>(node.headnode.Next.Next.Next.Next.Data);
            bt.Root.Right.Left  = new KarthicBTNode <int>(node.headnode.Next.Next.Next.Next.Next.Data);
            bt.Root.Right.Right = new KarthicBTNode <int>(node.headnode.Next.Next.Next.Next.Next.Next.Data);
        }
        public static int GetLength(KarthicLinkedList list)
        {
            if (list == null)
            {
                return(0);
            }

            Node runner = list.headnode;

            int length = 0;

            while (runner != null)
            {
                length++;
                runner = runner.Next;
            }

            return(length);
        }
Exemple #14
0
        private void button10_Click(object sender, EventArgs e)
        {
            string            input1 = this.textBox10.Text;
            string            input2 = this.textBox11.Text;
            KarthicLinkedList list1  = new KarthicLinkedList();

            foreach (string s in input1.Split(','))
            {
                list1.AddNode(new Node(Convert.ToInt32(s)));
            }
            KarthicLinkedList list2 = new KarthicLinkedList();

            foreach (string s in input2.Split(','))
            {
                list2.AddNode(new Node(Convert.ToInt32(s)));
            }

            Node result = AddTwoLinkedListFromBackwards(list1, list2);

            this.textBox9.Text = Iteratefromhead(result);
        }
        private void button3_Click(object sender, EventArgs e)
        {
            KarthicLinkedList linkedlist = NodeHelper.GetLinkedListByString(this.textBox1.Text);
            //Make it circular
            Node node = new Node();

            //this is just a hack to make list cirucular...there might be good way to do this
            //it won't work for no loop
            foreach (Node n in linkedlist)
            {
                if (n.Data == linkedlist.lastnode.Data)
                {
                    node = n;
                    break;
                }
            }

            linkedlist.lastnode.Next = node;

            Node startloop = GetLoopStartNode(linkedlist.headnode);

            this.textBox2.Text = startloop.Data.ToString();
        }
        private void button5_Click(object sender, EventArgs e)
        {
            KarthicLinkedList linkedlist = NodeHelper.GetLinkedListByString(this.textBox10.Text);

            this.textBox9.Text = NodeHelper.GetStringByNode(MergeSort(linkedlist.headnode));
        }
        private void button1_Click(object sender, EventArgs e)
        {
            KarthicLinkedList linkedlist = NodeHelper.GetLinkedListByString(this.textBox4.Text);

            this.textBox3.Text = IsPalindrome(linkedlist.headnode) == true ? "Palindrome" : "Not a Palindrome";
        }