2016-02-26 140 views
-1

因此,目前我在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(); 
} 

回答

3

缺少括号。

这就是为什么我是“总是添加大括号”的辩护人。

编辑为“解决方案”。

当前的代码是:

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 (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); 
} 
+1

您能否用解决方案示例详细说明您的答案。 –

+0

添加了详细的答案。 –

+0

多数民众赞成在伟大.... –

1

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

If如果没有大括号,语句只考虑if语句之后的立即下一条语句。

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

它只考虑return语句if语句为true。

如果陈述是错误的它转到下一条语句;

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 
}