Node Class
Coddy의 이진 트리 - 자료구조 시리즈 #3 코스 레슨 — 13개 중 3번째.
이진 트리의 기본 구성 요소인 Node를 만드는 것부터 시작해 보겠습니다.
챌린지
쉬움다음과 같은 기능을 가진 Node 클래스를 작성하세요:
- 입력을 받지 않고 노드 값을
0으로 초기화하는 생성자. - 입력을 받지 않고 노드의 값을 반환하는
getValue메서드. - 정수를 입력받아 노드의 값을 해당 값으로 설정하는
setValue메서드.
직접 해보기
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "node.h"
int main() {
Node n;
Node_init(&n);
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, "setValue") == 0) {
char* arg = strtok(NULL, " \t");
if (arg) setValue(&n, atoi(arg));
}
if (strcmp(cmd, "getValue") == 0) {
printf("%d\n", getValue(&n));
}
}
return 0;
}
이진 트리 - 자료구조 시리즈 #3의 모든 레슨
2Binary Tree Project
Node ClassNode with Sons