数据分析不是个事儿 2022-12-19 21:59 采纳率: 50%
浏览 20
已结题

求解C语言程序改错问题让用随机函数生成至少30000个整数


#include<iostream>
#include<cstdlib>
#include<cmath>
#define N 40000
using namespace std;


 struct Student
 {   
    int score;
    struct Student *next;
 };//创建单向链表,返回链表表头head
 struct Student *CreatLink(struct Student *head ,int n)
 {   
    int i;
    struct Student *p1,*p2;
    head=p1=(struct Student *)malloc(sizeof(struct Student));
    if(p1 == NULL)
    {   
        cout<<"Not enough memory to allocate buffer"<<endl;
        system("PAUSE");
        //exit(1); /* terminate program if out of memory */
    }
    p1->score = rand()%101;//产生随机值
    p1->next=NULL;
    for(i=2;i<=n;i++)
    {   
        p2=p1;
        p1=(struct Student *)malloc(sizeof(struct Student));
        if(p1 == NULL)
        {      
            cout<<"Not enough memory to allocate buffer"<<endl;
            system("PAUSE");
            exit(1); /* terminate program if out of memory */
        }
            p1->score = rand()%101;
            p1->next=NULL;//最近产生的节点下一节点指向空
            p2->next=p1;
    }
        return head;
 }//显示循环链表的成员

    void DisplayLink(struct Student *head)
    {   
        struct Student *p;
        p=head;
        do
        {   
            cout<<p->score<<endl;
            p=p->next;
        }
        while(p!=NULL); //p再次与head相等时,即所有成员都遍历完成
        printf("\\n\\n");
    }
  • 写回答

1条回答 默认 最新

  • 快乐鹦鹉 2022-12-19 22:08
    关注

    要先用srand函数生成随机种子

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 12月26日
  • 创建了问题 12月19日

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改