简体   繁体   English

我的程序意外停止工作

[英]My program unexpectedly stops working

I tried on codechef ide but I got a runtime error.我尝试了 codechef ide,但出现运行时错误。

On Codeblocks the program asks for the first input and then just says the program has stopped working correctly.在代码块上,程序要求第一个输入,然后只是说程序已停止正常工作。

And how to make sure I don't get any runtime errors?以及如何确保我没有收到任何运行时错误?

#include <stdio.h>
struct node
{
    int data;
    struct node* next;
};

struct node* head;

int main()
{
    head=NULL;
    int i,n,x;

    printf("\nEnter the number of nodes");
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        printf("\nEnter the value");
        scanf("%d",&x);
        Insert(x);
    }

    printf("\nHow many numbers?\n");
    Print();
    return 0;
}

void Insert(x)  /*To create new node in a linked list*/
{
    struct node* temp=(struct node*)malloc(sizeof(struct node));

    temp->data=x;
    temp->next=NULL;

    struct node* temp1;
    temp1=head;

    while(temp1->next!=NULL)
    {
        temp1=temp1->next;
    }

    temp1->next=temp;
}

void Print()  /*to print the linked list*/
{
    struct node* temp;
    temp=head;
    printf("\nThe list is");

    while(temp!=NULL)
    {
        printf("\n%d",temp->data);
        temp=temp->next;
    }

    printf("\n");
}

head of the list is always NULL.列表的头部始终为 NULL。 Make this correction and done进行此更正并完成

 void Insert(x)
 {
        struct node* temp=(struct node*)malloc(sizeof(struct node));
        temp->data=x;
        temp->next=NULL;
        struct node* temp1;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            temp1=head;
            while(temp1->next!=NULL)
            {
                temp1=temp1->next;
            }
            temp1->next=temp;
        }
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM