Menu
Coddy logo textTech

Node Class

Lesson 3 of 14 in Coddy's Linked List - Data Structures Series #5 course.

challenge icon

Challenge

Easy

Write a class Node with:

  • A constructor that gets an integer value. Store it as a class field, and initialize next to null (or your language's equivalent).
  • A method getValue that gets no input and returns the stored value.

Try it yourself

#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;
}

All lessons in Linked List - Data Structures Series #5