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

C程序在显示我的链表时随机崩溃,c,linked-list,C,Linked List,我的代码应该能找到1879到9987之间的所有素数。除了我去显示链表的时候,一切似乎都在按预期工作。它将大致显示前6个元素(每次运行程序时显示的距离是随机的)。正如您可以通过运行代码看到的,它正在打印所有需要的素数,但它并没有存储所有素数。感谢您的帮助。我正在使用devc作为我的编译器 #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *link

我的代码应该能找到1879到9987之间的所有素数。除了我去显示链表的时候,一切似乎都在按预期工作。它将大致显示前6个元素(每次运行程序时显示的距离是随机的)。正如您可以通过运行代码看到的,它正在打印所有需要的素数,但它并没有存储所有素数。感谢您的帮助。我正在使用devc作为我的编译器

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

         }
    }
}
#包括
#包括
类型定义结构节点
{
int数据;
结构节点*链接;
}n;
n*head=NULL;
空白插入(int);
void display();
无效擦除();
无效排队(int);
void pop();
作废po$p();
无效检索(int);
无效优先权();
内部主(空)
{
int num、count、choice;
isprime();
而(1)
{
printf(“\n 1.显示>”;
printf(“\n 2.删除列表>”;
printf(“\n 3.在末尾插入>”;
printf(“\n 4.从堆栈中弹出>”;
printf(“\n 5.在列表后面删除>”;
printf(“\n 6.搜索特定节点>”;
printf(“\n 7.退出>”;
printf(“\n输入您的选择>”);
scanf(“%d”,选择(&C);
开关(选择)
{
案例1:
显示();
打破
案例2:
擦除();
打破
案例3:
printf(“在末尾输入要插入的数据>”;
scanf(“%d”和&num);
排队(num);
打破
案例4:
pop();
打破
案例5:
po$p();
打破
案例6:
printf(“输入要搜索的节点>”;
scanf(“%d”和&num);
搜索(num);
打破
案例7:
出口(0);
}
}
返回0;
}
空白插入(整数X)
{
n*温度;
temp=(n*)malloc(sizeof(n*));
温度->数据=X;
temp->link=NULL;
if(head==NULL)
压头=温度;
其他的
{
温度->链接=头部;
压头=温度;
}
}
无效显示()
{
n*温度;
temp=(n*)malloc(sizeof(n*));
if(head==NULL)
printf(“\n没有列表”);
其他的
{
温度=水头;
printf(“\n head->”);
while(temp!=NULL)
{
printf(“%d->”,临时->数据);
温度=温度->链接;
}
printf(“空”);
}
}
无效擦除()
{
head=NULL;
}
无效排队(整数X)
{
n*temp,*newnode;
newnode=(n*)malloc(sizeof(n*));
newnode->data=X;
newnode->link=NULL;
temp=(n*)malloc(sizeof(n*));
if(head==NULL)
头=新节点;
其他的
{
温度=水头;
while(临时->链接!=NULL)
温度=温度->链接;
temp->link=newnode;
}
}
void pop()
{
n*温度;
temp=(n*)malloc(sizeof(n*));
if(head==NULL)
{
printf(“\n没有要弹出的内容”);
}
其他的
{
温度=水头;
printf(“\n弹出的元素为%d”,临时->数据);
头=头->链接;
免费(临时);
}
}
作废po$p()
{
n*温度;
temp=(n*)malloc(sizeof(n*));
if(head==NULL)
{
printf(“\n订单$p”无任何内容);
}
其他的
{
温度=水头;
而(临时->链接->链接!=NULL)
温度=温度->链接;
printf(“\n元素po$ped为%d”,临时->链接->数据);
temp->link=NULL;
}
}
无效搜索(整数X)
{
n*温度;
temp=(n*)malloc(sizeof(n*));
if(head==NULL)
{
printf(“\n无需搜索”);
}
其他的
{
温度=水头;
而(临时->数据!=X&&temp->链接!=NULL)
温度=温度->链接;
如果(临时->数据==X)
printf(“\n列表中的项目”);
否则如果(临时->链接==NULL)
printf(“\n项不在列表中”);
}
}
void isprime(){
整数计数,i,x;

对于(x=1879;x
(n*)malloc(sizeof(n*))
?这确实是错误的-它至少应该是
sizeof(n)
这段代码是教授给我们的。我们唯一要做的不是手动将数据输入到链表中,而是让程序插入素数,这不会减少代码的错误-它没有为
结构分配足够的内存。
。你还应该告诉教授你
temp=malloc(sizeof*temp);
是最能防止事故发生的方法。(只涉及一个名称!!!11!)