C 这有什么问题?这段代码没有在链表的开头插入元素
在开头添加元素的链表 我想在开始时添加,但它只接受第一个元素,然后 震颤 它不接受其他元素while循环有什么问题C 这有什么问题?这段代码没有在链表的开头插入元素,c,C,在开头添加元素的链表 我想在开始时添加,但它只接受第一个元素,然后 震颤 它不接受其他元素while循环有什么问题 #包括 类型定义结构节点\u类型{ int data;结构节点类型*next; }节点; typedef节点*列表; void main(){ 列表头,温度;整数n;字符ch; head=NULL; printf(“\n输入数据:(是/否):”; scanf(“%c”和“ch”); while(ch='y'| ch='y'){ printf(“\n输入元素:”); scanf(“%
#包括
类型定义结构节点\u类型{
int data;结构节点类型*next;
}节点;
typedef节点*列表;
void main(){
列表头,温度;整数n;字符ch;
head=NULL;
printf(“\n输入数据:(是/否):”;
scanf(“%c”和“ch”);
while(ch='y'| ch='y'){
printf(“\n输入元素:”);
scanf(“%d”和“&n”);
temp=(list)malloc(sizeof(node));
温度->数据=n;
温度->下一步=头部;
压头=温度;
printf(“\n输入更多数据:”);
scanf(“%c”和“ch”);
}
温度=水头;
while(temp!=NULL){
printf(“%d”,临时->数据);
温度=温度->下一步;
}
}
您应该改变阅读回答的方式:
scanf(" %c", &ch);
在格式字符串中添加空格将指示scanf
跳过任何空白字符,包括使用fflush(stdin)试图清除的挂起换行符代码>,它调用未定义的行为
同时检查scanf()
的返回值:它应该是1
,否则,转换不成功。对于符合C标准的无参数函数main的启动器,应声明如下
int main( void )
变量ch
的函数scanf
调用的格式字符串必须如下所示
scanf( " %c", &ch );
^^^^^^
printf( "\nEnter the data:(y/n): " );
while ( scanf( " %c", &ch ) == 1 && ( ch == 'y' || ch == 'Y' ) )
{
printf( "\nEnter Element: " );
scanf( "%d", &n );
temp = (list) malloc( sizeof( node ) );
if ( temp != NULL )
{
temp->data = n;
temp->next = head;
head = temp;
}
printf( "\nEnter more data: " );
}
在这种情况下,将跳过包括新行字符在内的空白字符。否则,函数将返回控制字符。您还应该检查流是否有结尾
while循环可以看起来像
scanf( " %c", &ch );
^^^^^^
printf( "\nEnter the data:(y/n): " );
while ( scanf( " %c", &ch ) == 1 && ( ch == 'y' || ch == 'Y' ) )
{
printf( "\nEnter Element: " );
scanf( "%d", &n );
temp = (list) malloc( sizeof( node ) );
if ( temp != NULL )
{
temp->data = n;
temp->next = head;
head = temp;
}
printf( "\nEnter more data: " );
}
这是一段稍加修改的代码
#include <stdio.h>
#include <stdlib.h>
typedef struct node_type {
int data;
struct node_type *next;
}list;
int main() {
list *head, *temp; int n; char ch;
head = NULL;
printf("\n Enter the data:(y/n):");
scanf("%c", &ch);
while (ch == 'y' || ch == 'Y') {
printf("\n Enter Element:");
scanf("%d", &n);
// Inserting a node into linked list at beginning:
// 1. Create a node and initialize its values
// Make sure that the next field points to current head node
temp = (list*) malloc(sizeof(list));
temp->data = n;
temp->next = head;
// 2. Update the head pointer so that it points to newly created node
head = temp;
printf("\n Enter more data:");
scanf(" %c", &ch);
}
temp = head;
while (temp != NULL) {
printf("%d", temp->data);
temp = temp->next;
}
return 0;
}
#包括
#包括
类型定义结构节点\u类型{
int数据;
结构节点类型*下一步;
}名单;
int main(){
列表*标题,*温度;整数n;字符ch;
head=NULL;
printf(“\n输入数据:(是/否):”;
scanf(“%c”和“ch”);
while(ch='y'| ch='y'){
printf(“\n输入元素:”);
scanf(“%d”和“&n”);
//在开始处将节点插入到链表中:
//1.创建节点并初始化其值
//确保下一个字段指向当前头部节点
temp=(列表*)malloc(sizeof(列表));
温度->数据=n;
温度->下一步=头部;
//2.更新头部指针,使其指向新创建的节点
压头=温度;
printf(“\n输入更多数据:”);
scanf(“%c”和“ch”);
}
温度=水头;
while(temp!=NULL){
printf(“%d”,临时->数据);
温度=温度->下一步;
}
返回0;
}
编辑:我也做了与前面提到的@Vlad和@chqrlie相同的事情。NULL在stdio.h
中没有定义。您应该使用\define
宏或stddef.h
或stdlib.h
手动定义它。在清除输入缓冲区时,最好使用fflush(stdin)
(这将删除未扩展的错误,同时处理字符和整数上的scanf()
)。它是在stdio.h
中预定义的。请注意,执行fflush(stdin)
是未定义的行为。某些“标准”库将其添加为扩展,但尽可能避免。您进行了哪些调试?无法解析问题,因为缺少标点符号…叹气。我觉得这是…@CherubimAnand,这是有问题的,因为如果在空白(或EOF)后面有n个其他非空白字符然后scanf
将被阻止。您不应该根据评论和答案中的指示修复问题…这会使讨论不一致。正如我在回答中所发布的,删除fflush(stdin);
并将scanf(“%c”、&ch);
中的格式替换为scanf(“%c”、&ch)
注意%
在
中定义之前的空格,以及其他标准标题,如
、
、
、
和fflush(stdin)
调用未定义的行为。清除stdin
中的挂起字符可以使用:scanf(“%*[^\n]”;scanf(“%*c”);
代码未缩进。对head==NULL
的测试无效,else
分支可以正确处理所有情况。测试scanf(“%d”、&n)的返回值建议使用;
。不建议强制转换malloc()的返回值。