节目里我试图遍历单链表,得到错误,好像做,while循环不正确应用,请错误(Id)的返回1个退出状态
该方案低于在哪里我试着用while和pointers来遍历。
#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);
}
如果链接器返回一个错误(您没有复制到该文章中),那么您的程序将无法运行。所以问题不在于“应用”循环。 – StoryTeller
当询问构建错误时,* always *在问题主体中包含* complete *错误输出。完整,包括可能的信息说明,无需编辑,*作为(复制粘贴)文本*。 –