错误 (Id) 返回 1 个退出状态

Error(Id) returned 1 exit status

本文关键字:退出 状态 返回 Id 错误      更新时间:2023-10-16

我试图遍历单向链表的程序,出现错误,似乎这样做,而循环没有正确应用,请检查

该程序在下面,我试图使用do和指针进行遍历。

 #include<stdio.h>
#include<stdlib.h>
int main()
{
    char ch;
    struct node
    {
        int info;
        struct node *next;
    };
    typedef struct node node;
    node *start, *ptr, *ne;
    ptr=NULL;
    int count=0;
    do
    {
    ne=(node*) malloc(sizeof(node));
    printf("nEnter Data: ");
    scanf("%d",&ne->info);
    if(ptr!=NULL)
    {
    ptr->next=ne;
    ptr=ptr->next;
    }
    else {
    start=ne;
    ptr=ne;
    }
    printf("nDo you wish to continue?n ");
    scanf("%c",&ch);
      }while(ch=='y'|| ch=='Y');
      ptr->next=NULL;
      printf("The linked list is: ");
      ptr=start;
      while(ptr!=NULL)
      {
      printf("t%d",&ptr->info);
      ptr=ptr->next;
      count++;
      }
      printf("nTotal number of elements: %d",count);
    return(0);     

}

更改

printf("t%d",&ptr->info);

 printf("t%d",ptr->info);

另外,我不建议在typedef struct node node;中使用相同的名称"节点"但这仍然不是问题。

编辑:

我还发现您的代码存在问题:

改变

printf("nDo you wish to continue?n ");
scanf("%c",&ch);

printf("nDo you wish to continue?n ");
getchar();
scanf("%c",&ch);

您也可以将其更改为

printf("nDo you wish to continue?n ");
scanf(" %c",&ch); //add a space before %c

为什么这会导致错误?

在程序中,当您尝试使用 'printf((' 打印任何内容时,缓冲区中会留下一个空格,因此我们必须读取它并忽略它,否则 ch 将被分配空格。要读取空格并忽略,如上所述,我们有两个选择。尝试任何一个。