Menu
Coddy logo textTech

Classe Nodo

Lezione 3 di 14 del corso Lista doppiamente concatenata - Serie sulle strutture dati n. 6 di Coddy.

challenge icon

Sfida

Facile

Scrivi una classe Node con:

  • Un costruttore che riceve un intero value. Memorizzalo come campo della classe e inizializza sia prev sia next a null (o all'equivalente nel tuo linguaggio).
  • Un metodo getValue che non riceve input e restituisce il value memorizzato.

Provalo tu

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

int main() {
    Node* node = NULL;
    char line[256];
    while (fgets(line, sizeof(line), stdin)) {
        line[strcspn(line, "\r\n")] = '\0';
        char* cmd = strtok(line, " \t");
        if (!cmd) continue;
        if (strcmp(cmd, "create") == 0) {
            char* arg = strtok(NULL, " \t");
            if (arg) node = Node_new(atoi(arg));
        }
        if (strcmp(cmd, "getValue") == 0) {
            printf("%d\n", Node_getValue(node));
        }
        if (strcmp(cmd, "nextIsNull") == 0) {
            printf("%s\n", node->next == NULL ? "true" : "false");
        }
        if (strcmp(cmd, "prevIsNull") == 0) {
            printf("%s\n", node->prev == NULL ? "true" : "false");
        }
    }
    return 0;
}

Tutte le lezioni di Lista doppiamente concatenata - Serie sulle strutture dati n. 6

Esercitati da solo: Compilatore C online