PLZ HELP ME WITH THIS CODE...I HAVE GIVEN THIS CODE SO MANY TIMES BUT NOBODY IS ABLE TO CLEAR MY ERROR....
1.DISPLAY IS NOT COMING ONLY LAST VALUE I ENTER IS DISPLAYED
2.DELETION OPETATION
.............AND YES I WANT TO USE LINKED LIST....IF ANY 1 KNOWS IT TELL ME.............

#include<stdio.h>
struct student
{
char name[20];
char  rollno[20];
struct student *next;
};
void display(struct student *newstudent,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("\nCurrent list\n");
while(newstudent!=NULL)
{
printf("Name:%s\nRoll:%d",newstudent->name,newstudent->rollno);
newstudent=newstudent->next;
}
}}
void delet(struct student *first,char 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);
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:");
gets(newstudent->rollno);
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(newstudent,n);
}
else
{
printf("List is Empty");
}
break;
case 3:
if(newstudent!=NULL)
{
display(newstudent,n);
}
else
{
printf("List is empty");
}
break;
case 4:
exit(0);
break;
}
}while(choice!=4);
return(0);
}

Recommended Answers

All 8 Replies

hey dude no need to invoke god in your post...there are enough human experts on this forum.:P While I am not the best guy to help with your problem, just be a little patient & in the meanwhile I'm sure you will be able to find many examples on linked list on this forum. All the best...
PS:Next time please don't make an all-caps post.

commented: Yup +6

First:

WRITING IN ALL CAPS IS CONSIDERED SHOUTING!!
And so your thread gets ignored.

Next:

Why do you keep opening new threads about the same linked-list program?
People gave you advice to improve your code, but you chose not to respond to them. Instead you opened up this new thread: HELPING ME PLZZZ FAST CODEZ NT WRKING

I'll give you the benefit of the doubt and say: Stop opening new threads on the same problem. Then ask a decent question in decent English and then you can expect people to help you

Just to add to the problems, post indented code as well.
Code can be hard enough to analyse even when it is indented properly. Your post just makes it that much worse.

FYI, I've been ignoring you until you fix this issue.

wow....what a way to ask for help!!

hey lets do it. First take user input:

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:");
          gets(newstudent->rollno);
          newstudent->next=NULL;
         if(first==NULL)
        {
               first=newstudent;
        }
        else
       {
              struct student *temp=first;
              while(temp->next!=NULL)
                    temp=temp->next;
              temp->next=newstudent;
        }
     }
break;

Now displaying

case 3:
if(first!=NULL)
{
     //display(newstudent,n);
     display(first,n);  //First is the head of ur link list
}
else
{
     printf("List is empty");
}
break;

Now display() Function

void function display(struct student *head, int n) {
     struct student *temp = head;  //Never change the head of ur link list thats why stored in temp variable
     while(temp!=NULL) {
           printf("\n%s, %s",temp->name, temp->rollno);
           temp = temp->next;
     }

}

Hope it will work!!

Sorry guys i am new to this community so i dont know how to ask.....and besides ur beautiful comments ur answers did not make my problems any easier....neways thanks for ur response

commented: passive agressiveness will get you far. -1

sir..your idea worked but now can u tellme how to do the deletion operation....i will be greatfull

hey manavsm,

In deletion u have compared the rollno(which is string) with '==' operator, which is wrong. In C string are not compared with '==' operator but there is function defined in string.h ie strcmp();

How to use:

if(strcmp(str1,str2)==0);

here str1 and str2 are strings if they are same then strcmp(str1,str2) will return 0.

Use this function to compare string. As Strings are nothing but a char pointer and comparing them with '==' will compare the addresses of both the pointers not the string.

Good luck!!

commented: youre so nice to the silly noobs. i just thought i'd tell you that, so now my cookie-karma is back to baseline. +4
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.