// AddNewNameToList takes in a name, adds it to the list maintaining the order // passes back an integer (-1 if error), giving inserted location public int AddNewNameToList(string theWord) { // now locate position to insert new name int position = 0; while ((position < nextFreeLocation) && (collection[position].CompareTo(theWord) < 0)) { position = position + 1; } // now do shunting - step 2 above for (int i = nextFreeLocation; i > position; i--) { collection[i] = collection[i - 1]; } // now insert theName into position - step 3 above collection[position] = new WordCountPair(theWord, 1); // increment nextFreeLocation for next time - step 4 above nextFreeLocation++; // pass back the position we inserted theName return(position); }
public int AddNewNameToList(string theWord) { int position = 0; while ((position < nextFreeLocation) && (collection[position].CompareTo(theWord) < 0)) { position = position + 1; } for (int i = nextFreeLocation; i > position; i--) { collection[i] = collection[i - 1]; } collection[position] = new WordCountPair(theWord, 1); nextFreeLocation++; return(position); }