簡體   English   中英

錯誤遍歷和打印C中的單鏈表

[英]Error traversing and printing singly linked list in C

迷失了Noob,嘗試逐行讀取文件,即“一個”,“兩個”,“三個”,然后將其添加到有序鏈接列表中(我相信我可以使用)。 但是,我無法弄清楚我的traverse_and_print列表函數的語法/邏輯(並且不理解*(去這里並獲取值),&(獲取地址)和->那樣。我的大部分工作代碼都在在https://repl.it/@MichaelB4/DeafeningTreasuredMathematics上進行 repl.it

// A complete working C program to demonstrate all insertion methods 
// from https://www.geeksforgeeks.org/linked-list-set-2- inserting-a-node/

#include <stdio.h> 
#include <stdlib.h> 
#include<string.h>

// Create structure for a linked list node 

struct Node 
{ 
    const char *data; 
    struct Node *next; 
}; 

struct Node *head;

// Given a reference (pointer to pointer) to the head of a list and a char*, appends a new node at the end
void append(struct Node** head_ref, const char *new_data) 
{ 
    // 1. allocate node
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node)); 

    struct Node *last = *head_ref;  // used in step 5

    // 2. put in the data
    new_node-> data  = new_data; 

    // 3. Set new_node.next to Null, as it will be inserted at tail of list 
    new_node -> next = NULL;

    // 4. If the Linked List is empty, then make the new node as head
    if (*head_ref == NULL) 
    { 
        *head_ref = new_node;
        //printf("head\n");
        printf("%s", new_node -> data); 
        return; 
    } 

    // 5. Else traverse till the last node

    while (last -> next != NULL)  
        last = last -> next;
        printf("%s", new_node -> data); 

    // 6. Change the next of last node, have last node point to one just inserted, the new node at the end of the list, tail 

    last -> next = new_node; 
    return; 
} 

void traverse_and_printList(head){
    struct Node* current = (struct Node*) malloc(sizeof(struct Node));
    current = head;
    while (head -> next != NULL)
        printf("%s", current -> data);
        current -> next = current;

}

/* Driver program to test above functions*/
int main() 
{ 
    // set up a file point to File to be opened
    FILE* fp;
    // holds contents of each line/word 
    char buffer[255]; 

    fp = fopen("words.txt", "r");
    if (fp == NULL)
    {
        fprintf(stderr, "Could not open infile"); 
        return 2;
    }

    /* create an empty node */
    struct Node* head = NULL; 

    int counter = 0;
    char *head_value[255]; 

    while(fgets(buffer, 255, (FILE*) fp)){
        //printf("%s", buffer);
        append(&head, buffer);
    }

    fclose(fp);
    traverse_and_printList(head);
    printf("\n");
    return 0; 
}

您對current指針的作用感到困惑。

首先,您不需要為其分配內存。 您不是要存儲任何新內容。 current指針只是一個值,可以幫助您移動列表中的項目。

其次,您不應該修改列表的數據。 current->next = current是偽造的。 它修改列表創建一個循環。 不好。

第三,縮進提示您的while循環包含兩個單獨的語句,但是它們周圍沒有塊作用域( { ... } )。 因此,只有第一條語句將成為循環的一部分。

最后一點是風格。 請不要在->加上空格。 盡管編譯器不在乎,但它使您的代碼很難被人類閱讀。

正確遍歷列表很簡單:

for(struct Node* current = head; current != NULL; current = current->next)
{
    printf("%s\n", current->data);
}

在我看來,您的“遍歷和打印”功能有問題。 你有:

void traverse_and_printList(head){
    struct Node* current = (struct Node*) malloc(sizeof(struct Node));
    current = head;
    while (head -> next != NULL)
        printf("%s", current -> data);
        current -> next = current;

}
  • 現代的GCC會警告您有關誤導性縮進current->next = current; 該行不受while循環的控制(C 不是 Python!)。
  • 在循環中測試head->current是錯誤的; 它應該是當前- current->head要測試的current->head
  • 作為樣式的一個點. 和箭頭->運算符之間的綁定非常緊密,並且兩側都不能有空格。
  • 打印功能不應分配任何節點。

因為您從函數定義行中省略了類型信息,所以head被解釋為一個int (使用C90必須允許的標准C規則,以便有機會被接受),並且必須使用未配置為抱怨C99之前的代碼的編譯器。 這是21世紀; 您不應使用30歲的符號。 找出如何至少指定C99以及如何獲得警告。 對於GCC,這將遵循-std=c99 -Wall -Werror -Wstrict-prototypes -Wmissing-prototypes ; 我也使用-Wextra ,通常還有更多選擇。

void traverse_and_printList(struct Node *head)
{
    struct Node *current = head;
    while (current->next != NULL)
    {
        printf("%s", current->data);
        current->next = current;
    }
}

尤其是在調試時,我可能會使用類似於以下的格式,在數據周圍加上標記(我選擇了[[]] ,但選擇的是您自己的),因為它可以發現問題(例如不需要的'\\r'在行尾):

printf("[[%s]]\n", current->data);

我也可能會打印指針:

printf("C %p N %p [[%s]]\n", (void *)current, (void *)current->next, current->data);

您可能希望使用更詳細的符號( Current而不是CNext代替N ),但是緊湊性也有其優點。

警告:修改后的代碼尚未在編譯器附近。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM