微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我尝试实现链接列表以在节点中存储学生详细信息

如何解决我尝试实现链接列表以在节点中存储学生详细信息

这是我实现链接列表的代码,用于在节点中存储学生详细信息

#include <stdio.h>
#include <stdlib.h>

struct Student_Database{
    char Name[100];
    char RegId[100];
    int age;
    double cgpa;
    struct Student_Database*next;
};

void  PushFirst(struct Student_Database* a){
    
    printf("enter the name:");
    fgets(a->Name,sizeof(a->Name),stdin);
    printf("enter the regId");
    fgets(a->RegId,sizeof(a->RegId),stdin);
    printf("enter the age");
    scanf("%d",&a->age);
    printf("enter the cgpa");
    scanf("%lf",&a->cgpa);
    a->next=NULL;
}

void Push(struct Student_Database* a,struct Student_Database* b){
 
    printf("enter the name:");
    fgets(a->Name,&a->cgpa);
    a->next=NULL;
    b->next=a;
}

void display(struct Student_Database* a){
    struct Student_Database* ptr=a;
    while(ptr!=NULL){
        printf("name of the student:");
        puts(ptr->Name);
        printf("the regdid:");
        puts(ptr->RegId);
        printf("the age:");
        printf("%d",ptr->age);
        printf("the cgpa:");
        printf("%lf",ptr->cgpa);
        ptr=ptr->next;
    }
}
/*here a is the popped node and b is the node before a   */
void pop(struct Student_Database* a,struct Student_Database* b){
    b->next=NULL;
    
}

int main(){
    struct Student_Database*head;
    struct Student_Database*one;
    
    head=(struct Student_Database*)calloc(1,sizeof(struct Student_Database));
    one=(struct Student_Database*)calloc(1,sizeof(struct Student_Database));
    PushFirst(head);
    Push(one,head);
    display(head);
   
   }

我有推送功能,它可以输入最后一个节点和之前的节点。
当我尝试 Push(one) 时,它不会输入名称。它接受第一个节点的名称输入 PushFirst(head)

输出

the output of the code

解决方法

因为您的 fgets 函数读取了 push 函数中的换行符 ('\n)。 对于这种从控制台输入读取数据,您应该使用 scanf

printf("enter the name: ");
scanf("%s",&a->Name);
printf("enter the regId: ");
scanf("%s",&a->RegId);
printf("enter the age: ");
scanf("%d",&a->age);
printf("enter the cgpa: ");
scanf("%lf",&a->cgpa);
a->next = NULL;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。