/// ------------------------------------------------------------------- /// <summary> /// This method read the data from a passed StreamReader and writes the /// the data by dataset sections. /// </summary> /// <param name="file"> /// A StreamReader with dataSets of phone numbers /// </param> /// ------------------------------------------------------------------- private static void HandleData(StreamReader file) { // A link list that stores phone numbers. var phoneList = new PhoneNumberList(); // A phone data type to hold the data from the phoneList. PhoneData phone; // Stores the number of datasets from the file. int numberOFDataSets; // Stores the number of phone number in a data set. int numberOfPhoneNumbers; if (int.TryParse(file.ReadLine(), out numberOFDataSets)) { file.ReadLine(); for (int i = 0; i < numberOFDataSets; i++) { Console.WriteLine($"----- Dataset {i + 1} ----- \n"); if (int.TryParse(file.ReadLine(), out numberOfPhoneNumbers)) { for (int j = 0; j < numberOfPhoneNumbers; j++) { phone = new PhoneData(PhoneLettersToNumbers. FormatToPhoneNumber(file.ReadLine())); phoneList.InsertByNumber(phone); } file.ReadLine(); phoneList.DisplayPhoneDups(); if (phoneList.HasDups()) { Console.WriteLine("\nDisplayed by count:\n"); phoneList.DisplayByCount(); } else { Console.WriteLine($"No duplicates."); } phoneList = new PhoneNumberList(); Console.WriteLine(); } } } }
/// ------------------------------------------------------------------- /// <summary> /// This method is used by the InsertByNumber method to find the /// position to insert a PhoneData object. /// </summary> /// <param name="current"> The current node to be evaluated.</param> /// <param name="previous">The node before the current node.</param> /// <param name="phone"> PhoneData object to be compared to the current /// node.</param> /// ------------------------------------------------------------------- private void FindInsertPosition(ref ListNode current, ref ListNode previous, ref PhoneData phone) { // A flag if the location to insert the node is found before // getting to the end of the link list. var found = false; current = firstNode; while (current != null && !found) { // Holds information from the Data variable in a ListNode when // converted to a PhoneData from comparisons. var currentData = current.Data as PhoneData; if (currentData.ToString().CompareTo(phone.ToString()) >= 0) { found = true; } else { previous = current; current = current.Next; } } }
/// ------------------------------------------------------------------- /// <summary> /// This inserts a PhoneData object into the link list. /// </summary> /// <remarks> /// When this method is only used to insert a PhoneData objects. This /// link list is become an ordered link list by phone number in ascending /// lexicographical order. If a phone number is already in the list then /// the number is not stored in the list instead the count in the /// associated phone number is incremented. /// </remarks> /// <param name="phone">The PhoneData object to be inserted.</param> /// ------------------------------------------------------------------- public void InsertByNumber(PhoneData phone) { // An iterator to track the current position in the link list. // Needed to give the iterator a value to stop a compiler error. ListNode current = new ListNode(""); // An iterator to track the position before the current iterator in // the link list. Needed to give the iterator a value to stop a // compiler error. ListNode previous = new ListNode(""); // The new node to insert into the list. ListNode newNode = new ListNode(phone); if (IsEmpty()) { InsertAtFront(phone); } else { FindInsertPosition(ref current, ref previous, ref phone); InsertData(ref current, ref previous, ref newNode); } }