显示我的链表时,C程序会随机崩溃

时间:2017-03-30 15:07:43

标签: c linked-list

我的代码应该找到1879年到9987年之间的所有素数。除了当我去显示链表时,一切似乎都按预期工作。它将粗略显示前6个元素(每次运行程序时它将随机显示多少)。正如您可以通过运行代码所看到的那样打印所需的所有素数,它并不是全部存储它们。任何帮助表示赞赏。我正在使用Dev C作为我的编译器。

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

         }
    }
}

0 个答案:

没有答案