2014-12-13 71 views
-3

我無法運行此代碼以提供兩個輸入。它在運行時突然停止。代碼如下。請幫我修復它。代碼在循環的第二次運行中停止

程序返回-1073741819(0xC0000005) 按任意鍵繼續。是錯誤消息

#include <stdio.h> 
#include<malloc.h> 
#include<conio.h> 
struct coordinates 
{ 
    int x; 
    int y; 
    struct coordinates *link; 
}; 
void append(struct coordinates **q,int xx , int yy) 
{ 
    struct coordinates *r,*s; 

    if(*q == NULL) 
    { 
     r = (struct coordinates *)malloc(sizeof(struct coordinates)); 
     r->x=xx; 
     r->y=yy; 
     *q=r; 
    } 

    else 
    { 
     r=*q; 
     while(r->link != NULL) 
     r= r->link; 

     s=(struct coordinates *)malloc(sizeof(struct coordinates)); 
     s->x=xx; 
     s->y=yy; 
     s->link=NULL; 

     r->link=s; 

    } 
} 
void display(struct coordinates *temp) 
{ 
    while (temp != NULL) 
    { 
     printf("x coordinate is %d ,Y coordinate is %d",temp->x,temp->y); 
     temp=temp->link; 
    } 
} 
int main() 
{ 
    struct coordinates *start; 
    start=NULL; 

    char name; 
    int xxx,yyy; 

    while(1) 
    { 
     printf("If you want to continue input loop press y \n"); 
     scanf(" %c", &name); 
     if (name == 'y') 
     { 
      printf("enter x coordinate of element \n"); 
      scanf("%d",&xxx); 
      printf("%d\n",xxx); 

      printf("enter y coordinate of element \n"); 
      scanf("%d",&yyy); 
      printf("%d\n",yyy); 

      append(&start,xxx,yyy); 

     } 
     else 
     { 
      printf("You have exited input loop \n"); 
      break; 
     } 

    } 
    display(start); 
} 

回答

3

使它

r->x = xx; 
r->y = yy; 
r->link = NULL; 

在附加的第一分支,否則r->link具有未指定的值(不太可能NULL)在列表的末尾的第二陣子。

相關問題