2016-02-26 149 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 
}