帮我看看这个c 程序错在哪了(在线等)

来源:百度知道 编辑:UC知道 时间:2024/09/21 10:55:59
请写一个C程序,将3-100以内的所有素数保存到一个链式线性表中,最后按顺序将单链表中的所有数据输出到屏幕上。

#include <stdio.h>
#include <malloc.h>
#define NULL 0
#define LEN sizeof(struct number)
struct number
{int num;
struct number * next;
};
int n,k,i,m;

void main()
{struct number * head;
struct student *p,*p1,*p2;
n=0;
p1=p2=(struct number *)malloc(LEN);
head=NULL;
for(i=3;i<101;i++)
{ k=sqrt(m);
for(i=2;i<=k;i++)
if(m%i==0)
break;
if(i>=k)
{p1->num=i;
p1=p1->next;}
n=n+1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
}
p2->next=NULL;
return(head);

p=head;
if(head!=NULL)
do
{printf("%d\n",p->num);
p=p->next;
}while(p!=NULL);

getch();
}

改了一下,错误在哪我就不说了,你对比一下就知道了。

#include <stdio.h>
#include <malloc.h>
#include<math.h>
#include<conio.h>

#define NULL 0
#define LEN sizeof(struct number)
struct number
{int num;
struct number * next;
};

void main()
{
int n,k,i,m;
number *head;
number *p,*p1,*p2;

head=NULL;
for(i=3;i<101;i++)
{
k=(int)sqrt(i);
for(m=2;m<=k;m++)
if(i%m==0)
break;
if(m>k)
{
if(head==NULL)
{
head=(struct number *)malloc(LEN);
head->num=i;
head->next=NULL;
p1=head;
}
else
{
p2=(struct number *)malloc(LEN);
p2->num=i;
p2->next=NULL;
p1->next=p2;
p1=p2;
}
}
}

p=head;
do
{
printf("%d\n",p->num);