c - If 链表中的语句未按预期工作

标签 c if-statement struct linked-list

所以目前我在 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 block 中包含多个指令,则必须创建一个带有大括号的 block 。

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

关于c - If 链表中的语句未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35642268/

相关文章:

c - 关于C中的文件输入和输出

c - 打印出选定数字的字符串问题

php - 如何在 php 代码中的 css 类上应用 if-else?

matlab - MATLAB 中的条件 "Or"语句

c++ - 如何计算下面的struct占用的内存空间?

CS50 PSET3 恢复段错误,无法获取输出 JPEG 的代码

c - C中删除方阵中的一列和一行

php - 多条件 IF 语句

c++ - 在不影响原始变量的情况下删除对变量的引用

c - 数组损失值