/// <summary> /// Counts the number of names that started with the first letter retrieved from the text box named uxFirstLetter /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxCount_Click(object sender, EventArgs e) { LinkedListCell <NameInformation>[] names = new LinkedListCell <NameInformation> [26]; _names.CopyTo(names, 0); try { string firstLetterString = uxFirstLetter.Text.Trim().ToUpper(); char firstLetter = firstLetterString[0]; int count = 0; int index = firstLetter - 'A'; if (names[index] == null) { uxCountTextBox.Text = "0"; } while (names[index] != null) //checks how many letters start with this letter { names[index] = names[index].Next; count++; } string txt = count.ToString() + " names begin with " + firstLetterString; uxCountTextBox.Text = txt; //updates the uxCountTextBox } catch (Exception ex) { MessageBox.Show("Incorrect file"); } }
/// <summary> /// Handles a Click event on the Open Data File button and places all the data by the first name of the last name /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxOpen_Click(object sender, EventArgs e) { if (uxOpenDialog.ShowDialog() == DialogResult.OK) { LinkedListCell <NameInformation> tempCell = new LinkedListCell <NameInformation>(); LinkedListCell <NameInformation>[] arrayOfNames = new LinkedListCell <NameInformation> [26];//each index is a linkedlist corresponding to the letter in alphabet try { string fn = uxOpenDialog.FileName; tempCell = GetAllInformation(fn); while (tempCell != null) { char letter = tempCell.Data.Name[0]; int index = letter - 'A'; arrayOfNames[index] = Insert(tempCell.Data, arrayOfNames[index]); tempCell = tempCell.Next; } _names = arrayOfNames; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }
/// <summary> /// Creates a new cell and adds it to the array /// </summary> /// <param name="k"> Key value for the new cell </param> /// <param name="v"> Value parameter for the cell </param> /// <param name="loc"> Index in the array we are adding this cell </param> private void Insert(TKey k, TValue v, int loc) { LinkedListCell <KeyValuePair <TKey, TValue> > newCell = new LinkedListCell <KeyValuePair <TKey, TValue> >(); newCell.Data = new KeyValuePair <TKey, TValue>(k, v); Insert(newCell, loc); }
/// <summary> /// Inserts the given key and value into the beginning of the linked list at /// the given table location. /// </summary> /// <param name="k">The key.</param> /// <param name="v">The value associated with k.</param> /// <param name="loc">The table location at which k and v are to be inserted.</param> private void Insert(TKey k, TValue v, int loc) { LinkedListCell <KeyValuePair <TKey, TValue> > cell = new LinkedListCell <KeyValuePair <TKey, TValue> >(); cell.Data = new KeyValuePair <TKey, TValue>(k, v); Insert(cell, loc); Count++; if (Count > _elements.Length) { // Check if (_currentIndex < _tableSizes.Length - 1) { LinkedListCell <KeyValuePair <TKey, TValue> >[] temp = _elements; _currentIndex++; int newSize = _tableSizes[_currentIndex]; _elements = new LinkedListCell <KeyValuePair <TKey, TValue> > [newSize]; for (int i = 0; i < temp.Length; i++) { while (temp[i] != null) { LinkedListCell <KeyValuePair <TKey, TValue> > front = temp[i]; temp[i] = temp[i].Next; int newLocation = GetLocation(front.Data.Key); Insert(front, newLocation); } } } } }
/// <summary> /// insets given cell into LL /// </summary> /// <param name="cell"></param> /// <param name="loc"></param> private void Insert(LinkedListCell <KeyValuePair <TKey, TValue> > cell, int loc) { LinkedListCell <KeyValuePair <TKey, TValue> > temp = GetCell(cell.Data.Key, cell); loc = GetLocation(temp.Data.Key); Insert(temp, loc); }
/// <summary> /// Public Indexer for the dictionary! /// </summary> /// <param name="k"> Key that is used in the get/set operations </param> /// <returns> The value associated with the key! </returns> public TValue this[TKey k] { get { TValue outValue; bool result = TryGetValue(k, out outValue); if (result == false) { throw new KeyNotFoundException(); } else { return(outValue); } } set { CheckKey(k); int location = GetLocation(k); LinkedListCell <KeyValuePair <TKey, TValue> > cell = GetCell(k, _elements[location]); if (cell == null) { // check Insert(k, value, location); } else { cell.Data = new KeyValuePair <TKey, TValue>(k, value); } } }
/// <summary> /// Finds the most common letter for the start of a name in the entire array /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxFindCommonLetter_Click(object sender, EventArgs e) { try { int[] letterAmount = new int[_names.Length]; for (int i = 0; i < _names.Length; i++) { LinkedListCell <NameInformation> temp = _names[i]; int count = 0; while (temp != null) { count++; temp = temp.Next; } letterAmount[i] = count; } int maxNumber = letterAmount.Max(); int letterPosition = Array.IndexOf(letterAmount, maxNumber); //find position of max number, now I need to convert back to a char char maxLetter = (char)(letterPosition + 'A'); uxCommonLetterResult.Text = "Most frequent first letter: " + char.ToUpper(maxLetter); } catch (Exception ex) { if (MessageBox.Show("Must read a file in first, press 'OK' for a more detailed exception.", "Warning", MessageBoxButtons.OKCancel) == DialogResult.OK) { MessageBox.Show(ex.ToString()); } } }
/// <summary> /// Output all data in sorted order by name /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// private void uxOutput_Click(object sender, EventArgs e) { LinkedListCell <NameInformation>[] names = new LinkedListCell <NameInformation> [26]; _names.CopyTo(names, 0); if (uxSaveFileDialog.ShowDialog() == DialogResult.OK) { string fn = uxSaveFileDialog.FileName; using (StreamWriter output = new StreamWriter(fn)) { try { for (int j = 0; j <= 25; j++) { while (names[j] != null) { string name = names[j].Data.Name.ToString(); string frequency = names[j].Data.Frequency.ToString(); string rank = names[j].Data.Rank.ToString(); output.WriteLine(name); output.WriteLine(frequency); output.WriteLine(rank); names[j] = names[j].Next; } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } }
/// <summary> /// This method inserts a new cell containing that NameInformation in sorted order by name in the given list. /// It returns a reference to the first cell in the list. /// </summary> /// <param name="nameInfo">name information that is needed to be insert</param> /// <param name="firstCell">a reference to the first cell in a linked list</param> /// <returns></returns> private LinkedListCell <NameInformation> Insert(NameInformation nameInfo, LinkedListCell <NameInformation> firstCell) { LinkedListCell <NameInformation> cell = new LinkedListCell <NameInformation>(); cell.Data = nameInfo; if (firstCell == null) { cell.Data = nameInfo; return(cell); } else if (nameInfo.Name.CompareTo(firstCell.Data.Name) < 0) { cell.Next = firstCell; firstCell = cell; return(firstCell); } LinkedListCell <NameInformation> tempCell = firstCell; while (tempCell.Next != null) { if (nameInfo.Name.CompareTo(tempCell.Next.Data.Name) > 0) { tempCell = tempCell.Next; } else { cell.Next = tempCell.Next; tempCell.Next = cell; return(firstCell); } } tempCell.Next = cell; return(firstCell); }
/// <summary> /// Gets the cell containing the given key in the given linked list. /// If the linked list doesn't contain such a cell, returns null. /// </summary> /// <param name="k">The key to look for.</param> /// <param name="list">The linked list to look in.</param> /// <returns>The cell containing k, or null if no such cell exists.</returns> private LinkedListCell <KeyValuePair <TKey, TValue> > GetCell(TKey k, LinkedListCell <KeyValuePair <TKey, TValue> > list) { while (list != null && !k.Equals(list.Data.Key)) { list = list.Next; } return(list); }
/// <summary> /// Finds the last cell in _elements whose key is less than the given key, or the /// header cell if no keys are less than the given key. /// </summary> /// <param name="key">The key.</param> /// <returns>The last cell whose key is less than the given key.</returns> private LinkedListCell <KeyValuePair <TKey, TValue> > FindLastLess(TKey key) { LinkedListCell <KeyValuePair <TKey, TValue> > p = _elements; while (p.Next != null && p.Next.Data.Key.CompareTo(key) < 0) { p = p.Next; } return(p); }
/// <summary> /// /// </summary> /// <param name="k"></param> /// <param name="v"></param> /// <param name="loc"></param> private void Insert(TKey k, TValue v, int loc) { //Insert(k, v, loc); LinkedListCell <KeyValuePair <TKey, TValue> > temp = new LinkedListCell <KeyValuePair <TKey, TValue> >(); k = temp.Data.Key; v = temp.Data.Value; Insert(k, v, loc); }
/// <summary> /// Gets the cell containing the given key in the given linked list, or null if no cell in the list contains the key. /// </summary> /// <param name="k">The key to look for.</param> /// <param name="list">The linked list to search.</param> /// <returns>The cell containing k, or null if there is no such cell in the list.</returns> private LinkedListCell <KeyValuePair <TKey, TValue> > GetCell(TKey k, LinkedListCell <KeyValuePair <TKey, TValue> > list) { for (LinkedListCell <KeyValuePair <TKey, TValue> > p = list; p != null; p = p.Next) { if (k.Equals(p.Data.Key)) { return(p); } } return(null); }
/// <summary> /// Handles a Click event on the Open Data File button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxOpen_Click(object sender, EventArgs e) { try { uxOpenDialog.ShowDialog(); _listName = parseFileToList(uxOpenDialog.FileName); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }
/// <summary> /// Enumerator that we actually use to iterate through _elements and yield return the KVP's! /// </summary> /// <returns> KVP's of _elements </returns> public IEnumerator <KeyValuePair <TKey, TValue> > GetEnumerator() { for (int i = 0; i < _elements.Length; i++) { LinkedListCell <KeyValuePair <TKey, TValue> > temp = _elements[i]; while (temp != null) { yield return(temp.Data); temp = temp.Next; } } }
/// <summary> /// Opens a file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxOpen_Click(object sender, EventArgs e) { if (uxOpenDialog.ShowDialog() == DialogResult.OK) { try { _info = ReadIn(uxOpenDialog.FileName); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } }
/// <summary> /// Reads the given file and forms a linked list from its contents. /// </summary> /// <param name="fn">The name of the file.</param> /// <returns>The linked list containing all the name information.</returns> private void GetAllInformation(string fn) { using (StreamReader input = new StreamReader(fn)) { while (!input.EndOfStream) { string name = input.ReadLine().Trim(); float freq = Convert.ToSingle(input.ReadLine()); int rank = Convert.ToInt32(input.ReadLine()); LinkedListCell <NameInformation> newCell = new LinkedListCell <NameInformation>(); newCell.Data = new NameInformation(name, freq, rank); int index = getIndexPosition(newCell.Data.Name[0]); LinkedListCell <NameInformation> currentCell = _names[index]; if (_names[index] == null) { _names[index] = newCell; continue; } if (newCell.Data.Name.CompareTo(currentCell.Data.Name) < 0) { newCell.Next = _names[index]; _names[index] = newCell; continue; } while (currentCell.Next != null) { if (newCell.Data.Name.CompareTo(currentCell.Data.Name) > 0 && newCell.Data.Name.CompareTo(currentCell.Next.Data.Name) < 0) { newCell.Next = currentCell.Next; currentCell.Next = newCell; break; } else { currentCell = currentCell.Next; } } if (currentCell.Next == null) { currentCell.Next = newCell; } } } }
/// <summary> /// Associates the given value with the given key. If k is null, throws an ArgumentNullException. /// If k is already in the table, throws an ArgumentException. /// </summary> /// <param name="k">The key to add.</param> /// <param name="v">The value to associate with k.</param> public void Add(TKey k, TValue v) { CheckKey(k); int loc = GetLocation(k); LinkedListCell <KeyValuePair <TKey, TValue> > cell = GetCell(k, _table[loc]); if (cell == null) { Insert(k, v, loc); } else { throw new ArgumentException(); } }
/// <summary> /// Opens file /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxOpen_Click(object sender, EventArgs e) { if (uxOpenDialog.ShowDialog() == DialogResult.OK) { try { string fn = uxOpenDialog.FileName; _nameInfo = ReadFile(fn); } catch (Exception ex) { MessageBox.Show("The following error occured: " + ex.ToString()); } } }
/// <summary> /// Tries to get the value associated with the given key. /// </summary> /// <param name="k">The key.</param> /// <param name="v">The value associated with k, or the default value if /// k is not in the dictionary.</param> /// <returns>Whether k was found as a key in the dictionary.</returns> public bool TryGetValue(TKey k, out TValue v) { CheckKey(k); LinkedListCell <KeyValuePair <TKey, TValue> > p = FindLastLess(k); if (p.Next == null || !p.Next.Data.Key.Equals(k)) { v = default(TValue); return(false); } else { v = p.Next.Data.Value; return(true); } }
/// <summary> /// Tries to get the value associated with the given key. /// </summary> /// <param name="k">The key to look for.</param> /// <param name="v">The value associated with k, or the default value if k is not in the dictionary.</param> /// <returns>Whether k was found.</returns> public bool TryGetValue(TKey k, out TValue v) { CheckKey(k); LinkedListCell <KeyValuePair <TKey, TValue> > cell = GetCell(k, _table[GetLocation(k)]); if (cell == null) { v = default(TValue); return(false); } else { v = cell.Data.Value; return(true); } }
/// <summary> /// Searches for name matches /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxLookup_Click(object sender, EventArgs e) { LinkedListCell <NameInformation> temp = _info; string Name = uxName.Text.Trim().ToUpper(); while (temp != null) { if (temp.Data.Name == Name) { uxFrequency.Text = temp.Data.Frequency.ToString(); uxRank.Text = temp.Data.Rank.ToString(); return; } temp = temp.Next; } MessageBox.Show("Name not found"); }
/// <summary> /// Handles a Click event on the Get Statistics button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxLookup_Click(object sender, EventArgs e) { string name = uxName.Text.Trim().ToUpper(); for (LinkedListCell <NameInformation> p = _names; p != null; p = p.Next) { if (p.Data.Name == name) { uxFrequency.Text = p.Data.Frequency.ToString(); uxRank.Text = p.Data.Rank.ToString(); return; } } MessageBox.Show("Name not found."); uxFrequency.Text = ""; uxRank.Text = ""; }
/// <summary> /// Adds the given key with the given associated value. /// If the given key is already in the dictionary, throws an /// InvalidOperationException. /// </summary> /// <param name="k">The key.</param> /// <param name="v">The value.</param> public void Add(TKey k, TValue v) { CheckKey(k); LinkedListCell <KeyValuePair <TKey, TValue> > p = FindLastLess(k); if (p.Next == null || !p.Next.Data.Key.Equals(k)) { LinkedListCell <KeyValuePair <TKey, TValue> > cell = new LinkedListCell <KeyValuePair <TKey, TValue> >(); cell.Data = new KeyValuePair <TKey, TValue>(k, v); cell.Next = p.Next; p.Next = cell; } else { throw new ArgumentException(); } }
/// <summary> /// Tries to get the value associated with the given key. /// </summary> /// <param name="k">The key.</param> /// <param name="v">The value associated with k, or the default value if /// k is not in the dictionary.</param> /// <returns>Whether k was found as a key in the dictionary.</returns> public bool TryGetValue(TKey k, out TValue v) { CheckKey(k); int loc = GetLocation(k); LinkedListCell <KeyValuePair <TKey, TValue> > newCell = GetCell(k, _elements[loc]); if (newCell == null) { v = default(TValue); return(false); } else { v = newCell.Data.Value; return(true); } }
/// <summary> /// Steps through a linked list, trying to find the designated key. If not found, returns null /// </summary> /// <param name="k"> Key we are looking for in list </param> /// <param name="list"> Linked List we are iterating through </param> /// <returns></returns> private LinkedListCell <KeyValuePair <TKey, TValue> > GetCell(TKey k, LinkedListCell <KeyValuePair <TKey, TValue> > list) { LinkedListCell <KeyValuePair <TKey, TValue> > temp = list; while (temp != null) { if (k.Equals(temp.Data.Key)) { return(temp); } else { temp = temp.Next; } } return(null); }
/// <summary> /// Advances this enumerator to the next position. /// </summary> /// <returns>Whether this brings the enumerator to a position prior to the end.</returns> public bool MoveNext() { if (_currentCell == null) { return(false); } _currentCell = _currentCell.Next; while (_currentCell == null) { _currentIndex++; if (_currentIndex == _table.Length) { return(false); } _currentCell = _table[_currentIndex]; } return(true); }
/// <summary> /// This method Reads all lines of a file, seperates the lines out into /// "name", "freq", "rank" and stores them into the LinkedListCell returned /// </summary> /// <param name="fn"></param> /// <returns></returns> private LinkedListCell <NameInformation> ReadFile(string fn) { using (StreamReader sr = new StreamReader(fn)) { LinkedListCell <NameInformation> nameInfo = null; while (!sr.EndOfStream) { string name = sr.ReadLine().Trim(); float freq = Convert.ToSingle(sr.ReadLine()); int rank = Convert.ToInt32(sr.ReadLine()); LinkedListCell <NameInformation> tempCell = new LinkedListCell <NameInformation>(); tempCell.Data = new NameInformation(name, freq, rank); tempCell.Next = nameInfo; nameInfo = tempCell; }//end while return(nameInfo); } }
/// <summary> /// Reads the given file and forms a linked list from its contents. /// </summary> /// <param name="fn">The name of the file.</param> /// <returns>The linked list containing all the name information.</returns> private LinkedListCell <NameInformation> GetAllInformation(string fn) { LinkedListCell <NameInformation> names = null; using (StreamReader input = new StreamReader(fn)) { while (!input.EndOfStream) { string name = input.ReadLine().Trim(); float freq = Convert.ToSingle(input.ReadLine()); int rank = Convert.ToInt32(input.ReadLine()); LinkedListCell <NameInformation> cell = new LinkedListCell <NameInformation>(); cell.Data = new NameInformation(name, freq, rank); cell.Next = names; names = cell; } } return(names); }
/// <summary> /// Handles a Click event on the Get Statistics button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void uxLookup_Click(object sender, EventArgs e) { bool found = false; string name = uxName.Text.Trim(); name = name.ToUpper(); LinkedListCell <NameInformation> temp = _listName; try { do { if (temp.Data.Name == name) { found = true; } else { temp = temp.Next; } } while (!found || temp == null); //MessageBox.Show("DONE WITH LOOP"); if (temp != null) { uxFrequency.Text = temp.Data.Frequency.ToString(); uxRank.Text = temp.Data.Rank.ToString(); } else { MessageBox.Show("Name not found."); uxFrequency.Text = ""; uxRank.Text = ""; } } catch (Exception ex) { MessageBox.Show("Name not found. - Exception"); uxFrequency.Text = ""; uxRank.Text = ""; } }