class Node
Pelajaran 3 dari 14 dalam kursus Linked List, Seri Struktur Data #5 di Coddy.
Tantangan
MudahTulis class Node dengan:
- Constructor yang menerima integer
value. Simpan sebagai field class, lalu inisialisasinextdengannull(atau nilai yang setara dalam bahasa pemrogramanmu). - Method
getValueyang tidak menerima input dan mengembalikanvalueyang disimpan.
Coba sendiri
#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;
}
Semua pelajaran di Linked List, Seri Struktur Data #5
Latihan sendiri: Compiler C online