数据分析不是个事儿 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日