Created
December 16, 2019 06:58
-
-
Save thinkphp/bae8a3fe68caff745b603b922b2ec871 to your computer and use it in GitHub Desktop.
Give a N = and removes all primes digits from this number using linked simple list.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <malloc.h> | |
struct Node { | |
int info; | |
struct Node *next; | |
}; | |
typedef struct Node Node; | |
Node *head = NULL; | |
int isPrime(int n) { | |
int prime = 1, k = 2; | |
if(n == 0 || n == 1) return 0; | |
if(n == 2 || n == 3) return 1; | |
while(prime && k*k <= n) { | |
prime = n % k != 0; | |
k++; | |
} | |
return prime; | |
} | |
void LinkedListCreated(int number) { | |
Node *newnode; | |
newnode = (Node*)malloc(sizeof(Node)); | |
newnode->info = number%10; | |
newnode->next = head; | |
head = newnode; | |
if(number > 9) LinkedListCreated(number/10); | |
} | |
void display(Node *head) { | |
Node *p = head; | |
while(p!=NULL) { | |
if(p->info) printf("%d ", p->info); | |
p = p->next; | |
} | |
printf("\n"); | |
} | |
void deletedPrimes(Node *head) { | |
Node *q, *r; | |
q = head; | |
if(isPrime(head->info)) { | |
r = head; | |
head = head->next; | |
free(r); | |
} | |
while(q->next != NULL) { | |
r = q->next; | |
if(isPrime(r->info)) { | |
q->next = r->next; | |
free(r); | |
} else { | |
q = q->next; | |
} | |
} | |
} | |
int main() { | |
int n; | |
printf("N="); | |
scanf("%d", &n); | |
LinkedListCreated(n); | |
display(head); | |
deletedPrimes(head); | |
display(head); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment