static void Main(string[] args) { MyMapNode <string, int> myMapNode = new MyMapNode <string, int>(5); string input = "Paranoids are not paranoid because they are paranoid but because " + "they keep putting themselves deliberately into paranoid avoidable situations"; string[] sample = input.Split(' '); foreach (string s in sample) { int count = 0; foreach (string m in sample) { if (s.Equals(m)) { count++; } } myMapNode.Add(s, count); } myMapNode.Display(); myMapNode.Frequency("paranoid"); myMapNode.RemoveElement("avoidable"); myMapNode.Display(); }
static void Main(string[] args) { Console.WriteLine("Hash table demo"); //() [] string paragraph = "Paranoids are not paranoid because they are paranoid but because they keep putting themselves deliberately into paranoid avoidable situations"; para = paragraph.Split(' '); MyMapNode <string, string> hash = new MyMapNode <string, string>(para.Length); int key = 0; foreach (string word in para) { hash.Add(key.ToString(), word); key++; } Console.WriteLine("Index Key Value"); for (int i = 0; i < para.Length; i++) { hash.Display(i.ToString()); } for (int i = 0; i < para.Length; i++) { try { MyMapNode <string, string> .dict.Add(para[i], 0); hash.Frequency(i.ToString(), para); } catch (Exception ex) { Console.WriteLine(ex.Message); } } foreach (KeyValuePair <string, int> x in MyMapNode <string, string> .dict) { Console.WriteLine(x.Key + " " + x.Value); } Console.WriteLine("Index Key Value"); hash.Remove("17"); for (int i = 0; i < para.Length; i++) { hash.Display(i.ToString()); } Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("Welcome to HashTable Program"); MyMapNode <int, string> hash = new MyMapNode <int, string>(15);//creating object and Key and value datatype is string and size is 5 //creating a string of sentence string phrase = "Paranoids are not paranoid because they are paranoid but because they keep putting themselves deliberately into paranoid avoidable situations"; //adding values into array string[] words = phrase.Split(' '); //split method is used //split string into substrings int a = 1; foreach (var element in words) { hash.Add(a, element); a++; } int index = 18; string choice = hash.Get(index); // getting the specific value from hashtable. Console.WriteLine("{0}th index values : is {1}", index, choice); int index2 = 15; string choice2 = hash.Get(index2); Console.WriteLine("{0}th index values : is {1}", index2, choice2); hash.Remove(index);//remove 18 index hash.Display(); Console.Read(); }
static void Main(string[] args) { Console.WriteLine("-----Welcome To HashTable Program-----"); MyMapNode <string, int> hash = new MyMapNode <string, int>(5); //creating a string of long sentence string words = "Paranoids are not paranoid because they are paranoid but because they keep putting themselves deliberately into paranoid avoidable situations"; //adding values into array string[] arr = words.Split(' '); //creating a linkedlist to check for duplicates value LinkedList <string> checkForDuplication = new LinkedList <string>(); //iterating loop over the array of sentence words foreach (string element in arr) { int count = 0; //another foreach loop to check if other same element is present //if present counter is incremented foreach (string match in arr) { if (element == match) { count++; //if same element is coming in 2nd time, it will be presnent in linkedlist, so loopwill break and no further counting will be done if (checkForDuplication.Contains(element)) { break; } } } //if element is already there in list, outer loop will be continued and loop will shift to next value //basically only values appearing for 1st time, will be added in linkedlist and added in hash table, so that frequency can be displayed. if (checkForDuplication.Contains(element)) { continue; } //added element in linkedlist checkForDuplication.AddLast(element); //added element and it's frequency in hashtable. hash.Add(element, count); } //getting the specific value from hashtable. int frequency = hash.Get("they"); Console.WriteLine("frequency for they:\t" + frequency); //Displaying all the elements from the linkedlist Console.WriteLine(); Console.WriteLine("Displaying all the key value pairs in hash table"); hash.Display(); Console.Read(); }
static void Main(string[] args) { Console.WriteLine("Enter the sentence"); string sentence = Console.ReadLine(); string[] splitMessage = sentence.Split(" "); MyMapNode <string, int> mapNode = new MyMapNode <string, int>(splitMessage.Length); for (int i = 0; i < splitMessage.Length; i++) { mapNode.Add(splitMessage[i], i); } Console.WriteLine("Enter the word where you require to know the frequency"); string word = Console.ReadLine(); mapNode.GetFrequency(word); Console.WriteLine("Enter the word that you want to remove"); string remove = Console.ReadLine(); mapNode.Remove(remove); mapNode.Display(); Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("Welcome to Hash Table"); //Declaring MyMapNode object with data type and size of array in parametrized constructor MyMapNode <string, int> hash = new MyMapNode <string, int>(5); //Adding values in hashtable. //hash.Add("0", "To"); //hash.Add("1", "be"); //hash.Add("2", "or"); //hash.Add("3", "not"); //hash.Add("4", "To"); //hash.Add("5", "be"); //getting the specific value from hashtable. //string hash5 = hash.Get("5"); //Console.WriteLine("5th index value:" + hash5); //creating a string of long sentence string words = "Paranoids are not paranoid because they are paranoid but because they keep putting themselves deliberately into paranoid avoidable situations"; //adding values into array string[] arr = words.Split(" "); //creating a linkedlist to check for duplicates value //if duplicate value is coming inside a array, as previous element(when it was not in list) would already contain total count of same elements //it will be skipped LinkedList <string> checkForDuplication = new LinkedList <string>(); //iterating loop over the array of sentence words foreach (string element in arr) { //count iterator for counting no of times word is coming int count = 0; //another foreach loop to check if other same element is present //if present counter is incremented foreach (string match in arr) { if (element == match) { count++; //if same element is coming in 2nd time, it will be presnent in linkedlist, so loopwill break and no further counting will be done if (checkForDuplication.Contains(element)) { break; } } } //if element is already there in list, outer loop will be continued and loop will shift to next value //basically only values appearing for 1st time, will be added in linkedlist and added in hash table, so that frequency can be displayed. if (checkForDuplication.Contains(element)) { continue; } //added element in linkedlist checkForDuplication.AddLast(element); //added element and it's frequency in hashtable. hash.Add(element, count); } //getting the specific value from hashtable. int frequency = hash.Get("Paranoids"); Console.WriteLine("frequency for Paranoids:\t" + frequency); //Displaying all the elements from the linkedlist Console.WriteLine("****************"); Console.WriteLine("Displaying all the key value pairs in hash table"); hash.Display(); Console.WriteLine("**********************************************"); //removing avoidable word from the hashtable hash.Remove("avoidable"); Console.WriteLine("Word removed from hashtable"); //getting the specific value from hashtable. int removedWordFrequency = hash.Get("avoidable"); Console.WriteLine("frequency for avoidable:\t" + removedWordFrequency); //Displaying all the elements from the linkedlist Console.WriteLine("****************"); Console.WriteLine("Displaying all the key value pairs in hash table"); hash.Display(); }
static void Main(string[] args) //main method { Console.WriteLine("Hash Table"); MyMapNode <string, int> hashtable = new MyMapNode <string, int>(5); hashtable.Display(); ///*UC1*/ //MyMapNode<string, string> hashtable = new MyMapNode<string, string>(5); //create MyMapNode class object adn passing key and value and size //hashtable.Add("0", "To"); //add key and value //hashtable.Add("1", "be"); //hashtable.Add("2", "or"); //hashtable.Add("3", "not"); //hashtable.Add("4", "to"); //hashtable.Add("5", "be"); //Console.WriteLine("\nShowing Value using Key using Get Method"); //Console.WriteLine($"{hashtable.Get("3")}"); //show the specific value from hashtable. //hashtable.Remove("5"); //Call Remove method //UC3 /* UC2:- Ability to find frequency of words in a large paragraph phrase “Paranoids are not * paranoid because they are paranoid but because they keep putting themselves * deliberately into paranoid avoidable situations” * - Use hashcode to find index of the words in the para. * - Create LinkedList for each index and store the words and its frequency. * - Use LinkedList to do the Hash Table Operation. * - To do this create MyMapNode with Key Value Pair and create LinkedList of MyMapNode. */ //MyMapNode<string, int> hashtable = new MyMapNode<string, int>(5); string input = "Paranoids are are not paranoid because they are paranoid but because they \n keep putting themselves deliberately into paranoid avoidable situations\n"; Console.WriteLine($"Statement is:- {input}"); hashtable.Display(); try { string[] inputArray = input.Split(); // split input statement using Split method foreach (string word in inputArray) //itterat word in inputArray { if (hashtable.Get(word) == 0) { hashtable.Add(word, 1); //Add ing key and value } else { int value = hashtable.Get(word) + 1; //add 1 hashtable.Set(word, value); //set word and value } } string[] newInputArray = inputArray.Distinct().ToArray(); //Unique value store not reapeat value foreach (var word in newInputArray) { Console.WriteLine("Frequency of Word ccurrence :- \"" + word + "\" is :- " + hashtable.Get(word)); //print word and count } } catch (Exception ex) { Console.WriteLine(ex.Message); } /* UC3:- Remove avoidable word from the phrase “Paranoids are not paranoid because they are paranoid but * because they keep putting themselves deliberately into paranoid avoidable situations” * - Use LinkedList to do the Hash Table Operation like here the removal of word avoidable. * - To do this create MyMapNode with Key Value Pair and create LinkedList of MyMapNode. */ hashtable.Remove("avoidable"); Console.ReadLine(); }