读书人

哪位高手能帮小弟我把Srand(int max)里

发布时间: 2012-02-15 12:09:44 作者: rapoo

谁能帮我把Srand(int max)里面的随机函数一个个的调进*HeadCreate()中!谢谢了!
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define NULL 0
#define keytype int
typedef struct node
{
keytype data;
struct node *next;
}NodeType;

NodeType *HeadCreate()
{
NodeType *head=NULL,*t;
keytype x;
int max;
scanf( "%d ",&max);
x=Srand(max);
t=(NodeType *)malloc(sizeof(NodeType));
t-> data=x;
t-> next=head;
head=t;
return head;
}

int Srand(int max)
{
int i;
srand((unsigned)time(NULL));
for(i=1;i <=max;i++)
printf( "%d\n ",rand());
return rand();
}

NodeType *Seq_search(NodeType *head,keytype k)
{
NodeType *p;
p=head-> next;
while(p!=NULL&&p-> data!=k)
p=p-> next;
return p;
}
main()
{
NodeType *la=NULL;
NodeType *p;
int k;
scanf( "%d ",&k);
la=HeadCreate();
p=Seq_search(la,k);
if(p!=NULL)
printf( "Find!, The Locatin is:%x,It 's data is %d\n ",p,p-> data);
else
printf( "Sorry,can 't find! ");
}

[解决办法]
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define NULL 0
#define keytype int
typedef struct node
{
keytype data;
struct node *next;
}NodeType;
int* Srand(int* const pn,const int max);

NodeType *HeadCreate()
{
NodeType *head=NULL,*t;
//keytype x;
int max;
printf( "max: ");
scanf( "%d ",&max);

//要一次产生max个随机数,必须有max个随机数存放的空间
int * pn=(int*)malloc(sizeof(int)*max);
Srand(pn,max);//把pn传进去由Srand使用
//t=(NodeType *)malloc(sizeof(NodeType));
head=t=(NodeType *)malloc(sizeof(NodeType)*max);
for(int i=0;i <max;i++)
{
t-> data=pn[i];
t-> next=t+sizeof(NodeType);
t=t-> next ;
}
t-> next=NULL;
//t-> data=x;
//t-> next=head;
//head=t;
free(pn);
return head;
}

int* Srand(int* const pn,const int max)//const表示pn不会被修改
{
int i;
srand((unsigned)time(NULL));
//for(i=1;i <=max;i++)
for(i=0;i <max; i++)
{
pn[i]=rand();
printf( "%d\n ",pn[i]);
}
return pn;
}

NodeType *Seq_search(NodeType *head,keytype k)
{
NodeType *p;
//p=head-> next;
p=head;


while(p!=NULL&&p-> data!=k)
p=p-> next;
return p;
}
main()
{
NodeType *la=NULL;
NodeType *p;
int k;

la=HeadCreate();
//链表创建后再输入要查找的数:
printf( "K: ");
scanf( "%d ",&k);
p=Seq_search(la,k);
if(p!=NULL)
printf( "Find!, The Locatin is:%x,It 's data is %d\n ",p,p-> data);
else
printf( "Sorry,can 't find!\n ");

return 0;
}

读书人网 >C语言

热点推荐