Esempio n. 1
0
        /**
         *  //============ Helper Functions ========================
         */

        private void CheckStatusAndPerformOperation(
            LinkedList.SNode curr,
            Hashtable lht,
            LinkedList.SNode prev = null
            )
        {
            if (curr == null)
            {
                return;
            }

            Console.WriteLine("checking status for {0}", curr.data);

            int data = curr.data;
            int len  = 0;

            if (lht[data] != null)
            {
                len = (int)lht[data];
            }

            if (len == 0)
            {
                Console.WriteLine("lenght is  0");
                lht.Add(data, 1);
                //this.LogHash(lht);
            }
            else if (len == 1)
            {
                Console.WriteLine("lenght is  1");
                RemoveNode(curr, lht, prev);
            }
        }
Esempio n. 2
0
        public void RemoveDups(SinglyLinkedList singly)
        {
            LinkedList.SNode temp = singly.head;
            LinkedList.SNode prev = null;

            Hashtable lht = new Hashtable
            {
                /**
                 *  Since the first element will never be a duplicate
                 *  we can therefore add it into the hashtable
                 *  as an initial element
                 */
                { temp.data, 1 }
            };

            while (temp.next != null)
            {
                Console.WriteLine(temp.next.data);
                prev = temp;
                CheckStatusAndPerformOperation(temp.next, lht, prev);
                temp = temp.next;
            }

            HelperSLL objHelper = new HelperSLL();

            objHelper.PrintList(singly);
            Console.WriteLine();
            Console.WriteLine("Done");
        }
Esempio n. 3
0
        public int KthItemSingly(SinglyLinkedList singly, int kth)
        {
            LinkedList.SNode main_ptr_node = singly.head;
            LinkedList.SNode ref_ptr_node  = singly.head;

            int count = 0;

            while (ref_ptr_node != null && kth != count)
            {
                // Console.WriteLine("Count - {0}, Kth - {1}", count, kth);
                // Console.WriteLine("ref - {0} , main - {1}", ref_ptr_node.data, main_ptr_node.data);
                ref_ptr_node = ref_ptr_node.next;
                count++;
            }

            // Console.WriteLine("Broken out of Loop");

            while (ref_ptr_node != null)
            {
                // Console.WriteLine("Count - {0}, Kth - {1}", count, kth);
                // Console.WriteLine("ref - {0} , main - {1}", ref_ptr_node.data, main_ptr_node.data);
                ref_ptr_node  = ref_ptr_node.next;
                main_ptr_node = main_ptr_node.next;
            }

            return(main_ptr_node.data);
        }
Esempio n. 4
0
        private void RemoveNode(LinkedList.SNode curr, Hashtable lht, LinkedList.SNode prev = null)
        {
            if (curr == null)
            {
                return;
            }
            Console.WriteLine("removing {0}", curr.data);
            Console.WriteLine("");

            //lht[curr.data] = (int)lht[curr.data] - 1;
            prev.next = curr.next;
            CheckStatusAndPerformOperation(curr.next, lht, prev);
        }
Esempio n. 5
0
        private int LinkedListSize(SinglyLinkedList singly, List <KeyValuePair <int, int> > lht)
        {
            LinkedList.SNode temp = singly.head;

            int count = 0;

            while (temp != null)
            {
                count++;
                this.AddToList(lht, temp.data, count);
                temp = temp.next;

                this.PrintList(lht);
            }

            return(count);
        }
Esempio n. 6
0
        public void DeleteNodeFromMiddle(SinglyLinkedList singly, int?itemToDelete)
        {
            if (itemToDelete == null)
            {
                return;
            }
            LinkedList.SNode temp = singly.head;
            LinkedList.SNode prev = null;

            while (temp.data != -1 && temp.data != itemToDelete)
            {
                prev = temp;
                temp = temp.next;
            }

            if (temp.data != -1)
            {
                prev.next = temp.next;
            }

            return;
        }