0

I am trying to specifically use sieve prime numbers method to delete any numbers from the list that are not prime. I have a program that I have been working on, I so far don't have any prime numbers in the list, but I also end up deleting some prime numbers like 17 and 29, etc. *Note I am only deleting the multiples of 2 through 32. this is my code.

#include <stdio.h>
#include <stdlib.h>
typedef struct node{
    int data;
    struct node *next;
}node;
node *Inserttail(node *head, int x){
    node *temp = (node*)malloc(sizeof(node));
    temp->data = x;
    temp->next = NULL;
    node *temp1 = head;
    if(head==NULL)
        return temp;
    else if(head->next ==NULL){
        head ->next = temp;
        return head;
    }
    while(head->next != NULL)
        head = head->next;
    head->next = temp;
    return temp1;
}
void Print(node *head){
    while(head != NULL){
        printf(" %d", head->data);
        head = head ->next;
    }
}
node *Deletemultiples(node *head, int k){
    node *temp = head, *old = temp;
    int i = 1;
    while(temp!=NULL){
        if(i%k==0 && i!= k)
            old->next = temp->next;
        old=temp;
        temp= temp->next;
        i++;
    }
    return head;
}
int main(){
    node *head = NULL;
    int i;
    for(i=1; i<=1000; i++)
        head = Inserttail(head, i);
    for(i=2; i<=32; i++)
        head = Deletemultiples(head, i);
    Print(head);
    return 0;
}

this is my output that I am getting right now:

 1 2 3 5 7 11 13 19 23 31 35 43 49 59 61 79 83 103 109 119 133 151 155 175 193 211 215 241 259 275 283 323 331 361 373 403 419 443 455 499 511 541 571 613 623 649 673 719 733 781 803 841 871 919 931 991

The method that I am also using now is also not effective for me to be able to actually free that specific link from the list.

below_avg_st
  • 187
  • 15
  • 2
    Use `data` instead of `i`. Also You have memory leak. – BLUEPIXY Apr 25 '17 at 21:42
  • Note : In general `1` is not a prime number. – BLUEPIXY Apr 25 '17 at 21:49
  • 1
    and as usual, [don't case the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Sean Bright Apr 25 '17 at 21:54
  • hmm... my linked list is not printing using GCC or devc++. I was using an online compiler on my phone to program because I my laptop died. Now that I'm back on my laptop there is no output on the screen. I'm pretty sure my code and call are right for the Print function (?). – below_avg_st Apr 25 '17 at 23:18
  • @BLUEPIXY I've fixed my program, but I don't have an output if I execute with gcc. Is there perhaps something I did wrong in my Print function or call? – below_avg_st Apr 25 '17 at 23:21
  • I do not know the fixes you made so I can not say certain things, But Try `fflush(stdout);` or `printf("\n");`. E.g `Print(head);fflush(stdout);` or `Print(head);printf("\n");` – BLUEPIXY Apr 25 '17 at 23:27

1 Answers1

2

In your Deletemultiples function, you have the following:

int i = 1;
while (temp != NULL) {
   if (i % k == 0 && i != k)
       /* … Other stuff … */

The i in this case has no relationship to your list, so you shouldn't be using it. Instead, you want to look at the data member of your node structure:

while (temp != NULL) {
    if (temp->data % k == 0 && temp->data != k)

And this will give you the results that you are looking for:

1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 …
Sean Bright
  • 118,630
  • 17
  • 138
  • 146
  • that seems to have worked, thanks. I think I was going about this the wrong way by using 'i' and then getting stuck on that idea. – below_avg_st Apr 25 '17 at 22:03