为什么这个 C 程序执行在通过 scanf() 输入时停止工作?
Why this C program execution stops working while inputting through scanf()?
这是一个简单的 C 程序,用于创建和显示单链接 -list.The creat() 函数在前一个节点之后创建一个新节点,将节点数据作为 parameter.The display()函数打印链表。此程序片段无法正常运行:
for(b=1;b<=5;b++) {
scanf("%d ",&a);
creat(a);
}
如果通过 scanf() 插入两个或三个值,则执行将停止。
这有什么问题吗?
如果您跳过 scanf() 并输入如下语句,它会起作用:
for(b=1;b<=5;b++) {
creat(7);
}
主要代码:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head=NULL;
typedef struct node Node;
void creat(int d);
void display();
int main()
{
int a,b;
printf("Input data to build a linked-list:\n");
for(b=1;b<=5;b++) {
scanf("%d ",&a); /*Error statement maybe*/
creat(a);
}
printf("The list is:-\n");
display();
return 0;
}
void creat(int d)
{
Node *new,*curr;
new=(Node *) malloc(sizeof(Node));
new->data=d;
new->next=NULL;
if(head==NULL)
{
head=new;
curr=new;
}
else
{
curr->next=new;
curr=new;
}
}
void display()
{
Node *p;
p=head;
while(p)
{
printf("%d--->",head->data);
p=p->next;
}
printf("NULL\n");
}
尝试在 %d 之后不使用 space - scanf 可能相当脆弱....
scanf("%d",&a);
实际上问题是由函数creat()
-
造成的
此函数中的 else
部分正在创建问题。应该是这样的-
else
{
curr=head;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new;
}
遍历到最后一个节点并添加新节点。
以及 scanf
。
scanf("%d ",&a); /*Error statement maybe*/
^Remove the space.
也在函数中void display()
while(p)
{
printf("%d--->",head->data);
p=p->next;
}
您正在打印 head->data
但它不会递增到下一个,而是 p
设置为 p->next
。因此,此函数不会打印整个 link 列表.
printf
应该是这个-
printf("%d--->",p->data);
} *head=NULL;
改为} *head=NULL, *curr;
scanf("%d ",&a);
改为scanf("%d",&a);
Node *new,*curr;
改为Node *new;
printf("%d--->", head->data);
改为printf("%d--->", p->data);
这是一个简单的 C 程序,用于创建和显示单链接 -list.The creat() 函数在前一个节点之后创建一个新节点,将节点数据作为 parameter.The display()函数打印链表。此程序片段无法正常运行:
for(b=1;b<=5;b++) {
scanf("%d ",&a);
creat(a);
}
如果通过 scanf() 插入两个或三个值,则执行将停止。 这有什么问题吗? 如果您跳过 scanf() 并输入如下语句,它会起作用:
for(b=1;b<=5;b++) {
creat(7);
}
主要代码:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
} *head=NULL;
typedef struct node Node;
void creat(int d);
void display();
int main()
{
int a,b;
printf("Input data to build a linked-list:\n");
for(b=1;b<=5;b++) {
scanf("%d ",&a); /*Error statement maybe*/
creat(a);
}
printf("The list is:-\n");
display();
return 0;
}
void creat(int d)
{
Node *new,*curr;
new=(Node *) malloc(sizeof(Node));
new->data=d;
new->next=NULL;
if(head==NULL)
{
head=new;
curr=new;
}
else
{
curr->next=new;
curr=new;
}
}
void display()
{
Node *p;
p=head;
while(p)
{
printf("%d--->",head->data);
p=p->next;
}
printf("NULL\n");
}
尝试在 %d 之后不使用 space - scanf 可能相当脆弱....
scanf("%d",&a);
实际上问题是由函数creat()
-
else
部分正在创建问题。应该是这样的-
else
{
curr=head;
while(curr->next!=NULL)
{
curr=curr->next;
}
curr->next=new;
}
遍历到最后一个节点并添加新节点。
以及 scanf
。
scanf("%d ",&a); /*Error statement maybe*/
^Remove the space.
也在函数中void display()
while(p)
{
printf("%d--->",head->data);
p=p->next;
}
您正在打印 head->data
但它不会递增到下一个,而是 p
设置为 p->next
。因此,此函数不会打印整个 link 列表.
printf
应该是这个-
printf("%d--->",p->data);
} *head=NULL;
改为} *head=NULL, *curr;
scanf("%d ",&a);
改为scanf("%d",&a);
Node *new,*curr;
改为Node *new;
printf("%d--->", head->data);
改为printf("%d--->", p->data);