为了账号安全,请及时绑定邮箱和手机立即绑定

大佬可以帮忙找下问题吗...创建单向链表总是出问题

#include <stdio.h>
#include <stdlib.h>

struct student
{
    int num;
    struct student *next;
};
struct student *creat(int n){
    struct student *head,*pnew,*ptail;
    int i;
    pnew=(struct student *)malloc(sizeof(struct student));
    scanf("%d",&pnew->num);
    head=ptail=pnew;
    for(i=1;i<n;i++){
        pnew=(struct student *)malloc(sizeof(struct student));
        scanf("%d",&pnew->num);
        ptail->next=pnew;
        ptail=pnew;
    }
    ptail->next=NULL;
    return head;
};
void print(struct student *head){
    struct student *p=head;
    while(p!=NULL){
        printf("%d",p->num);
     p=p->next;
    }
}
int main()
{
    int n,i;
    struct student *head;
    scanf("%d",&n);
    creat(n);
    print(head);
    return 0;
}

正在回答

1 回答

按照我的理解,应先找到头结点才能继续创建单向链表。并且头结点是不能存放数据的,你这句scanf("%d",&pnew->num);
head=ptail=pnew;我觉得看着像是对头结点存放了数据,去掉scanf试试看可不可以.链表指针这块我也步熟

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C语言入门
  • 参与学习       926306    人
  • 解答问题       20799    个

C语言入门视频教程,带你进入编程世界的必修课-C语言

进入课程

大佬可以帮忙找下问题吗...创建单向链表总是出问题

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信