Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C program Please, make some improvements: In the newname(void) , use atoi to convert char to integers. You need to use the line char numstr[81];

C program Please, make some improvements: In the newname(void), use atoi to convert char to integers. You need to use the line char numstr[81]; Currently, it is not used.
 //agelink.c //maintains list of agents //uses linked list #include  #include  #include  //strcmp #define TRUE 1 void listall(void); void newname(void); void delink(void); void memexit(void); /********************************************************************* this is the structure to hold a agent information. prt *ptrnext is a point to link to next prt structure *********************************************************************/ struct prs { char name[40]; int agnumb; float height; struct prs *ptrnext; }; struct prs *ptrfirst = NULL; int n = 0; /*********************************************************************** main() calls other methods depending on user choice. you do not need the change code here. **********************************************************************/ int main(void){ while(TRUE) // cycle until user chooses 'q' { printf(" 'e' enter new agent 'l' list all agents"); printf(" 'd' delete a agent 'r' read file 'q' exit: "); char option; scanf("%c", &option); getchar(); switch(option) //gethche() gets() { case 'e': newname(); break; case 'l': listall(); break; case 'd': delink(); break; case 'q': memexit(); break; default: printf(" Enter only selections listed"); //puts("this is to be print out to screen") }//end switch }//end while }// end main(); /************************************************************** Complete this methods to ask user to enter agent name, agent number, and agent height to add a new agent to the linked list. use scanf() to read from the user inputs use atoi to convert char to integers. **************************************************************/ void newname(void){ struct prs *ptrthis; // this is the pointer to new agent. char numstr[81]; ptrthis = malloc(sizeof(struct prs)); if(ptrthis==NULL) { printf(" Can't reallocate memory"); return; } //your code start here printf("Enter Agent's name:"); scanf("%s", (ptrthis->name)); getchar(); printf("Enter Agent' age: "); scanf("%d", &(ptrthis->agnumb)); getchar(); printf("Enter Agent's height: "); scanf("%f", &(ptrthis->height)); getchar(); ptrthis->ptrnext = NULL; if(!ptrfirst) ptrfirst = ptrthis; else { ptrthis->ptrnext = ptrfirst; // note: you need make the line right to handle additonal link. ptrfirst = ptrthis; } } /************************************************************** Complete this method to print out all the agent list to screen **************************************************************/ void listall(void){ struct prs *ptrthis; if(ptrfirst == NULL){ printf(" Empty list"); perror("error in listall method"); return ; } ptrthis = ptrfirst; // in this do while loop, to print out the list do{ printf(" "); printf("Name: %s ", ptrthis->name); printf("Age: %d ", ptrthis->agnumb); printf("Height: %f ", ptrthis->height); ptrthis = ptrthis->ptrnext; } while (ptrthis != NULL); } /****************************************************************** //delink() //delete entry in agent list matching the name from user input ******************************************************************/ void delink(void){ struct prs *ptrthis, *ptrlast; // utility pointer char delname[81]; int flag = TRUE; //flag to indicate successful search if(ptrfirst == NULL){ printf(" Empty list in delink. "); return; } printf(" Enter name ot be deleted: "); scanf("%s",delname); getchar(); ptrthis = ptrfirst; //implement logica here to search and delete the record matching the name // user input from terminal. do not forget to free the memory resouces //used by the deleted records using free(void *ptr) method. ptrlast = ptrfirst; do{ //compare the name on the list if(strcmp(ptrthis->name, delname)==0){ //handle if first node is being deleted if(ptrthis == ptrfirst){ ptrfirst = ptrfirst->ptrnext; } ptrlast->ptrnext = ptrthis->ptrnext; free(ptrthis); ptrthis = NULL; flag = TRUE-1; //break the while loop here break; } ptrlast = ptrthis; ptrthis = ptrthis->ptrnext; }while(ptrthis !=NULL); if(flag==TRUE) printf("No such name on list "); else printf("Agent with name '%s' has been deleted ", delname); } //free all memeory and exist; void memexit(void){ struct prs *ptrthis, *ptrfree; if(ptrfirst == NULL) exit(0); ptrthis = ptrfirst; do{ ptrfree = ptrthis; ptrthis = ptrthis->ptrnext; free(ptrfree); }while (ptrthis != NULL); ptrfirst = NULL; //make list empty exit(0); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions