Valgrind显示" valgrind:地址0x40152b7处无法识别的指令 - 非法指令(核心转储)",

时间:2015-06-22 07:07:51

标签: c linked-list valgrind

我的代码是关于在链接列表中的有序插入&我不明白模糊性在哪里。 但是当我运行该程序时,SEEMS工作正常。这是编译器的错还是我的错?我是新手,所以请帮助我。

#include<stdio.h>
#include<stdlib.h>
struct node{
int i;
struct node *link;
};
int main()
{
struct node *head,*p,*temp,*q;
int data;
char c;
printf("Do you want to enter data? Y/N");
scanf(" %c",&c);
fflush(stdin);
if((c=='Y')||(c=='y'))
 {
   head=(struct node*)malloc(sizeof(struct node));
   printf("Enter the data: ");
   scanf(" %d",&data);
   head->i=data;
   head->link=NULL;
 }
printf("Do you wan to enter more data? Y/N");
scanf(" %c",&c);
fflush(stdin);
q=head;
while((c=='y')||(c=='Y'))
 {
   printf("Enter the data: ");
   scanf(" %d",&data);
   temp=(struct node *)malloc(sizeof(struct node));
   temp->i=data;
   temp->link=NULL;
   if(q->i>=temp->i)
      {
        temp->link=q;
        head=temp;
      }
   else
      {
        while((q->link!=NULL)&&(temp->i>q->link->i))
           {
             q=q->link;
           }
        temp->link=q->link;
        q->link=temp;
      }
              q=head;
     fflush(stdin);
     printf("Do you want to enter more data? Y/N");
     scanf(" %c",&c);
 }
    p=head;
  while(p!=NULL)
     {
       printf("%d ",p->i);
       p=p->link;
     }
  return 0;
 }

0 个答案:

没有答案