C 为什么我的while循环条件不工作?
无论输入什么字符,此程序都会要求输入数字。那么问题就在while循环中了 为什么我的while循环条件不工作?正在检查的临时值不等于NULLC 为什么我的while循环条件不工作?,c,while-loop,do-while,C,While Loop,Do While,无论输入什么字符,此程序都会要求输入数字。那么问题就在while循环中了 为什么我的while循环条件不工作?正在检查的临时值不等于NULL #include <stdio.h> #include <malloc.h> #include <stdlib.h> typedef struct node { int num; struct node *ptr; } nod; nod *head = NULL; void insert() {
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef struct node
{
int num;
struct node *ptr;
} nod;
nod *head = NULL;
void insert()
{
int n;
nod *temp = (struct node*) malloc(sizeof(struct node));
printf("Enter nnumber \n");
scanf("%d", &n);
temp->num = n;
temp->ptr = NULL;
if (head == NULL)
{
head = temp;
}
else
{
temp->ptr = head;
head = temp;
}
}
void display()
{
nod* temp;
temp = head;
while (temp != NULL)
{
printf(" --> %d", temp->num);
temp = temp->ptr;
}
}
int main()
{
char ch;
do
{
insert();
display();
char ch;
printf("\n enter more data ? (y/n)");
scanf("\n %c", &ch);
}
while (ch != 'n');
return 0;
}
#包括
#包括
#包括
类型定义结构节点
{
int-num;
结构节点*ptr;
}点头;
nod*head=NULL;
空白插入()
{
int n;
nod*temp=(结构节点*)malloc(sizeof(结构节点));
printf(“输入nnumber\n”);
scanf(“%d”和“&n”);
温度->数值=n;
temp->ptr=NULL;
if(head==NULL)
{
压头=温度;
}
其他的
{
温度->ptr=压头;
压头=温度;
}
}
无效显示()
{
nod*temp;
温度=水头;
while(temp!=NULL)
{
printf(“-->%d”,temp->num);
温度=温度->ptr;
}
}
int main()
{
char ch;
做
{
插入();
显示();
char ch;
printf(“\n输入更多数据?(y/n)”);
scanf(“\n%c”,&ch);
}
while(ch!=“n”);
返回0;
}
提前感谢为什么在
循环中重新声明
移除char ch
内部循环并修复您的问题。像
int main()
{
char ch;
do
{
insert();
display();
printf("enter more data ? (y/n)\n");
scanf(" %c", &ch);
}
while (ch != 'n');
return 0;
}
您好,这里更欢迎使用格式正确的问题:)我看到了潜在的问题,但首先编辑您的问题,使其看起来可读。旁注:您的所有函数定义必须是…(void)
请提供适当的缩进。难以阅读的代码不会吸引很多人来帮助删除字符
您在main()
中的do while循环中声明。从那开始。不相关的,if(head==NULL)…else…
内容对于前推链接列表来说毫无价值。只需分配新节点并填充其成员,将其ptr
成员设置为head
,然后将head
设置为新节点指针值。如果该功能中需要其他功能,则不需要。