C语言 如果链表中的语句未按预期工作



所以目前我在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();
}

缺少大括号。

这就是为什么我是"始终添加大括号"的捍卫者。

编辑"解决方案"。

当前代码为:

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);
}

但是,您从返回指令开始,因此以下 2 行将永远不会执行。

重新排序您的说明,以便在退货前打印。

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);
}

首先,即使条件为假,它也不会进入if statement

如果没有大括号,If语句仅考虑 if 语句之后的紧接下一条语句。

if (count == index)
            return(current->data);

如果语句为真,则仅考虑返回语句。

如果语句为 false,则转到 ; 之后的下一条语句 即

 a = current->data;
            printf("n Testing If in linked list, should bring same desired value which is 4 %d n ",a);

这就是你觉得如果语句不起作用的原因。

如果需要在 if 循环中使用

printf则需要在 if 循环中使用多个语句的语法。 即通过大括号

if (condition)
{
   //statement
   //statement
}

相关内容

  • 没有找到相关文章

最新更新