Ok, here is the beast...
Code:
#include <stdio.h>
typedef struct
{
char *str;
int len;
} String;
struct Node
{
struct Node * next;
String str;
};
struct Node * AllocNode(String string)
{
struct Node * node = (struct Node *)malloc(sizeof(struct Node));
if(node != NULL)
{
node->next = 0;
node->str = string;
}
return node;
}
void DestroyList(struct Node ** st)
{
struct Node * next, * prev = *st;
while(prev != NULL)
{
next = prev->next;
free(prev);
prev = next;
}
*st = 0;
}
struct Node * AddNode(struct Node ** st, String string)
{
struct Node * next = *st;
struct Node * node = AllocNode(string);
if(*st != NULL)
{
while(next->next != NULL)
{
next = next->next;
}
next->next = node;
}
else
{
*st = node;
}
return node;
}
void PrintList(struct Node * st)
{
struct Node * next = st;
while(next != NULL)
{
printf("%c\n", next->str);
next = next->next;
}
}
int MainMenu(void)
{
int c = 0;
printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
"-MAIN MENU-",
"1. Initialize the list by reading in strings from the input and adding one by one to the linked list.",
"2. Print all the strings in the list.",
"3. Remove a string from a list (interactively).",
"4. Add a string to the list (interactively).",
"5. Search for a string in the list.",
"6. Make a deep copy of the list.",
"7. Concatenate two lists.",
"8. Destory the list. A new list can be created by selecting 1.",
"9. Quit.\n");
scanf("%d", &c);
return c;
}
int main()
{
struct Node * st = NULL;
String string;
int choice = 0;
char yon = 'y';
choice = MainMenu();
do
{
if (choice == 1)
{
printf("%s", "Please enter a string: \n");
scanf("%s", &string);
AddNode(&st, string);
do
{
printf("%s", "Please enter a string: \n");
scanf("%s", &string);
AddNode(&st, string);
printf("%s", "Would you like to enter another string? y or n?\n");
scanf("%c", &yon);
}
while (yon != 'n');
choice = MainMenu();
}
else if (choice == 2)
{
printf("%s", "Your list contains:\n");
PrintList(st);
choice = MainMenu();
}
else if (choice == 3) {printf("\n%s", "You picked 3.\n"); }
else if (choice == 4)
{
printf("%s", "Please enter a string: \n");
scanf("%s", &string);
AddNode(&st, string);
choice = MainMenu();
}
else if (choice == 5) {printf("\n%s", "You picked 5.\n"); }
else if (choice == 6) {printf("\n%s", "You picked 6.\n"); }
else if (choice == 7) {printf("\n%s", "You picked 7.\n"); }
else if (choice == 8)
{
DestroyList(&st);
printf("%s", "Your list has been destroyed. To create another, simply chose choice 1.\n");
choice = MainMenu();
}
}
while (choice != 9);
return 0;
}