生成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;
}