i am facing problem when i am trying to get input...my input is not being taken correctly ...

#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);
first=newstudent;
printf("\nEnter the name:");
gets(first->name);
printf("\nEnter roll no:");
scanf("%d",first->rollno);
first->next=NULL;
last=first;
for(i=1;i<n;i++)
{
x=newstudent;
printf("Enter your name:");
gets(first->name);
printf("Enter your rollno:");
scanf("%d",&first->rollno);
x->next=NULL;
last->next=NULL;
last=x;
}
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);
}

What have you coded a Link-List:(

Ok any ways, I am tring to solve:

case 1:
printf("\nEnter the no of students you want to add:");
scanf("%d",&n);
[B]//have you allocate any memory for newstudent....No u don't
newstudent = (struct student*) malloc(sizeof(student));
first=newstudent;[/B]printf("\nEnter the name:");
gets(first->name);
printf("\nEnter roll no:");
scanf("%d",first->rollno);
first->next=NULL;
last=first;
for(i=1;i<n;i++)
{
[B]//again you have not allocated memory for newstudent
newstudent = (struct student*) malloc(sizeof(student));
[/B]

x=newstudent;
printf("Enter your name:");
gets(first->name);
printf("Enter your rollno:");
[B]//scanf("%d",&first->rollno);[/B]
scanf("%d",first->rollno);
x->next=NULL;
last->next=NULL;
last=x;
}
break;

What a mess u have written please try to use functions for Creating, Deleting and Displaying etc.

display is also not coming along with deletion

hey buddy,

Try to clean ur code by using functions, then only it will be easy for us to find whats wrong in that.

hey, Chap,

theres nothing inherently WRONG with not breaking it into smaller functions. though it might be BETTER, its not a requirement, and you shouldnt berate the questioner because s/he didnt code the way you prefer.

But I will agree, the code is nearly unreadable, yes... But that is because the poster did not use CODE TAGS.

MANAVSM, if you hope to get help here, please use CODE TAGS, and make sure you keep the indentations, so we can read what you have written.

its not because we're being sticklers to some arbitrary rule... many of us are in work or school and we dont have time to fix formatting of someones code just to read it.

i try and pop in from time to time, and give help when i can. but if i cant read your code without a lot of effort, i'll be more likely to just ignore it altogether.

just so you'll know.


.

how to use code tags ..i dont know i am new to this community

i provided LINKS in the previous post.

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.