indrajeet6 0 Newbie Poster

Hi,
I'm trying to write a linked list Program to accept a no. and print all the prime numbers less than it, using the sieve method, Explained below:

Suppose that n=30.Then we list all the numbers till 30, and cross out 1 and all the multiples of 2, then 3,then 5.

This results in 7 being the first element from 5 that has not been crossed out.But 7*7>30,so all the remaining numbers are the primes less than 30.

The code that has the Logical Error and the main() are as below:

void append(struct node **q,int num)
{   struct node *temp,*r;
	 temp = *q;
	 if(*q==NULL)
	 {   temp = (struct node *)malloc(sizeof(struct node));
		  temp->data=num;
		  temp->link=NULL;
		  *q=temp;
	 }
	 else
	 {  temp = *q;
		 while(temp->link !=NULL)
		 {  temp=temp->link;   /*This is where the computer shows me the cursor is when I have to do a manual break, as the computer stops responding*/
		 }
		 r = (struct node *)malloc(sizeof(struct node));
		 r->data=num;
		 r->link=NULL;
		 temp->link=r;
	 }
}


void main()
{ float i,j,k;
  float n;
  node *p;        //head pointer
  clrscr();
  p=NULL;
  printf("\tThe Sieve Method to find the Primes before a given number");
  printf("\nEnter the number\n");
  scanf("%f",&n);
  for(i=0;i<=i;i++)/*Creating the List of numbers*/
    { append(&p,i);
    }
  for(j=2;j<=sqrt(n);j++)
    { for(k=2*j;k<=n;k++)
       { del(&p,k);
       }
    }
  display(&p);
}

I am also attaching the code as an attachment file.

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.