32060049
2015-11-29 01:45
采纳率: 94.4%
浏览 1.9k
已采纳

C语言动态链表建立问题

#include
#include
#define LEN sizeof(struct Student)

int n = 0;//记录数据的个数

struct Student
{
    int number;
    int score;
    struct Student *next;
};


int main()
{
    struct Student *creat(void);
    void print(struct Student *p);
    struct Student *p;
    p = creat();
    print(p);
    return 0;
}


struct Student *creat(void)
{
    struct Student *p1,*p2;
    struct Student *head = NULL;
    p1 = p2 = (struct Student *)malloc(LEN);
    printf("please enter Students' informations(number & score:)");
    scanf("%d %d",&p1->number,&p1->score);//get
    while(p1->number != 0)
    {
        n += 1;
        if(n==1)head = p1;
        else p2->next = p1;
        p2 = p1;
        printf("please enter Students' informations(number &score):");
        scanf("%d %d",p1->number,p1->score);
        p1 = (struct Student*)malloc(LEN);

    }
   p2->next = NULL;
   return (head);
};


void print(struct Student *p)
{
    printf("their informations as follows:\n");
    while(p->number!=0)
    {
        printf("%d %d",p->number,p->score);
        p = p->next;
    }
}

代码如上,运行后输入第二个数据后显示“该内存不能为written”,用的是CODEBLOCKS。卡在这一天了,求解答,万分感谢~

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • Seek_Of 2015-11-29 01:57
    已采纳

    while(p1->number != 0) { n += 1; if(n==1)head = p1; else p2->next = p1; p2 = p1; printf("please enter Students' informations(number &score):"); scanf("%d %d",p1->number,p1->score); p1 = (struct Student*)malloc(LEN); }

    检查这里的scanf函数

    已采纳该答案
    打赏 评论
  • Meditator_hkx 2015-11-29 01:53

    你定义的create函数怎么是个指针?

    打赏 评论
  • dreamxiaowei 2015-11-29 02:12
     #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #define LEN sizeof(struct Student)
    int n = 0;//记录数据的个数
    struct Student
    {
        int number;
        int score;
        struct Student *next;
    };
    
    
    int main()
    {
        struct Student *creat(void);
        void print(struct Student *p);
        struct Student *p;
        p = creat();
        print(p);
        return 0;
    }
    
    
    struct Student *creat(void)
    {
        struct Student *p1, *p2;
        struct Student *head = NULL;
        p1 = p2 = (struct Student *)malloc(LEN);
        printf("please enter Students' informations(number & score:)");
        scanf("%d %d", &p1->number, &p1->score);//get
        while (p1->number != 0)
        {
            n += 1;
            if (n == 1)
                head = p1;
            else
                p2->next = p1;
            p2 = p1;
            printf("please enter Students' informations(number &score):");
            p1 = (struct Student*)malloc(LEN);
            scanf("%d %d", &p1->number, &p1->score);
    
    
        }
        p2->next = NULL;
        return (head);
    };
    
    
    void print(struct Student *p)
    {
        printf("their informations as follows:\n");
        while (p!= NULL)
        {
            printf("%d %d\n", p->number, p->score);
            p = p->next;
        }
    }
    
    打赏 评论
  • dreamxiaowei 2015-11-29 02:13

    1:

      p1 = (struct Student*)malloc(LEN);
            scanf("%d %d", &p1->number, &p1->score);
    

    2:

      while (p!= NULL)
        {
            printf("%d %d\n", p->number, p->score);
            p = p->next;
        }
    
    打赏 评论

相关推荐 更多相似问题