读书人

急请大家指点一下啊谢了!解决方法

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

急急急!请大家指点一下啊!谢了!
怎样修改Srand,加上随机数存储目的的指针 或者 分配足够空间存储随机数反回地址外面释放空间.才能使我的程序能够调用不同的随机函数进行查找!请修改一下吧!

#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 i;
int value;
srand((unsigned)time(NULL));
value = rand();
printf( "%d\n ",value);
return value;
}
NodeType *HeadCreate()
{

NodeType *head=NULL,*t;
int i, max;
scanf( "%d\n ",&max);
head=(NodeType *)malloc(sizeof(NodeType));
head-> data=Srand();
t=head;
for (i=0;i <max;++i)
{
t-> next=(NodeType *)malloc(sizeof(NodeType));
t-> next-> data=Srand();
t=t-> next;
t-> next=NULL;
}
return head;
}

NodeType *Seq_search(NodeType *head,keytype k)
{
NodeType *p=p-> next;
while(p!=NULL&&p-> data!=k)
p=p-> next;
return p;
}

int main(void)
{

NodeType *la=NULL;
NodeType *p;
int k;
scanf( "%d\n ",&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! ");
return 0;
}


[解决办法]
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define NULL 0
#define keytype int

typedef struct node
{
keytype data;
struct node *next;
}NodeType;

void Srand(NodeType* node)
{
node-> data = rand();
printf( "%d\n ",node-> data);
}

NodeType *HeadCreate()
{
srand((unsigned)time(NULL));

NodeType *head=NULL,*t;
int i, max;
printf( "input the number of node: ");
scanf( "%d ",&max);

head=(NodeType *)malloc(sizeof(NodeType));
Srand(head);
t=head;
for (i=0;i <max;++i)
{
t-> next=(NodeType *)malloc(sizeof(NodeType));
Srand(t-> next);
t=t-> next;
t-> next=NULL;
}
return head;
}

NodeType *Seq_search(NodeType *head,keytype k)
{
NodeType *p = head;
while(p!=NULL&&p-> data!=k) p=p-> next;


return p;
}

int main(void)
{

NodeType *la=NULL;
NodeType *p;
la=HeadCreate();

int k;
printf( "Input number to seach: ");
scanf( "%d ",&k);

p=Seq_search(la,k);
if(p!=NULL)
printf( "Find!The Locatin is:%p,It 's data is %d\n ",p,p-> data);
else
printf( "Sorry,can 't find! ");
return 0;
}

[解决办法]
#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 i;
int value;
value = rand();
//printf( "%d\n ",value); //这个输出不要了,定义了一个 print 函数查看链表
return value;
}
NodeType *HeadCreate()
{

NodeType *head=NULL,*t;
int i, max;
printf( "How many node(s): "); //提示信息
scanf( "%d ",&max); //scanf函数 \n 格式控制字符不应当使用
head=(NodeType *)malloc(sizeof(NodeType));
head-> data=Srand();
t=head;
for (i=0;i <max;++i)
{
t-> next=(NodeType *)malloc(sizeof(NodeType));
t-> next-> data=Srand();
t=t-> next;
t-> next=NULL;
}
return head;
}

NodeType *Seq_search(NodeType *head,keytype k)
{
NodeType *p=head;
while(p!=NULL&&p-> data!=k)
p=p-> next;
return p;
}

void print(NodeType *head)
{
NodeType *p=head;
printf( "\nThe list is:\n ");
while(p!=NULL)
{
printf( "%d -> ", p-> data);
p = p-> next;
}
printf( "NULL\n ");
}

int main(int argc, char* argv[])
{
NodeType *la=NULL;
NodeType *p;
int k;

srand((unsigned)time(NULL)); //! 种子在这里安置,否则容易得到重复序列
la=HeadCreate();
print(la); //输出链表查看
printf( "Search: "); //提示信息
scanf( "%d ",&k); //scanf函数 \n 格式控制字符不应当使用
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! ");

system( "pause ");
return 0;
}

读书人网 >C++

热点推荐