C语言数据结构
各路大神请帮帮,急
请编写一个完整的程序,使用顺序表实现一下任务:(1)产生50个1到100的随机整数,将其中的偶数依次保存到顺序表中。
(2)输出顺序表的所有元素,并求顺序表的长度。
程序代码:#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LEN 1024
struct sArray
{
int iAdata[LEN];
int LENth;
};
typedef struct sArray *p;
int count=0;
p initlist(p L)
{
L =(p) malloc(sizeof(struct sArray));
L->LENth = 0;
return L;
}
void create(p L)
{
int i,a;
srand((int)time(0));//设置随机数种子
for (i = 0; i < 50; i++)
{
/*printf("%d ",(int)rand()%10);*/
a = (int)rand() % 100+1;//生成随机数
if (a % 2 == 0)//将其中的偶数存到表中
{
L->iAdata[L->LENth] = a;
L->LENth++;
}
}
count = L->LENth;
}
void prit(p L)
{
p temp;
temp = L;
int j=0;
while (j<count)
{
printf("%d ",temp->iAdata[j]);
j++;
}
printf("\n");
printf("长度为:%d",temp->LENth);
}
int main()
{
p pl=NULL;
pl=initlist(pl);
create(pl);
prit(pl);
return 0;
}