链接列表程序显示学生标记

时间:2022-04-25 20:53:48
 #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 个解决方案

#1


1  

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)

#2


1  

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这是scanf的安全版本时,你不仅要传递缓冲区的地址,还要传递它的大小作为下一个参数,即:

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

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

您可以在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.

代码的另一个问题是您使用NULL初始化当前值,然后尝试访问其Next字段。

#3


0  

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.

你忘记在下一个指针添加NULL,因此你只得到单个输出。

#1


1  

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)

#2


1  

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这是scanf的安全版本时,你不仅要传递缓冲区的地址,还要传递它的大小作为下一个参数,即:

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

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

您可以在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.

代码的另一个问题是您使用NULL初始化当前值,然后尝试访问其Next字段。

#3


0  

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.

你忘记在下一个指针添加NULL,因此你只得到单个输出。