c语言基础编程题求解

3. 编写程序,建立一个有3个结点的单向链表,每个结点包含姓名、年龄和工资。编写两个函数,一个用于建立链表,另一个用来输出链表。
#include <stdio.h>
#include <malloc.h>
#define stu struct student
stu
{
char name[20];
int age;
int wage;
stu *next;
};
stu *creat( void )
{

}
void output( stu *p )
{
while ( p != NULL )
{
printf( "%s: ", p->name );
printf( "age=%d wage=%d\n", p->age, p->wage );
p = p->next;
}
}
int main()
{
stu *p;
p = creat( );
output( p );
}
要求:1)上述程序文件名E4-8.C的文件,存放在E盘根目录下;
2)把建立链表的函数creat补充完整,数据输入和链表建立在函数中实现;
3)不得修改函数creat外的任何语句。

#include <stdio.h>

#include <malloc.h>

#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\n", p->age, p->wage );

    p = p->next;

  }

}

int main()

{ stu *p;

  p = creat( );

  output( p );

}

温馨提示:答案为网友推荐,仅供参考