This program is an exercise in using linked lists. The user will enter a number of strings and the program will display the list in alphabetical order. Running the program may display:
enter a new name (y/n)? y
name: Eleanore Roosevelt
enter a new name (y/n)? z
bad input, try again
enter a new name (y/n)? y
name: Jack the Ripper
enter a new name (y/n)? y
name: Elvis Presley
enter a new name (y/n)? n
Eleanore Roosevelt
Elvis Presley
Jack the Ripper
Your program will accept all input using the fgets()) function to allow white space in the input. This function will be discussed in class. The program will accept names from the user and insert each name into a linked list. When a new name is entered into the list it is placed in a position which maintains the list in alphabetical order -- e.g. Mata Hari would be placed between John Lennon and Superman if those names were already in the list. When the user is done entering names the list will be traversed displaying the names in list (and hence alphabetical) order.
To determine when one name (string) precedes another in alphabetical order use the following function:
int strcmpi(char *s1, char *s2)
{
while (*s1 && tolower(*s1) == tolower(*s2))
{
s1++;
s2++;
}
return tolower(*s1) - tolower(*s2);
}
This function will return a negative value if s1 precedes s2, a positive value if s2 precedes s1 and 0 if they are equal.
As seen in the example above, the program will ask the user if he or she wants to enter a new name. The program will accept new names as long as the user enters "y". If the user enters anything other than "y" or "n" the program will continue to ask until the user enters a valid response. Responses to this prompt will also be accepted using fgets().
Your program must display good programming style (to be discussed in class) and make appropriate use of functions.