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);
}

Well you can make our task a lot easier by INDENTING your code


Anyway thanks to Eclipse I could indent your code directly.
Now lets look at your "delet" function

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");
		}
	}
}

This appears to be linked list program,
While deleting an element also need to hold a reference to the element preceding the node to be deleted, so your "if" condition should change to

ptr->next->rollno==val

and you will also need to set the "next" pointer of the node before the node-to-delete to the address of the node after the node-to-delete by writing:

ptr->next=ptr->next->next;

before invoking free on the node-to-delete

Well error is coming ..Segmentattion fault
just plzz try to help me out as soon as possible

void delet(struct student *first,int val)
{
struct student *ptr,*temp;
ptr=first;
while(ptr!=NULL)
{
if(ptr->next->rollno==val)
{
ptr=temp;
ptr->next=ptr->next->next;
free(temp);
}
}
}

Haven't you ever heard of code indentation ?????

How can you free "ptr", ptr points to the node preceding the node to be deleted, you do not free "ptr", you free the node after "ptr"

Please think !!! do not just directly copy paste the code given here.

You really need to learn to debug !!!
How can you do:-

ptr=temp;

thats complete carelessness, it should be

temp=ptr->next

Also your while condition should change and I am not going to help u on that, you should start thinking and stop simply copy pasting points given here

hey i have not freed "ptr" i have freed temp..OK n ne wayz if you dont know then dont come up with stupid comments like this ....i think u should learn this..what the fuck can u do ...NERD

Comments
Posters like this aren't needed on these forums.
Come back when you've learnt some manners and how to indent code
Fine at least am not a low life begger with an attitude

by using:

if(ptr->next->rollno == val)

You are bypassing the first node. This will never check the first node for rollno.

It should first check for first node and then loop for remaining node.

For first node :

if(ptr->rollno == val) {
    temp = ptr;
    ptr = ptr->next;
    first = ptr;
    free(temp);
} else {
    
   //Your remaining code as above
   //Iterate through remaining nodes

}

hey i have not freed "ptr" i have freed temp..OK n ne wayz if you dont know then dont come up with stupid comments like this ....i think u should learn this..what the fuck can u do ...NERD

No wonder your program doesn't work as you don't have a clue what you are doing.
....Anyways I wasnt the one begging via PM for help

This article has been dead for over six months. Start a new discussion instead.