注册 登录
编程论坛 C语言论坛

单链表究竟该如何调用呢?不知哪里错了?

tyrooo 发布于 2019-12-09 19:54, 1336 次点击
为什么输入了5个数却无法输出呢?
程序代码:
#include <stdio.h>
#include<stdlib.h>
typedef struct student
{
    int score;
    struct student *next;
}Link;
//函数声明//
Link *creat(int);
void print(Link *head,int n);
//构造一个单链表//
Link *creat(int n)
{
    Link *head,*node,*end;
    head=(Link*)malloc(sizeof(Link));
    end=head;
    int i;
    for (i=0;i<n;i++)
    {
        node=(Link*)malloc(sizeof(Link));
        scanf("%d",&node->score);
        end->next=node;
        end=node;
    }
    end->next=NULL;
    return head;
}
//依次输出每个数//
void print(Link *head,int n)
{
    int i=0;
  while(i<n)
    {head=head->next;
     printf("%d",head->score);
     i++;
    }
//主函数调用,输入5个int数//
main()
{

 Link *p;

 p=creat(5);

 print(p,5);
}
2 回复
#2
纯蓝之刃2019-12-09 22:07
帖子https://bbs.bccn.net/thread-498310-1-1.html
https://bbs.bccn.net/viewthread.php?tid=497393&highlight=%C1%B4%B1%ED有你需要的

[此贴子已经被作者于2019-12-9 22:10编辑过]

#3
tyrooo2019-12-09 22:32
回复 2楼 纯蓝之刃
谢谢,原来是main() 中缺少了 typedef struct student Link;
1