2016-10-18 7 views
1

コーデックを試しましたが、ランタイムエラーが発生しました。私のプログラムが予期せず動作を停止します

コードブロックでは、プログラムは最初の入力を要求してから、プログラムが正しく機能しなくなったというメッセージが表示されます。

また、ランタイムエラーが発生しないようにする方法を教えてください。

#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

[こちら]([https://en.wikipedia.org/wiki/Indent_style#Allman_style) –

関連する問題