问题描述
#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)
输出:
解决方法
因为您的 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;