clinked-listmallocdisplaylisthttpunhandledexception

Link list program to Display Student marks


 #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?


Solution

  • 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)