#define ALLOW_INSTRUCTOR_CODE /*************************************************************************** *************************************************************************** ****** BEFORE BEGINNING OR TURNING IN THIS EXERCISE IT IS EXTREMELY ****** ****** IMPORTANT THAT YOU READ, FULLY UNDERSTAND, AND ADHERE TO THE ****** ****** REQUIREMENTS STATED IN THE DOCUMENT TITLED, "ASSIGNMENT ****** ****** SUBMISSION REQUIREMENTS", DOWNLOADABLE FROM THE COURSE WEB ****** ****** SITE. ****** *************************************************************************** **************************************************************************/ /*************************************************************************** ************* C/C++ Programming 2: Assignment 6 - Exercise 4 ************* ********* BEGIN EXERCISE REQUIREMENT - THIS MUST BE A "C" PROGRAM ********* *************************************************************************** (4 points) Given the structure definition: typedef struct List { struct List *next; // pointer to the next list node char *str; // pointer to a string int count; // # of times this string was encountered } LIST; write functions OpenFile, CreateList, PrintList, and FreeList: OpenFile syntax: FILE *OpenFile(const char *fileName); Parameters: fileName - a pointer to the name of the file to be opened Synopsis: Opens the file named in in the read-only text mode. If the open fails an error message is output to stderr and the program is terminated with an error exit code. Return: a pointer to the open file if the open succeeds; otherwise, the function does not return. CreateList syntax: LIST *CreateList(FILE *fp); Parameter: fp - a pointer to an open text file containing zero or more whitespace-separated English words (called strings) Synopsis: Reads strings from the text file in and creates a new singly-linked list in which each node represents a unique case-dependent string found in the file. As each string is read the current list is searched for a node that may already exist for that string. If one is found, its string occurrence count is merely incremented. If none is found, a new node is created at the head of the list (pushed) to represent the new string, and its occurrence count is set to 1. Reading of the file stops when EOF is reached. Return: the list's head pointer. Examples: A list with 9 nodes will be created if the file contains: Is it the day the earth stood still? It is! but a list with 11 nodes will be created if the file contains: Is it the day the earth stood still ? It is ! In both cases the count in each node will be 1 except for the "the" node, which will contain a count of 2. Note that "Is" differs from "is" and "it" differs from "It" due to case, and each will get a separate node. PrintList syntax: LIST *PrintList(const LIST *head); Parameter: head - the head pointer to the previously-described list Synopsis: Displays the data attributes of the list whose head pointer is passed to it. The display strings and occurrence counts must be aligned for easy readability. Return: parameter Example: the 107 ea White 25 ea White? 4 ea if 16 ea etc... FreeList syntax: void FreeList(LIST *head); Parameter: head - the head pointer to the previously-described list Synopsis: Frees all dynamic allocations in the list. Return: void * Test the program on input file "TestFile1.txt", downloaded from the course web site. * Do not attempt to read the entire file into your program at once. * Do not sort the list; merely push each new node onto the top. * If you use the SafeMalloc function, show its code. Hint: * Place input test file "TestFile1.txt" in the appropriate default location described in the "FILE LOCATION QUESTIONS AND ANSWERS" section of the "ASSIGNMENT SUBMISSION REQUIREMENTS" document. *************************************************************************** ************************ END EXERCISE REQUIREMENT ************************* **************************************************************************/ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING ABOVE IT. **************************************************************************/ /* * TODO: * REPLACE THIS COMMENT WITH YOUR TITLE BLOCK. */ /* * TODO: * REPLACE THIS COMMENT WITH YOUR C CODE, PLUS ANYTHING NECESSARY TO * SUPPORT MY TEST CODE BELOW. DO NOT INCLUDE ANY UNNEEDED HEADER FILES! */ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING BELOW IT. **************************************************************************/ /* ***** Change nothing below this comment without instructor permission. **** * Everything that follows was written to help test/verify your code. You * do not need to understand the details of my code to write yours! */ #define FILENAME "TestFile1.txt" #ifdef ALLOW_INSTRUCTOR_CODE int main(void) { FILE *fp = OpenFile(FILENAME); LIST *head = CreateList(fp); fclose(fp); FreeList(PrintList(head)); return EXIT_SUCCESS; } #endif