my code is supposed to find all the prime numbers between 1879 and 9987. everything seems to be working as intended except for when i go to display the linked list. it will display the first 6 elements roughly (it is random how far it will display each time the program is run). as you can see by running the code that it is printing all of the prime numbers required, it just doesn't store them all. any help is appreciated. I am using Dev C for my compiler.
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *link;
}n;
n *head = NULL;
void insert(int);
void display();
void erase();
void enqueue(int);
void pop();
void po$p();
void search(int);
void isprime();
int main(void)
{
int num,count, choice;
isprime();
while(1)
{
printf("\n 1. To display>");
printf("\n 2. To Delete the list>");
printf("\n 3. To insert at the end>");
printf("\n 4. Pop out of the stack>");
printf("\n 5. To delete at the back of the list>");
printf("\n 6. To search a particular node>");
printf("\n 7. To exit>");
printf("\n Enter your choice>");
scanf("%d",&choice);
switch(choice)
{
case 1:
display();
break;
case 2:
erase();
break;
case 3:
printf("Enter the data you want to insert at the end>");
scanf("%d",&num);
enqueue(num);
break;
case 4:
pop();
break;
case 5:
po$p();
break;
case 6:
printf("Enter the node you want to search>");
scanf("%d",&num);
search(num);
break;
case 7:
exit(0);
}
}
return 0;
}
void insert(int X)
{
n *temp;
temp = (n*)malloc(sizeof(n*));
temp->data = X;
temp->link = NULL;
if(head == NULL)
head = temp;
else
{
temp->link = head;
head = temp;
}
}
void display()
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
printf("\n There is no list");
else
{
temp = head;
printf("\n head->");
while(temp != NULL)
{
printf("%d->",temp->data);
temp = temp->link;
}
printf("NULL");
}
}
void erase()
{
head = NULL;
}
void enqueue(int X)
{
n *temp,*newnode;
newnode = (n*)malloc(sizeof(n*));
newnode->data = X;
newnode->link = NULL;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
head = newnode;
else
{
temp = head;
while(temp->link != NULL)
temp = temp->link;
temp->link = newnode;
}
}
void pop()
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
{
printf("\n nothing to pop");
}
else
{
temp = head;
printf("\n element popped is %d",temp->data);
head = head->link;
free(temp);
}
}
void po$p()
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
{
printf("\n nothing to po$p");
}
else
{
temp = head;
while(temp->link->link != NULL)
temp = temp->link;
printf("\n Element po$ped is %d",temp->link->data);
temp->link = NULL;
}
}
void search(int X)
{
n *temp;
temp = (n*)malloc(sizeof(n*));
if(head == NULL)
{
printf("\n nothing to search");
}
else
{
temp = head;
while(temp->data != X && temp->link != NULL)
temp = temp->link;
if(temp->data == X)
printf("\n item in the list");
else if(temp->link == NULL)
printf("\n item is not in the list");
}
}
void isprime(){
int count,i,x;
for(x = 1879;x<=9987;x++){
count = 0;
for(i=2;i<=x/2;i++){
if(x%i==0){
count++;
break;
}
}
if(count==0 && x!= 1){
insert(x);
printf("%d",x);
}
}
}