when i am entering name and roll no only the last name and rollno is displayed and rest is not....i unable to detect where i went wrong and also ...deletion operation is also not working.........and ya i want to try this using linked list...plz i have to do this ne how .......


include<stdio.h>
struct student
{
char name[20];
int rollno;
struct student *next;
};
void display(struct student *first)
{
struct student *ptr;
ptr=first;
printf("\nCurrent list:\n");
while(ptr!=NULL)
{
printf("Name:%s\nRoll:%d",ptr->name,ptr->rollno);
ptr=ptr->next;
}
}
void delet(struct student *first,int val)
{
struct student *ptr,*list ;
ptr=first;
while(ptr!=NULL)
{
if(ptr->rollno==val)
{
list=ptr;
ptr=ptr->next;
free(list);
}
}
}
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);


switch(choice)
{
case 1:
printf("\nEnter the no of students you want to add:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newstudent=(struct student*)malloc(sizeof(struct student)
first=newstudent;
printf("\nEnter the name:");
gets(first->name);
printf("\nEnter roll no:");
scanf("%d",first->rollno);
first->next=NULL;
last=first;
}
break;
case 2:
if(first!=NULL)
{
printf("\nEnter the roll:");
scanf("%d",&val);
delet(first,val);
display(first);
}
else
{
printf("List is Empty");
}
break;
case 3:
if(first!=NULL)
{
display(first);
}
else
{
printf("List is empty");
}
break;
case 4:
exit(0);
break;
}
}while(choice!=4);
return(0);
}

I have modified your code:

case 1:
printf("\nEnter the no of students you want to add:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
newstudent=(struct student*)malloc(sizeof(struct student)
//first=newstudent;
printf("\nEnter the name:");
gets(newstudent->name);
printf("\nEnter roll no:");
scanf("%d",newstudent->rollno);
newstudent->next=NULL;

if(first==null) {
first = newstudent;

} else{

struct student* temp = first;
while(temp->next != null)
    temp = temp->next;

[B]temp->next=newstudent;[/B]
//last=first;

}
}
break;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.