// This is a linked list program. In the following program, the user enters the name , last name , zip code and scores of students. The delete function for the given program must take in a last name from the user and then delete the entries of all the students with that last name. I am having trouble as to how to implement this delete function. The complete code is as follows. //
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void print();
void insert();
void searchzip();
void range();
void median();
void delete();
int counter=0;
struct student
{
char firstname[21];
char lastname[21];
float score;
char zip[6];
struct student *next;
};
int n;
struct student *head;
int main()
{
int i,option;
head=NULL;
do
{
printf("\nType the number of records you want to enter (Please ensure that the number is more than or equal to 5) :");
scanf("%d",&n);
}
while(n<5);
for(i=0;i<n;i++)
{
struct student *new,*current;
new=(struct student*)malloc(sizeof(struct student));
printf("\nnPlease input records of studnent with following format first name last name zipcode score\n");
scanf("%s%s%s%f",new->firstname,new->lastname,new->zip,&new->score);
new->next=NULL;
if(head==NULL)
{
head=new;
current=new;
}
else
{
current->next=new;
current=new;
}
}
do
{
printf("\nPlease choose the appropriate options :");
printf("\nPrint records (press 1)\nAdd a new record (press 2)\nDelete Record(s) (press 3)\nSearch by ZipCode (press 4)\nSearch by score range (press 5)\nFind Median Score (press 7)\nExit the program (press 0)\n");
scanf("%d",&option);
switch(option)
{
case 1 :
print();
break;
case 2 :
insert();
break;
case 3 :
delete();
break;
case 4 :
searchzip();
break;
case 5 :
range();
break;
case 7 :
median();
break;
case 0 :
break;
default : printf("\n Please enter a valid option.");
}
}
while(option>=1 && option<=5);
return 0;
}
void print()
{
struct student*temp=head;
printf("\nThe following are the student records :");
while(temp!=NULL)
{
printf("\nFirst name: %s Last name: %s Score: %f ZipCode: %s",temp->firstname,temp->lastname,temp->score,temp->zip);
temp=temp->next;
}
printf("\n");
}
void insert()
{
struct student *new,*current,*temp;
new=(struct student *)malloc(sizeof(struct student));
printf("\nnPlease input records of studnent with following format first name last name zipcode score\n");
scanf("%s%s%s%f",new->firstname,new->lastname,new->zip,&new->score);
if(head==NULL)
{
head=new;
current=new;
}
else
{
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new;
}
}
void searchzip()
{
char code[6];
printf("\nPlease enter the zip code that you want to search :\n");
scanf("%s",code);
struct student *temp=head;
do
{
if(strcmp(code,temp->zip)==0)
{
printf("\nFirst name: %s Last name: %s Score: %f ZipCode: %s",temp->firstname,temp->lastname,temp->score,temp->zip);
}
temp=temp->next;
}
while(temp!=NULL);
}
void range()
{
float max,min;
printf("\nPlease enter the maximum and minimum score range :");
scanf("%f %f",&max,&min);
struct student *temp=head;
do
{
if((temp->score>=min)&&(temp->score<=max))
{
printf("\nFirst name: %s Last name: %s Score: %f ZipCode: %s",temp->firstname,temp->lastname,temp->score,temp->zip);
}
temp=temp->next;
}
while(temp!=NULL);
}
void median()
{
float arr[n],tempvar,med;
int i=0,j,pos;
struct student *temp=head;
do
{
arr[i]=temp->score;
i++;
temp=temp->next;
}
while(temp!=NULL);
print();
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
tempvar=arr[j];
arr[j]=arr[j+1];
arr[j+1]=tempvar;
}
}
}
if(n%2==0)
{
pos=n/2;
med=(arr[pos]+arr[pos-1])/2.0;
}
else
{
pos=(n/2);
med=arr[pos];
}
printf("\nThe median of all the students is : %f",med);
printf("\nThe students having a score above the median are :");
struct student *temp_2=head;
do
{
if((temp_2->score)>=med)
{
printf("\nFirst name: %s Last name: %s Score: %f ZipCode: %s",temp_2->firstname,temp_2->lastname,temp_2->score,temp_2->zip);
}
temp_2=temp_2->next;
}
while(temp_2!=NULL);
}
void delete()
{
int counter=0;
char name[21];
printf("\nPlease enter the last name that you want to delete :\n");
scanf("%s",name);
}