static void updateRankInList(ref LinkedList <WordStructre> list) { int Rank = 1; LinkedListNode <WordStructre> currentNode = list.First; while (currentNode != null) { WordStructre node = currentNode.Value; node.Rank = Rank++; currentNode.Value = node; currentNode = currentNode.Next; } }
//static string input = @"Devise and implement, in the programming language of your choice, an efficient algorithm that reports a requested //number of top, i.e. 100, smallest value value distinct words language and their total occurrence across a given number of lists //containing words ordered as per Exercise "; //static string input = "Bbn Bcn Ben Bdn Bfn Ban Ban aaa aaaa"; //static string input = " z y y q l t b a t r w q l k j h g f d s a z x c v b n m p o i u y t r e w q 1 c j f e aa ad ab aaa a "; //static string input = "hi hello"; //static string input = "I’am Iam iam, you’re Youre,"; //static string input = "boat born"; // static string input = "car cat"; // static string input = "boat born"; static void Main(string[] args) { string filePath = string.Empty; int printCount = 10; if (args.Length == 0) { Console.Write("\n Enter input file path "); filePath = Console.ReadLine(); Console.Clear(); Console.WriteLine("This number is only using for display purpose. Full list get serialize in the bin file for future Use in Exercise 2 "); Console.WriteLine("Enter Required Number "); int.TryParse(Console.ReadLine(), out printCount); } if (args.Length > 0) { filePath = args[0]; if (args.Count() > 1) { int.TryParse(args[1], out printCount); } } if (!IsInputFileIsValid(filePath)) { Console.Write("\n invalid File Pathe. Press Any key to Exit "); Console.ReadLine(); return; } input = File.ReadAllText(filePath); string[] inputArray = input.Split(new char[0], StringSplitOptions.RemoveEmptyEntries); LinkedList <WordStructre> waitedList = new LinkedList <WordStructre>(); foreach (String st in inputArray) { String word = st; char[] array = word.ToArray(); RemoveUnWantedCharBetweenWord_ToLower(ref word); // If only number in the word. No need to count, if (word == "") { continue; } #region Adding First Word into the list if (waitedList.Count == 0) { waitedList.AddLast(new WordStructre { word = word, Occurrence = 1 }); continue; } #endregion else { LinkedListNode <WordStructre> currentNode = waitedList.First; while (currentNode != null) { bool loopBreak = false; #region only case when we add less level word to the list currentNode.Value.word.Length > word.Length if (currentNode.Value.word.Length > word.Length) { waitedList.AddBefore(currentNode, new WordStructre { word = word, Occurrence = 1 }); loopBreak = true; break; } #endregion #region If the same level word if (currentNode.Value.word.Length == word.Length) { // If mor than one Occurrence of a same word if (CompareWord.Equal == StrcmpNext(currentNode.Value.word, word)) { WordStructre updatedNode = currentNode.Value; updatedNode.Occurrence += 1; currentNode.Value = updatedNode; loopBreak = true; break; } //if new word is Small we can simply add new in the begin Since it a sorted list if (CompareWord.NewWordSmallThanCurrentNodeAddBefore == StrcmpNext(currentNode.Value.word, word)) { { waitedList.AddBefore(currentNode, new WordStructre { word = word, Occurrence = 1 }); loopBreak = true; } } //Add New is Large so add after the current node else { if (currentNode.Next != null) { //Check for the next node in the list,add if next node word is small the word if (CompareWord.NewWordSmallThanCurrentNodeAddBefore == StrcmpNext(currentNode.Next.Value.word, word)) { waitedList.AddAfter(currentNode, new WordStructre { word = word, Occurrence = 1 }); loopBreak = true; } // Add next level word for frist time else if (CompareWord.CountLargerThan == StrcmpNext(currentNode.Next.Value.word, word)) { waitedList.AddAfter(currentNode, new WordStructre { word = word, Occurrence = 1 }); loopBreak = true; } } else { waitedList.AddAfter(currentNode, new WordStructre { word = word, Occurrence = 1 }); loopBreak = true; } } } #endregion #region Add next level word if current node next is null else { if (currentNode.Next == null) { waitedList.AddAfter(currentNode, new WordStructre { word = word, Occurrence = 1 }); loopBreak = true; } } #endregion if (loopBreak) { break; } currentNode = currentNode.Next; //Reach the requested Rank } } } updateRankInList(ref waitedList); SaveResultInTextFile(waitedList, printCount, Path.GetFileNameWithoutExtension(filePath)); serializerLinkedList(waitedList, Path.GetFileNameWithoutExtension(filePath)); //PrintResult(waitedList, printCount); Console.ReadLine(); }