2016-10-18 1 views
1

codechef에서 시도했지만 런타임 오류가 발생했습니다.내 프로그램이 예기치 않게 작동을 멈 춥니 다.

코드 블럭에서 프로그램은 첫 번째 입력을 요구 한 다음 프로그램이 올바르게 작동하지 않는다고 말합니다.

그리고 런타임 오류가 발생하지 않도록하는 방법은 무엇입니까?

#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"); 
} 
+4

'head'는 항상 NULL입니다. 잘못된 것입니다. –

+1

안녕하세요, 무엇입니까'void Insert (x)'????? 유형은 어디 있습니까? –

+0

코드를 디버깅하려고 했습니까? –

답변

1

목록의 머리글은 항상 NULL입니다. 수정하여 완료

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; 
     } 
} 
+1

답변의 코드 형식이 올바르게 지정되어야합니다. 질문을 수정하십시오. –

+0

@MichaelWalz 내게 말해줘 그렇지 않으면 어디서나 이런 식으로 의견을 나누기위한 링크를 줄 수는 없다. – Anjaneyulu

+0

[this] (https://en.wikipedia.org/wiki/Indent_style#Allman_style)을 읽으시오. –

관련 문제