Question: #include #include #include #include typedef struct { short year; char picture[42]; char director[42]; } PICTURE; void printInstr(void); LIST* buildList(void); void process(LIST* list); char getChoice(void); void

 #include #include #include #include typedef struct { short year; char picture[42];

#include #include #include #include

typedef struct { short year; char picture[42]; char director[42]; } PICTURE;

void printInstr(void); LIST* buildList(void); void process(LIST* list); char getChoice(void); void printList(LIST* list); void search(LIST* list); int cmpYear(void* pYear1, void* pYear2);

int main() { // Local Definitions LIST* list; // Statements printInstr(); list = buildList(); process(list); printf("End Best Pictures Hope you found your favorite! "); return 0; }

void printInstr(void) { //Statements printf("This program prints the Academy Awards Best Picture of the Year and its director. Your job is to enter the year; we will do the rest. Enjoy. "); return; } // printInstr

LIST* buildList(void) { //Local Definitions FILE* fpData; LIST* list; short yearIn; int addResult; PICTURE* pPic; char p[42], d[42]; //Statements list = createList(cmpYear); if (!list) printf("\aCannot create list "), exit(100); fpData = fopen("pictures.txt", "r"); if (!fpData) printf("\aError opening input file "), exit(110); while (fscanf(fpData, " %hd", &yearIn) == 1) { pPic = (PICTURE*)malloc(sizeof(PICTURE)); if (!(pPic)) printf("\aOut of Memory in build list "), exit(100); pPic->year = yearIn; // Skip tabs and quote while ((fgetc(fpData)) != '"'); fscanf(fpData, "%40[^\"]%*c", p); while ((fgetc(fpData)) != '"'); fscanf(fpData, "%40[^\"]%*c", d); strcpy(pPic->picture, p); strcpy(pPic->director, d); // Insert into list addResult = addNode(list, pPic); if (addResult != 0) if (addResult == -1) printf("Memory overflow adding movie\a "), exit(120); else printf("Duplicate year %hd not added \a",pPic->year); while (fgetc(fpData) != ' '); } // while return list; } // buildList

void process(LIST* list) { //Local Definitions char choice; //Statements do { choice = getChoice(); switch (choice) { case 'I': case 'D': case 'P': printList(list); break; case 'S': search(list); case 'Q': break; } // switch } while (choice != 'Q'); return; } // process

char getChoice(void) { //Local Definitions char choice; bool valid; //Statements printf("======== MENU ======= " "Here are your choices: " " S: Search for a year " " P: Print all years " " Q: Quit " "Enter your choice: "); do { scanf(" %c", &choice); choice = toupper(choice); switch (choice) { case 'S': case 'I': case 'D': case 'P': case 'Q': valid = true; break; default: valid = false; printf("\aInvalid choice " "Please try again: "); break; } // switch } while (!valid); return choice; } // getChoice

void printList(LIST* list) { //Local Definitions PICTURE* pPic; //Statements // Get first node if (listCount(list) == 0) printf("Sorry, nothing in list \a"); else { printf(" Best Pictures List "); traverse(list, 0, (void**)&pPic); do { printf("%hd %-40s %s ",pPic->year,pPic->picture,pPic->director); } while (traverse(list, 1, (void**)&pPic)); } // else printf("End of Best Pictures List "); } // printList

void search(LIST* list) { //Local Definitions short year; bool found; PICTURE pSrchArgu; PICTURE* pPic; //Statements printf("Enter a four digit year: "); scanf("%hd", &year); pSrchArgu.year = year; found = searchList(list, &pSrchArgu, (void**)&pPic); if (found) printf("%hd %-40s %s ", pPic->year, pPic->picture, pPic->director); else printf("Sorry, but %d is not available. ", year); return; } // search

int cmpYear(void* pYear1, void* pYear2) { //Local Definitions int result; short year1; short year2; year1 = ((PICTURE*)pYear1)->year; year2 = ((PICTURE*)pYear2)->year; if (year1 year2) result = +1; else result = 0; return result; } // cmpYear typedef struct node { void* dataPtr; struct node* link; } NODE; typedef struct { int count; NODE* pos; NODE* head; NODE* rear; int(*compare) (void* argu1, void* argu2); } LIST;

LIST* createList (int(*compare) (void* argu1, void* argu2)) { //Local Definitions LIST* list; //Statements list = (LIST*)malloc(sizeof(LIST)); if (list) { list->head = NULL; list->pos = NULL; list->rear = NULL; list->count = 0; list->compare = compare; } // if return list; } // createList

int addNode(LIST* pList, void* dataInPtr) { //Local Definitions bool found; bool success; NODE* pPre; NODE* pLoc; //Statements found = _search(pList, &pPre, &pLoc, dataInPtr); if (found) // Duplicate keys not allowed return (+1); success = _insert(pList, pPre, dataInPtr); if (!success) // Overflow return (-1); return (0); } // addNode

static bool _insert(LIST* pList, NODE* pPre, void* dataInPtr) { //Local Definitions NODE* pNew; //Statements if (!(pNew = (NODE*)malloc(sizeof(NODE)))) return false; pNew->dataPtr = dataInPtr; pNew->link = NULL; if (pPre == NULL) { // Adding before first node or to empty list. pNew->link = pList->head; pList->head = pNew; if (pList->count == 0) // Adding to empty list. Set rear pList->rear = pNew; } // if pPre else { // Adding in middle or at end pNew->link = pPre->link; pPre->link = pNew; // Now check for add at end of list if(pNew->link == NULL) pList->rear = pNew; } // if else (pList->count)++; return true; } // _insert

bool removeNode (LIST* pList, void* keyPtr, void** dataOutPtr) { //Local Definitions bool found; NODE* pPre; NODE* pLoc; //Statements found = _search(pList, &pPre, &pLoc, keyPtr); if (found) _delete(pList, pPre, pLoc, dataOutPtr); return found; } // removeNode

void _delete(LIST* pList, NODE* pPre, NODE* pLoc, void** dataOutPtr) { //Statements *dataOutPtr = pLoc->dataPtr; if (pPre == NULL) // Deleting first node pList->head = pLoc->link; else // Deleting any other node pPre->link = pLoc->link; // Test for deleting last node if (pLoc->link == NULL) pList->rear = pPre; (pList->count)--; free(pLoc); return; } // _delete

bool searchList(LIST* pList, void* pArgu, void** pDataOut) { //Local Definitions bool found; NODE* pPre; NODE* pLoc; //Statements found = _search(pList, &pPre, &pLoc, pArgu); if (found) *pDataOut = pLoc->dataPtr; else *pDataOut = NULL; return found; } // searchList

bool _search(LIST* pList, NODE** pPre, NODE** pLoc, void* pArgu) { //Macro Definition #define COMPARE \ ( ((* pList->compare) (pArgu, (*pLoc)->dataPtr)) ) #define COMPARE_LAST \ ((* pList->compare) (pArgu, pList->rear->dataPtr)) //Local Definitions int result; //Statements *pPre = NULL; *pLoc = pList->head; if (pList->count == 0) return false; // Test for argument > last node in list if (COMPARE_LAST > 0) { *pPre = pList->rear; *pLoc = NULL; return false; } // if while ((result = COMPARE) > 0) { // Have not found search argument location *pPre = *pLoc; *pLoc = (*pLoc)->link; } // while if (result == 0) // argument found--success return true; else return false; } // _search

static bool retrieveNode(LIST* pList, void* pArgu, void** dataOutPtr) { //Local Definitions bool found; NODE* pPre; NODE* pLoc; //Statements found = _search(pList, &pPre, &pLoc, pArgu); if (found) { *dataOutPtr = pLoc->dataPtr; return true; } // if *dataOutPtr = NULL; return false; } // retrieveNode

bool emptyList(LIST* pList) { //Statements return (pList->count == 0); } // emptyList

bool fullList(LIST* pList) { //Local Definitions NODE* temp; //Statements if ((temp = (NODE*)malloc(sizeof(*(pList->head))))) { free(temp); return false; } // if // Dynamic memory full return true; } // fullList

int listCount(LIST* pList) { //Statements return pList->count; } // listCount

bool traverse(LIST* pList, int fromWhere, void** dataPtrOut) { //Statements if (pList->count == 0) return false; if (fromWhere == 0) { // Start from first node pList->pos = pList->head; *dataPtrOut = pList->pos->dataPtr; return true; } // if fromwhere else { // Start from current position if (pList->pos->link == NULL) return false; else { pList->pos = pList->pos->link; *dataPtrOut = pList->pos->dataPtr; return true; } // if else } // if fromwhere else } // traverse

LIST* destroyList(LIST* pList) { //Local Definitions NODE* deletePtr; //Statements if (pList) { while (pList->count > 0) { // First delete data free(pList->head->dataPtr); // Now delete node deletePtr = pList->head; pList->head = pList->head->link; pList->count--; free(deletePtr); } // while free(pList); } // if return NULL; } // destroyList

help me why this code happen the error I don't know about this.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!