Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

Node Class

الدرس 3 من 14 في دورة القائمة المرتبطة - سلسلة هياكل البيانات #5 على Coddy.

challenge icon

التحدي

سهل

اكتب فئة Node تحتوي على:

  • مُنشئ (constructor) يستقبل عدداً صحيحاً value. قم بتخزينه كحقل داخل الفئة، وقم بتهيئة next إلى null (أو ما يعادلها في لغتك البرمجية).
  • طريقة (method) باسم 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