Menu
Coddy logo textTech

Node Class

Lección 3 de 14 del curso Lista Enlazada - Serie de Estructuras de Datos #5 de Coddy.

challenge icon

Desafío

Fácil

Escribe una clase Node con:

  • Un constructor que recibe un entero value. Almacénalo como un campo de la clase e inicializa next a null (o el equivalente en tu lenguaje).
  • Un método getValue que no recibe ninguna entrada y devuelve el value almacenado.

Pruébalo tú mismo

#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");
        }
    }
    return 0;
}

Todas las lecciones de Lista Enlazada - Serie de Estructuras de Datos #5