编写程序,建立一个有3个节点的单向链表,每个节点包含名字,年龄,工资。编写函数creat,

2025-05-09 07:07:06
推荐回答(3个)
回答1:

你那个next需是指针,*next,
h = (struct student *)malloc(sizeof(struct student)); int i=1;
p1 = p2 = h;
while(i<3)

{p1=p2;
p2=p1->next;
p2= (struct student *)malloc(sizeof(struct student)
}

回答2:

#include
#include
#define stu struct student
stu
{ char name[20];
int age;
int wage;
stu *next;
};
stu *creat( void )
{ stu *p,*q,*h;
int i;
for(i=0; i<3; i++)
{ p=(stu*)malloc(sizeof(stu));
scanf("%s%d%d",p->name,&(p->age),&(p->wage));
if(i==0)h=q=p;
else q->next=p,q=p;
}
q->next=NULL;
return h;
}
void output( stu *p )
{ while ( p != NULL )
{ printf( "%s: ", p->name );
printf( "age=%d wage=%d", p->age, p->wage );
p = p->next;
}
}
int main()
{ stu *p;
p = creat( );
output( p );
}

回答3:

h = (struct student *)malloc(sizeof(struct student)); int i=1;
p1 = p2 = h;
while(i<3)
{p1=p2;
p2=p1->next;
p2= (struct student *)malloc(sizeof(struct student)