Node Class
Lesson 3 of 14 in Coddy's Linked List - Data Structures Series #5 course.
Challenge
EasyWrite a class Node with:
- A constructor that gets an integer
value. Store it as a class field, and initializenexttonull(or your language's equivalent). - A method
getValuethat gets no input and returns the storedvalue.
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;
}