Ejemplo n.º 1
0
        /// <summary>
        /// UC 1 Adds the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        public void Add(string key)
        {
            int index = GetArrayIndex(key);

            if (linkedlist[index] == null)
            {
                linkedlist[index] = new LinkedList <MyMapNode>();
            }

            // Search the linked list of respective index
            // If key already exists then increase its frequency
            // Else add the element
            foreach (MyMapNode element in linkedlist[index])
            {
                if (element.Key.Equals(key))
                {
                    element.Value += 1;
                    return;
                }
            }
            MyMapNode temp = new MyMapNode(key);

            temp.Value = 1;
            linkedlist[index].AddLast(temp);
        }
Ejemplo n.º 2
0
        public static void HashTableDemo()
        {
            Console.WriteLine("Welcome to Hash Table Demo");
            var sentence = "Paranoids are not paranoid because they are paranoid but because they keep putting themselves deliberately into paranoid avoidable situations";

            string[] words = sentence.Split(' ');
            var      map   = new MyMapNode <string, int>(10);

            foreach (var word in words)
            {
                if (map.Get(word) == 0)
                {
                    map.Add(word, 1);
                }
                else
                {
                    var freq = map.Get(word) + 1;
                    map.Set(word, freq);
                }
            }
            Console.WriteLine("Frequency of Word \"paranoid\" is : {0}", map.Get("paranoid"));
            Console.WriteLine("Before Removal : ");
            Console.WriteLine("Frequency of word \"avoidable\" is : {0}", map.Get("avoidable"));
            map.Remove("avoidable");
            Console.WriteLine("After Removal : ");
            Console.WriteLine("Frequency of word \"avoidable\" is : {0}", map.Get("avoidable"));
        }
Ejemplo n.º 3
0
        public void Remove(string key)
        {
            int index = GetArrayIndex(key);

            // If the linked list at that index is null
            if (linkedlist[index] == null)
            {
                Console.WriteLine("The element not found");
                return;
            }
            int       count = 0;
            MyMapNode temp  = null;

            // Search the linked list of respective index
            // If the element is found then proceed
            // else return
            foreach (MyMapNode element in linkedlist[index])
            {
                count++;
                if (element.Key.Equals(key))
                {
                    temp = element;
                    break;
                }
                else if (linkedlist[index].Count == count)
                {
                    return;
                }
            }

            // Remove the element if it exists
            linkedlist[index].Remove(temp);
        }