Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct node
- {
- int info;
- struct node *next;
- };
- typedef struct node node;
- node *inserthead(node *head, int a){
- node *ptr;
- ptr = (node*)malloc(sizeof(node));
- ptr->info = a;
- ptr->next = head;
- return(ptr);
- }
- node *deletemultiples(node *head, int k){
- node *ptrcur = head, *ptrlast = ptrcur, *deletethis;
- while (ptrcur != NULL){
- if(ptrcur->info != k && ptrcur->info%k == 0){
- ptrlast->next = ptrcur->next;
- deletethis = ptrcur;
- ptrlast = ptrcur;
- ptrcur = ptrcur->next;
- free(deletethis);
- }
- else{
- ptrlast = ptrcur;
- ptrcur = ptrcur->next;
- }
- }
- return(head);
- }
- void printlist (node *head){
- while (head != NULL){
- if (head->next != NULL)
- printf("%d, ", head->info);
- if (head->next == NULL)
- printf("%d",head->info);
- head = head->next;
- }
- printf("\n");
- }
- void freelist (node *head){
- node *del = head;
- while(head != NULL) {
- head = head->next;
- free(del);
- del = head;
- }
- }
- int main(){
- int i;
- node *head = NULL;
- // This for loop creates the initial list of all integers 2-1000.
- for (i = 1000; i > 1; i--){
- head = inserthead(head, i);
- }
- // This deletes all multiples of all numbers between 1-32 from the list.
- if(head != NULL){
- for (i = 2; i <= 32; i++){
- deletemultiples(head, i);
- }
- // This prints the list, which should be the primes.
- printlist(head);
- freelist(head);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment