您的位置:首页 > 其它

单链表_生成100个随机数_查询并输出大于K的数

2016-09-15 11:20 316 查看

生成100个随机数,并查询大于k的输出。

#include"stdio.h"
#include"time.h"
#include"stdlib.h"
typedef struct node
{
int num;
struct node * next;
}node;

void chaxun(int k,node *h)//查询大于等于k的并输出
{
node *p=h->next;
int jishu=0;

while(p->next!=NULL)
{
if(p->num>=k)
{

printf("第 %5d 个结点的 %5d 大于等于 %5d\n",jishu,p->num,k);
jishu++;
}
p=p->next;
}
}
int main()
{
srand(time(NULL));//生成时间种子。
int a[100];//定义100个整数
for(int i=0;i<100;i++)
{
a[i]=rand()%10000;//生成100个随机数
}

node *h=(node *)malloc(sizeof(node));//新建头节点h
h->next=NULL;
node *p=h;

for(int i=0;i<100;i++)
{
node *r=(node *)malloc(sizeof(node));
r->num=a[i];
r->next=NULL;
p->next=r;
p=r;
}

printf("查询大于等于:");
printf("_______的数\b\b\b\b\b");
int k;
scanf("%d",&k);
printf("\n");
chaxun(k,h);
node *r2=h;

while(r2->next!=NULL)
{
node *r3=r2->next;
r2=r3->next;
free(r3);
}
free(h);
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  随机数 单链表 查询
相关文章推荐