i want to delete the entry of a student using its roll no....its not working
It is my earlier program and i just modified it but now i am not being able to perform deletion operation..if you will help me my project will get over..
#include<stdio.h>
struct student
{
char name[20];
int rollno;
int physics;
int chemistry;
int biology;
int maths;
int IP;
int total;
float percentage;
struct student *next;
};
void display(struct student *head,int n)
{
struct student *temp=head;
printf("\nCurrent list\n");
while(temp!=NULL)
{
printf("\nName:%s\nRoll:%d\nPhysics:%d\nChemistry:%d\nBiology:%d\nMaths:%d\nIP:%d\n",temp->name,temp->rollno,temp->physics,temp->chemistry,temp->biology,temp->maths,temp->IP);
printf("Total:%d",temp->total);
printf("\nPercentage:%f",temp->percentage);
if(temp->percentage<=45)
{
printf("\nYou just failed");
}
else
{
printf("\nYou passed");
}
temp=temp->next;
}
}
void delet(struct student *first,int val)
{
struct student *ptr,*temp;
ptr=first;
while(ptr!=NULL)
{
if(ptr->rollno==val)
{
temp=ptr;
ptr=ptr->next;
free(temp);
}
else
{
printf("The roll no does not exists");
}
}
int main()
{
struct student *first,*last,*x,*newstudent;
first=last=NULL;
int c=0,val,choice,i,n;
do
{
printf("\n\n\tMenu linked list\n");
printf("1.Create\n2.Delete\n3.Display\n4.Exit\n");
printf("\nEnter your choice:");
scanf("%d",&choice);
getchar();
switch(choice)
{
case 1:
printf("\nEnter the no of students you want to add:");
scanf("%d",&n);
getchar();
for(i=0;i<n;i++)
{
newstudent=(struct student*)malloc(sizeof(struct student));
printf("\nEnter the name:");
gets(newstudent->name);
printf("\nEnter roll no:");
scanf("%d",&newstudent->rollno);
getchar();
printf("\nEnter the student marks");
printf("\nEnter physics,chemistry,biology,maths,IP marks:");
scanf("%d%d%d%d%d",&newstudent->physics,&newstudent->chemistry,&newstudent->biology,&newstudent->maths,&newstudent->IP);
getchar();
newstudent->total=newstudent->physics+newstudent->chemistry+newstudent->biology+newstudent->maths+newstudent->IP;
newstudent->percentage=newstudent->total/5;
newstudent->next=NULL;
if(first==NULL)
{
first=newstudent;
}
else
{
struct student *temp=first;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newstudent;
}
}
break;
case 2:
if(first!=NULL)
{
printf("\nEnter the roll:");
scanf("%d",&val);
delet(first,val);
display(first,n);
}
else
{
printf("List is Empty");
}
break;
case 3:
if(first!=NULL)
{
display(first,n);
}
else
{
printf("List is empty");
}
break;
case 4:
exit(0);
break;
}
}while(choice!=4);
return(0);
}