Node Class
Coddy의 연결 리스트 - 자료구조 시리즈 #5 코스 레슨 — 14개 중 3번째.
챌린지
쉬움다음과 같은 Node 클래스를 작성하세요:
- 정수
value를 인자로 받는 생성자. 이를 클래스 필드에 저장하고,next를null(또는 해당 언어의 대응되는 값)로 초기화합니다. - 입력값 없이 저장된
value를 반환하는getValue메서드.
직접 해보기
#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;
}