I can't get my code to run right at all. This is C programming. I need to also figure out where to plug in to prompt user for words and make it store them. Help please. This is the assignment guidelines:
Write a program that will do the following:
Continuously prompt the user for words and store the words in a linked list, keeping the words in alphabetical order. Do not use an array to store the words or in any way pre-allocate any memory. You can, and should, use an array when initially reading the word from the user. Use malloc() to request memory for each word after it is read and then store the node into the linked list in its proper location.
Use a function GetWord to read a single word from the user.
As each word is entered into the list, print the contents of the list using a function PrintList. If the list is empty the function should print “List is empty”. The output of the PrintList function should be on the right side of the screen as shown on the sample output.
When the user indicates that there are no more words to enter, print the final contents of the list.
Then delete the last node in the list repeatedly, until the list is empty, calling the PrintList function after each word is removed from the list.
Your program should allow for the possible storage of any number of words. Each word should allow a maximum size of 15 letters. Your program should make certain that this word length is not exceeded so that the program does not crash if longer words are entered by the user. If a longer word is entered it should be discarded.
A sample run is shown below. Your data entry prompts should be on the left side of the screen as shown. Your output should look EXACTLY like the example, except for the actual words entered.
Sample Run
List is empty
Enter a word: four
four
Do you have more words? yes or no: y
Enter a word: score
four
score
Do you have more words? yes or no: y
Enter a word: and
and
four
score
Do you have more words? yes or no: y
Enter a word: seven
and
four
score
seven
Do you have more words? yes or no: y
Enter a word: years
and
four
score
seven
years
Do you have more words? yes or no: y
Enter a word: ago
ago
and
four
score
seven
years
Do you have more words? yes or no: n
Final list is:
ago
and
four
score
seven
years
Deleting nodes one at a time
ago
and
four
score
seven
ago
and
four
score
ago
and
four
ago
and
ago
List is empty
Press any key to continue
This is the code I have so far:
Code:#include "stdafx.h" #include #include "stdlib.h" #include "ctype.h" #include "stdio.h" #define MAX_WORD 16 /*function prototype*/ void GetWord (char *word,FILE *ffptr); void PrintList (struct node *head); struct node { char word[MAX_WORD]; struct node *next; }; int main(void) { struct node *list = NULL; /*points to the first word in the list */ struct node *tmpPtr = NULL; /*used to scan through the list*/ struct node *prevPtr = NULL; /*points to previous word while scanning*/ struct node *newPtr = NULL; /*points to new node*/ char tempWord[MAX_WORD * 2]; FILE *ffptr; if((ffptr = fopen("File Data.txt", "r")) == NULL) { printf("File could not be opened\n"); return 1; } PrintList (list); GetWord(tempWord,ffptr); list = (struct node *)malloc(sizeof(struct node)); strcpy(list->word, tempWord); list->next = NULL; PrintList (list); while(!feof(ffptr)) { /* get next word and build node*/ GetWord(tempWord,ffptr); newPtr = (struct node *)malloc(sizeof(struct node)); strcpy(newPtr->word, tempWord); newPtr->next = NULL; tmpPtr = list; prevPtr = NULL; while (strcmp(newPtr->word, tmpPtr->word) > 0) { prevPtr = tmpPtr; tmpPtr = tmpPtr->next; if (tmpPtr == NULL) break; } if (tmpPtr == NULL) { prevPtr->next = tmpPtr; } else if (prevPtr == NULL) { newPtr->next = tmpPtr; list = newPtr; } else { prevPtr->next = newPtr; newPtr->next = tmpPtr; } PrintList(list); fclose(ffptr); } printf("\t\t\t\t\t\tFinal list is:\n\n"); PrintList(list); printf("\t\t\t\t\t\tDeleting nodes one a a time"); while (list !=NULL) { if (list->next ==NULL) { free (list); list = NULL; } else { tmpPtr = list; while (tmpPtr->next != NULL) { prevPtr = tmpPtr; tmpPtr = tmpPtr->next; } prevPtr->next = NULL; free (tmpPtr); } PrintList(list); } getch(); return 0; } void GetWord(char *word, FILE *ffptr) { fscanf(ffptr,"%s", word); while (strlen(word) > MAX_WORD-1 ) { fscanf(ffptr,"%s", word); } } void PrintList (struct node *head) { struct node *curPtr; printf("\n\n"); if (head == NULL) printf("\t\t\t\t\t\tList is empty"); else { curPtr = head; printf("\t\t\t\t\t\t%s\n", curPtr->word); while (curPtr->next != NULL) { curPtr = curPtr->next; printf("\t\t\t\t\t\t%s\n", curPtr->word); } } printf("\n\n"); }