Menu
CoddyTech

Node 类

Coddy 链表:数据结构系列第 5 课 课程的第 3 课,共 14 课。

challenge icon

挑战

简单

编写一个类 Node,包含:

  • 一个构造函数,接收一个整数 value。将它存储为类字段,并将 next 初始化为 null(或你所用语言中的等效值)。
  • 一个方法 getValue,不接收输入,并返回存储的 value。

亲手试试

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

链表:数据结构系列第 5 课中的所有课

自己练习: C 在线编译器