/*Topic 59: The list is as follows typedef struct _LINKLIST{INT data;struct _linklist*next;} Linklist The following node data fields 1 2 3 4 5 6 7 8 12 19 .... Requirement 1: Create List requirement 2: Delete nodes with an even number of nodes, 70 requirements 3: Write test cases 30 points*/#include<stdio.h>#include<stdlib.h>#include<string.h>typedefstruct_linklist{intdata; struct_linklist *Pnext;} linklist;//initializing the node of a linked headerintInitlink (linklist **pout/* out*/){ intErro_msg =0; if(pout==NULL) {erro_msg=1; printf ("pout==null Erro msg:%d\n", erro_msg); returnerro_msg; } linklist* Phead = (linklist *)malloc(sizeof(linklist)); if(phead==NULL) {erro_msg=2; printf ("failed to allocate memory Erro msg:%d\n", erro_msg); returnerro_msg; } phead->data =0; Phead->pnext =NULL; *pout =Phead; returnerro_msg;}//Create a linked list nodeintCreatelink (linklist *pin/*inch*/,intdata) { intErro_msg =0; if(pin==NULL) {erro_msg=1; printf ("pin==null Erro msg:%d\n", erro_msg); returnerro_msg; } linklist*phead =pin; Linklist*pcurrent = phead->Pnext; //Create a new nodeLinklist *pmalloc = (linklist *)malloc(sizeof(linklist)); Pmalloc->data =data; Pmalloc->pnext =NULL; if(pcurrent==NULL) {Phead->pnext =Pmalloc; returnerro_msg; } //Traverse to the end of the linked list node while(pcurrent->Pnext) {pcurrent= pcurrent->Pnext; } pcurrent->pnext =Pmalloc; returnerro_msg;}//Delete a node with an even number of nodesintDellink (linklist *pin/*inch*/){ intErro_msg =0; if(pin = =NULL) {erro_msg=1; printf ("pin==null Erro msg:%d\n", erro_msg); returnerro_msg; } linklist*phead =pin; Linklist*pcurrent = phead->Pnext; Linklist* Pprior =Phead; while(pcurrent) {if(pcurrent->data%2==0) {Pprior->pnext = pcurrent->Pnext; //Release this node Free(pcurrent); Pcurrent=NULL; Pcurrent= pprior->Pnext; } Else{Pprior=pcurrent; Pcurrent= pcurrent->Pnext; } } returnerro_msg;}//Print linked listvoidPRINTFA (linklist *pin/*inch*/){ if(pin==NULL) {printf ("The list cannot be empty! \ n"); } linklist*phead =pin; Linklist*pcurrent = phead->Pnext; while(pcurrent) {printf ("%d\n", pcurrent->data); Pcurrent= pcurrent->Pnext; }}//release linked listintFreelink (linklist **pin/*inch*/){ intErro_msg =0; if(pin==NULL) {erro_msg=1; printf ("pin==null Erro msg:%d\n", erro_msg); returnerro_msg; } linklist*phead = *pin; Linklist*pcurrent = phead->Pnext; Linklist*pprior =Phead; while(pcurrent) {Pprior->pnext = pcurrent->Pnext; //Freeing Memory if(pcurrent) { Free(pcurrent); Pcurrent=NULL; } pcurrent= pprior->Pnext; } if(phead) { Free(Phead); Phead=NULL; } returnerro_msg;}voidMain () {inti =0, ret=0; //initializing the node of a linked headerLinklist *phead =NULL; //Initialize the head node.Initlink (&phead); //Create a node for(i =0; I <9; i++) {ret=createlink (Phead, i +1); if(ret!=0) {printf ("Create linked list node Error! \ n"); //release all linked list nodes GotoEND; } } //Print all NodesPRINTFA (Phead); //Delete a partial noderet=Dellink (Phead); if(ret!=0) {printf ("Delete even node error! \ n"); //release all linked list nodes GotoEND; } //Print all NodesPRINTFA (Phead); END://Release all NodesFreelink (&phead); System ("Pause");}
C language Tempered