Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.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.len = strlen ( string.str );
node->str.str = malloc ( node->str.len + 1 );
if ( node->str.str != NULL )
strcpy ( node->str.str, string.str );
}
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 * AddNodeIF(struct Node ** st) //this has some problems.
{
FILE *ifp;
String string;
char buff[BUFSIZ];
string.str = buff;
ifp = fopen("input.txt", "r");
do
{
fscanf(ifp, "%s", &string.str);
AddNode(&st, string);
}
while (!feof(ifp));
return st;
fclose(ifp);
}
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;
}
struct Node * RemoveNode(struct Node ** st, String string)
{
//seach for the string in the list;
//if found, remove it;
//if not found, print "string not found in list;
}
int Search(struct Node ** st, String string)
{
struct Node * next = *st;
int c = 0;
if (*st != NULL)
{
//if string in list, c = 1; return c;
//else, c = 2; return c;
}
}
struct Node * CopyList(struct Node * st)
(
//struct Node * tempst;
//then copy all nodes in st to tempst;
//print tempst;
)
struct Node * Concatinate(struct Node * st1, struct Node * st2)
{
//struct Node * tempst;
//then copy all nodes in st1;
//then copy all nodes in st2;
//print tempst;
}
void PrintList(struct Node * st)
{
struct Node * next = st;
while(next != NULL)
{
printf("%s\n", next->str.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 * st1 = NULL;
struct Node * st2 = NULL;
String string;
int choice = 0;
int listchoice = 0;
int yon = 0;
char buff[BUFSIZ];
string.str = buff;
choice = MainMenu();
do
{
if (choice == 1)
{
printf("%s", "Which list would you like to add strings to? 1 or 2?");
scanf("%d", listchoice);
if (listchoice == 1)
{
st1 = AddNodeIF();
AddNode(&st1, string);
choice = MainMenu();
}
else
{
st2 = AddNodeIF();
AddNode(&st2, string);
choice = MainMenu();
}
}
else if (choice == 2)
{
printf("%s", "Which list would you like to print? 1 or 2?");
scanf("%d", listchoice);
if (listchoice == 1)
{
printf("%s", "List 1 contains:\n");
PrintList(st1);
choice = MainMenu();
}
else
{
printf("%s", "List 2 contains:\n");
PrintList(st2);
choice = MainMenu();
}
}
else if (choice == 3)
{
printf("%s", "Which list would you like to remove a string from? 1 or 2?");
scanf("%d", listchoice);
if (listchoice == 1)
{
printf("%s", "Please enter the string you wish to remove from list 1: \n");
scanf("%s", string.str);
RemoveNode(&st1, string);
choice = MainMenu();
}
else
{
printf("%s", "Please enter the string you wish to remove from list 2: \n");
scanf("%s", string.str);
RemoveNode(&st2, string);
choice = MainMenu();
}
}
else if (choice == 4)
{
printf("%s", "Which list would you like to add a string to? 1 or 2?");
scanf("%d", listchoice);
if (listchoice == 1)
{
printf("%s", "Please enter the string you wish to add to list 1: \n");
scanf("%s", string.str);
AddNode(&st1, string);
choice = MainMenu();
}
else
{
printf("%s", "Please enter the string you wish to add to list 2: \n");
scanf("%s", string.str);
AddNode(&st2, string);
choice = MainMenu();
}
}
else if (choice == 5)
{
printf("%s", "Which list would you like to search in? 1 or 2?");
scanf("%d", listchoice);
if (listchoice == 1)
{
yon = Search(st1, string);
if (yon == 1)
{
printf("%s", "Yes, the string entered exists in list 1.\n");
}
else
{
printf("%s", "No, the string entered does not exist in list 1.\n");
}
else
{
yon = Search(st2, string);
if (yon == 1)
{
printf("%s", "Yes, the string entered exists in list 1.\n");
}
else
{
printf("%s", "No, the string entered does not exist in list 1.\n");
}
}
}
else if (choice == 6)
{
printf("%s", "Which list would you like to copy? 1 or 2?");
scanf("%d", listchoice);
if (listchoice == 1)
{
CopyList(st1);
}
else
{
CopyList(st2);
}
}
else if (choice == 7)
{
Concatinate(st1, st2);
}
else if (choice == 8)
{
printf("%s", "Which list would you like to destroy? 1 or 2?");
scanf("%d", listchoice);
if (listchoice == 1)
{
DestroyList(&st1);
printf("%s", "List 1 has been destroyed. To create another, simply chose choice 1.\n");
choice = MainMenu();
}
else
{
DestroyList(&st2);
printf("%s", "List 2 has been destroyed. To create another, simply chose choice 1.\n");
choice = MainMenu();
}
}
}
while (choice != 9);
return 0;
}
Ugh...just want to get this overwith, please help