急急急!请大家指点一下啊!谢了!

QQ530661007 2007-06-25 11:59:09
怎样修改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;
}
...全文
191 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bm1408 2007-06-26
  • 打赏
  • 举报
回复
你说的是什么意思?
QQ530661007 2007-06-26
  • 打赏
  • 举报
回复
谢谢大家
jixingzhong 2007-06-26
  • 打赏
  • 举报
回复
以上程序是在原程序基础上略做修改得到 ...
jixingzhong 2007-06-26
  • 打赏
  • 举报
回复
#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;
}
lightnut 2007-06-26
  • 打赏
  • 举报
回复
#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;
}
jixingzhong 2007-06-26
  • 打赏
  • 举报
回复
问题忒多 ...

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧