1 Pluspunkt 0 Minuspunkte

Wie kann ich diese Linked list als Parameter an eine Funktion übergeben?

struct Node {
    int points;
    char playername[50];
    struct Node* next;
};

int main() {

    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head->next = NULL;

    struct Node* first = (struct Node*)malloc(sizeof(struct Node));
    first->points = 1;
    strcpy(first->playername, "tick");
    first->next = NULL;
    head->next = first;

    struct Node* second = (struct Node*)malloc(sizeof(struct Node));
    second->points = 2;
    strcpy(second->playername, "trick");
    second->next = NULL;
    first->next = second;

    struct Node* third = (struct Node*)malloc(sizeof(struct Node));
    third->points = 3;
    strcpy(third->playername, "track");
    third->next = NULL;
    second->next = third;

    // Funktion mit Linked List als Parameter
    
    struct Node* current = head->next;
    while (current != NULL) {
        struct Node* temp = current;
        current = current->next;
        free(temp);
    }
    free(head);

    return 0;
    
}
von  

1 Antwort

0 Pluspunkte 0 Minuspunkte

Du übergibst den Pointer auf die Struktur.

void printList(struct Node* head) {

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

}

void main() {

    // ...

    printList(head);

    // ...

    return 0;
}

von (716 Punkte)