Question: There seems to be a problem with my deleteStudent function because it only deletes the first name on the node it does not delete the

There seems to be a problem with my deleteStudent function because it only deletes the first name on the node it does not delete the whole node I have no idea why. This was a linked list implementation in C. This is my code:

#include #include #include

struct student { char firstName[20]; char lastname[20]; float score; char zipcode[10];

struct student *next; };

void printRecords(struct student *head) { struct student *temp=head; while(temp!=NULL) { printf(" First name: %s,Last name: %s,Score %f, Zip code:%s",temp->firstName,temp->lastname,temp->score,temp->zipcode); temp=temp->next; } printf(" "); }

void addNew(struct student **head) { struct student *temp1,*temp2;

temp1=(struct student*)malloc(sizeof(struct student)); printf(" Enter First name, Last name, score and Zip code:"); scanf("%s%s%f%s",temp1->firstName,temp1->lastname,&temp1->score,temp1->zipcode); if (*head==NULL) { *head=temp1; (*head)->next=NULL;

} else { temp2=*head;

while(temp2->next!=NULL) temp2=temp2->next; temp2->next=temp1; temp1->next=NULL; } }

void deleteStudent(struct student **head) { struct student *temp1=*head,*temp2; char lastName[20]; printf(" Enter last name to delete student: "); scanf("%s",&lastName); while(temp1!=NULL) { temp2=temp1; if(strcmp(temp1->lastname,lastName)==0) { strcpy(temp2->firstName,temp1->firstName); strcpy(temp2->lastname,temp1->lastname); temp2->score=temp1->score; temp2->next=temp1->next; free(temp1);

} temp1=temp1->next; } } void searchZip(struct student *head) { struct student *temp=head; char zip[20]; printf(" Enter zip code : "); scanf("%s",zip); while(temp!=NULL) { if(strcmp(temp->zipcode,zip)==0) //if searching found then display printf(" First name : %s, Last Name %s, Score %f, Zip code : %s",temp->firstName,temp->lastname,temp->score,temp->zipcode); temp=temp->next; //Goto next node } printf(" "); } void searchRange(struct student *head) { struct student *temp=head; float max,min; printf(" Enter minimum and maximum of score: "); scanf("%f%f",&min,&max); while(temp!=NULL) { if(temp->score>min&&temp->score<=max) printf(" First name: %s, Last name: %s,Score: %f,Zip code: %s",temp->firstName,temp->lastname,temp->score,temp->zipcode); temp=temp->next; }

} void medianScore(struct student* head) { float median,temp; int size=0,i,j,abovemedian=0; float scores[50]; while(head!=NULL) //Loop for getting all scores to array { scores[size++]=head->score; head=head->next; } //Sorting process for(i=0;iscores[j+1]) { temp=scores[j]; scores[j]=scores[j+1]; scores[j+1]=temp; }

//Printing median if(size%2==0) //If even scores are found median=(scores[(size/2)]); else //Odd scores found median=(scores[(size/2)+1]); printf(" Median is %f",median);

//no.of items more than median for(i=0;imedian) abovemedian++;

printf(" No.of students above median %d ",abovemedian); } int main() { struct student *head=NULL; int choice=1,i; printf("Enter 5 records: "); addNew(&head); addNew(&head); addNew(&head); addNew(&head); addNew(&head);

while(choice!=0) { printf(" "); printf("Print records (press 1) "); printf("Add a new record (press 2) "); printf("Delete record (press 3) "); printf("Search by Zip code(press 4) "); printf("Search by score range (press 5) "); printf("Find median score (press 6) "); printf("Exit the program (press 0) ");

scanf("%d",&choice); switch(choice) {

case 1: printRecords(head); break; case 2: addNew(&head); break; case 3: deleteStudent(&head); break; case 4: searchZip(head); break; case 5: searchRange(head); break; case 6: medianScore(head); break; case 0: printf("Exiting programm..."); break; default: printf("Invalid entry. ");

}} return 0; }

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!