|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 shuofxz 于 2016-3-27 22:25 编辑
下面的代码是将输入的学号和成绩建立链表,并最终输出
代码可以运行,我想问的是:
struct student *creat() 为什么函数名前面要加*,有什么作用?
- #include "stdio.h"
- #include "malloc.h"
- #define NODE struct student
- struct student{
- int num;
- float score;
- struct student *next;
- };
- struct student *creat(){
- NODE *p, *q, *head;
- p = q = (NODE*)malloc(sizeof(NODE));
- head = NULL;
- int n = 0;
- printf("请输入学号:");
- scanf("%d",&(p->num));
- while(p->num)
- {
- printf("请输入成绩:");
- scanf("%f", &(p->score));
- printf("\n");
- if(n == 0){
- head = p;
- n += 1;
- }
- else{
- q->next = p;
- }
- q = p;
- p = (NODE*)malloc(sizeof(NODE));
- printf("请输入学号:");
- scanf("%d",&(p->num));
- }
- q->next = NULL;
- return head;
- }
- void print(NODE *head){
- NODE *p;
- p = head;
- while(p)
- {
- printf("%d \t %3.1f\n", p->num, p->score);
- p = p->next;
- }
- }
- int main(){
- struct student *stu;
- stu = creat();
- print(stu);
- return 0;
- }
复制代码 |
|