Link list program to Display Student marks

8.2k views Asked by At
 #include<stdio.h>

 #include<conio.h>

 #include<stdlib.h>

 void Insert();
 void DisplayList();
 struct Student
 {
 char Name[10];
 int Marks;
 struct Student *Next;
 } *Start;
 int main()
 {
 Start = NULL;
 int Choise;
 while (1)
 {
 printf("enter number to choose ");
 scanf_s("%d", &Choise);
 switch (Choise)
 {
 case 1:
    Insert();
    break;
    case 3:
        DisplayList();
        break;
    default:
    printf("Incorrect assignment Press relevant key :");
    }
    }
    }
    void Insert()
    {
    struct Student *Temp, *current=NULL;
    Temp = (struct Student *) malloc(sizeof(struct Student));
    printf("Enter Name Of Student");
    scanf_s("%s",&Temp->Name);
    printf("Enter Marks Of Student");
    scanf_s("%d", &Temp->Marks);
    Temp->Next = NULL;
    if (Start == NULL)
    {
        Start = Temp;
        Temp->Next = NULL;
    }
     else
        current = Start;
        while (current->Next != NULL)
        {
        current = current->Next;
        current->Next = Temp;
        }
    }
  void DisplayList()
   {
    struct Student *current, *Temp;
    current = Start->Next;
    if (Start == NULL)
    {
        printf("No Element in the list");
    }
    else
    {
        for (current = Start; current != NULL; current = current->Next)
        {
            printf("The List are\n");
            printf_s("%d",current->Marks);
        }

    }

this is a Program written for single linked list.when i display the list it give only one element in the list. Whenever i am trying to print the elements of the linked list, it gives the output only one element what mistake i do please help?

3

There are 3 answers

1
BLUEPIXY On BEST ANSWER

change

else
    current = Start;
    while (current->Next != NULL)
    {
    current = current->Next;
    current->Next = Temp;
    }

to

else {
    current = Start;
    while (current->Next != NULL)
    {
        current = current->Next;
    }
    current->Next = Temp;
}

and

scanf_s("%s", Temp->Name, sizeof(Temp->Name)); //remove & and add size(see Amnon's answer)
0
Amnon Shochot On

When using scanf_s which is the safe version of scanf, you are expected to pass not only the address of the buffer but also its size as the next parameter, i.e.:

scanf_s("%s", Temp->Name, _countof(Temp->Name));

You can read more about it in http://msdn.microsoft.com/en-us/library/w40768et.aspx

The other issue with the code is that you're initializing current with NULL but then trying to access its Next field.

0
pranav mishra On

Change the code here:

  else
     current = Start;
     while (current->Next != NULL)
    {
     current = current->Next;
     current->Next = Temp;
     }

to this:

else {
     current = Start;
      while (current->Next != NULL)
      {
      current = current->Next;
       }
      current->Next = Temp;
      temp->next=NULL;
    }

you forget to add NULL at next pointer and therefore you are getting only single output.