0

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);

         }
    }
}
Tyler StMartin
  • 17
  • 1
  • 1
  • 5
  • 3
    `(n*)malloc(sizeof(n*))` ?? this is really wrong - it should at least be `sizeof(n)` – UnholySheep Mar 30 '17 at 15:11
  • This code was given to us by the professor. the only thing we have to do is instead of manually inputting data into the linked list, we have the program insert the prime numbers – Tyler StMartin Mar 30 '17 at 15:17
  • 3
    That doesn't make the code any less wrong - it doesn't allocate enough memory for the `struct`. – UnholySheep Mar 30 '17 at 15:24
  • So you should ask your professor why he hands out an example with a type name of `n` which is really a poor choice for a typename. – Jabberwocky Mar 30 '17 at 15:29
  • 1
    You should also tell your professor that you [shouldn't cast the return of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – UnholySheep Mar 30 '17 at 15:42
  • Is it really the source code provided by your professor ? In the `search()` function, after allocating the node `temp = (n*)malloc(sizeof(n*));`, the variable is overwritten by `temp = head;` ==> **Memory leak** !!! – J. Piquard Mar 30 '17 at 15:42
  • yes this is his code – Tyler StMartin Mar 30 '17 at 15:51
  • The mistake is reproduced systematically in both `display()`, `pop()` and `po$p()` functions, meaning a misunderstanding of node pointers. – J. Piquard Mar 30 '17 at 15:57
  • Remove the `malloc` calls from `display()`, `pop()`, and `po$p()` (is that a legal function name?) and the one that sets `temp` in `enqueue()`, and change the one in `insert()` to `malloc(sizeof(n))`. – Ian Abbott Mar 30 '17 at 16:56
  • Also your `po$p()` function doesn't work when the list length is less than 2, and it doesn't `free()` the node it removed. – Ian Abbott Mar 30 '17 at 17:01
  • 1
    `temp = malloc(sizeof *temp);` is the most accident-proof way. (there is only one name involved!!!11!) – joop Mar 30 '17 at 17:10

0 Answers0