Node 类
Coddy 链表:数据结构系列第 5 课 课程的第 3 课,共 14 课。
挑战
简单编写一个类 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 在线编译器