C 链接列表中的If语句未按预期工作
因此,目前我尝试测试的C 链接列表中的If语句未按预期工作,c,if-statement,struct,linked-list,C,If Statement,Struct,Linked List,因此,目前我尝试测试的GetNth函数中有一个if语句。但是当我插入一个printf函数时,它让我注意到,即使不满足条件,它也会通过if语句,然而,当我删除printf语句时,程序会完美地工作。如有任何解释,将不胜感激 注意!这不是我的代码,我试图研究链表,并试图改变周围的代码学习 守则: #include <stdio.h> #include <stdlib.h> #include <assert.h> /* Link list node */ struc
GetNth
函数中有一个if
语句。但是当我插入一个printf
函数时,它让我注意到,即使不满足条件,它也会通过if
语句,然而,当我删除printf
语句时,程序会完美地工作。如有任何解释,将不胜感激
注意!这不是我的代码,我试图研究链表,并试图改变周围的代码学习
守则:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/* Link list node */
struct node
{
int data;
struct node* next;
};
/* Given a reference (pointer to pointer) to the head
of a list and an int, push a new node on the front
of the list. */
void push(struct node** head_ref, int new_data)
{
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data */
new_node->data = new_data;
/* link the old list off the new node */
new_node->next = (*head_ref);
/* move the head to point to the new node */
(*head_ref) = new_node;
}
/* Takes head pointer of the linked list and index
as arguments and return data at index*/
int GetNth(struct node* head, int index)
{
struct node* current = head;
int count = 0; /* the index of the node we're currently
looking at */
int a;
while (current != NULL)
{
if (count == index)
return(current->data);
a = current->data;
printf("\n Testing If in linked list, should bring same desired value which is 4 %d \n ",a);
count++;
current = current->next;
}
/* if we get to this line, the caller was asking
for a non-existent element so we assert fail */
assert(0);
}
/* Drier program to test above function*/
int main()
{
/* Start with the empty list */
struct node* head = NULL;
/* Use push() to construct below list
1->12->1->4->1 */
push(&head, 1);
push(&head, 4);
push(&head, 1);
push(&head, 12);
push(&head, 1);
if (head != NULL)
{
}
/* Check the count function */
printf("Element at index 3 is %d", GetNth(head, 3));
getchar();
}
#包括
#包括
#包括
/*链接列表节点*/
结构节点
{
int数据;
结构节点*下一步;
};
/*给定指向头部的引用(指针到指针)
对于列表和int,在前面推一个新节点
在名单上*/
无效推送(结构节点**head\u ref,int new\u数据)
{
/*分配节点*/
结构节点*新节点=
(结构节点*)malloc(sizeof(结构节点));
/*输入数据*/
新建_节点->数据=新建_数据;
/*将旧列表链接到新节点*/
新建节点->下一步=(*head\u ref);
/*移动头部以指向新节点*/
(*head_ref)=新节点;
}
/*获取链表和索引的头指针
作为参数并在索引处返回数据*/
int GetNth(结构节点*头,int索引)
{
结构节点*当前=头部;
int count=0;/*当前正在处理的节点的索引
看着*/
INTA;
while(当前!=NULL)
{
如果(计数==索引)
返回(当前->数据);
a=当前->数据;
printf(“\n如果在链表中进行测试,则应得到相同的期望值,即4%d\n”,a);
计数++;
当前=当前->下一步;
}
/*如果我们打到这个电话,打电话的人会问
对于不存在的元素,因此我们断言失败*/
断言(0);
}
/*用于测试上述功能的干燥器程序*/
int main()
{
/*从空列表开始*/
结构节点*head=NULL;
/*使用push()构造下面的列表
1->12->1->4->1 */
推送(和头部,1);
推压(和头部,4);
推送(和头部,1);
推压(头,12);
推送(和头部,1);
if(head!=NULL)
{
}
/*检查计数功能*/
printf(“索引3处的元素为%d”,GetNth(head,3));
getchar();
}
缺少大括号
这就是为什么我是“总是加牙套”的捍卫者
编辑“解决方案”
目前的代码是:
while (current != NULL)
{
if (count == index)
return(current->data);
a = current->data;
printf("\n Testing If in linked list, should bring same desired value which is 4 %d \n ",a);
count++;
current = current->next;
}
如果没有大括号,if语句只适用于下一条指令,即return(current->data)代码>
如果要在If块中包含多条指令,则必须创建一个带大括号的块
if (count == index)
{
return(current->data);
a = current->data;
printf("\n Testing If in linked list, should bring same desired value which is 4 %d \n ",a);
}
但是,您以返回指令开始,因此将永远不会执行以下两行
在退货前重新排列打印说明
if (count == index)
{
a = current->data;
printf("\n Testing If in linked list, should bring same desired value which is 4 %d \n ",a);
return(current->data);
}
首先,即使条件为false,它也不会进入if语句
If
语句只考虑If语句之后的下一个语句(如果没有大括号)
if (count == index)
return(current->data);
若语句为true,它只考虑返回语句
如果语句为false,则转到后面的下一个语句代码>
i、 e
这就是为什么你觉得if语句不起作用
如果需要在If循环中使用printf
,则需要对If循环中的多个语句使用语法。i、 e通过花括号
if (condition)
{
//statement
//statement
}
你能用解决方案的例子详细说明你的答案吗?补充了一个详细的答案。我同意这个诊断。我不同意这种补救方法——我更喜欢的补救方法是思考,记住C不是Python。
if (condition)
{
//statement
//statement
}