This is a C program please copy and paste it dont write in paper so i can unders
ID: 3801555 • Letter: T
Question
This is a C program please copy and paste it dont write in paper so i can understand it.
1.) Create a loop which generates numbers between 1879 and 9987 and detect whether it is aprime number or not,and then insert it in a linked list and then implement the following functions
a.) push b.) pop c.) dequeue d.)displayinreverse e).search(return the index of the location)
2.) Create a linked list with all the courses you took (science, java, c++, phyc, descrete math) and implement all the functions which I have given in the previous question(Make sure your name is in the list)
Explanation / Answer
#include<stdlib.h>
#include<stdio.h>
#include<stdbool.h>
typedef struct node
{
int num;
struct node *next;
}node;
bool check_prime(int n)
{
int i;
bool flag = true;
for(i = 2; i <= n/2; ++i)
{
// condition for nonprime number
if(n % i == 0)
{
flag = false;
break;
}
}
return flag;
}
void create_list(node **head, int number)
{
node *travel ,*temp;
temp = (node*)malloc(sizeof(node));
temp->num = number;
temp->next = NULL;
if(*head == NULL)
{
*head = temp;
return;
}
travel = *head;
while(travel != NULL)
{
travel = travel->next;
}
travel = temp;
}
void print_list(node **head)
{
node *travel = NULL;
travel = *head;
while(travel != NULL)
{
printf("%d ", travel->num);
travel = travel->next;
}
}
int main()
{
int i;
node *head;
bool flag = false;
head = NULL;
for(i = 1879; i <= 9987; i++)
{
flag = check_prime(i);
if(flag)
{
//printf("%d ", i);
create_list(&head, i);
}
}
printf("Prime Numbers in linked list are : ");
print_list(&head);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.