0

CでリンクリストのPop関数を実装するのに問題があります.

最初の実行では機能しないという点で、これは非常に奇妙なエラーです。

Node * operations = GetOperations(); // Retrieve linked-list of operations
Print(operations);
while (1) {
    if (operations == NULL) {
        return 0;
    }

    Node * temp = Pop(&operations);
    printf("Node popped is: %s\n", temp->data);
    Print(operations);
    Node * temp2 = Pop(&operations);
    printf("Node2 popped is: %s\n", temp2->data);
    Print(operations);
    // Other code down here... //
    return 1;
}

そして、ポップの私のコード:

Node * Pop (Node ** head) {
    if (*head == NULL) {
        printf("Error popping! List is empty.\n");
        return NULL;
    }

    Node * temp = *head;
    *head = (*head)->next;
    return temp;
}

私のノードのコード:

typedef struct node {
    char * data;
    struct node * next;
} Node;

GetOperations が返された状態でプログラムを実行すると (->次のノードを表す):

Op1->Op2->Op3...

次の出力が得られます。

Operations: Op1->Op2->Op3
Node popped is: (null)
Operations: Op1->Op2->Op3
Node2 popped is: Op1
Operations: Op2->Op3

ご覧のとおり、最初のものpopは決して機能しません。

4

1 に答える 1