C언어/문제풀다 하나씩

head 따라 이동

mcdn 2020. 4. 10. 18:59
반응형

 

#include <iostream>
#include <cstring>
using namespace std;

struct Node {
	char a;
	Node* left;
	Node* right;

};

int main() {

	Node* head = new Node();
	head->a = 'A';
	head->left = new Node();
	head->right = new Node();
	head->left->a = 'B';
	head->right->a = 'C';
	head->left->left = new Node();
	head->left->right = new Node();
	head->left->left->a = 'D';
	head->left->right->a = 'E';

	char input[5];
	cin >> input;
	int len = strlen(input);
	
	Node* last = head;
	if (len != 1) {
		for (int i = 1; i < len;i++) {
			if (input[i] == 'R') {
				last = last->right;
			}
			else if (input[i] == 'L') {
				last = last->left;
			}
		}
	}
	cout << last->a;
	


	return 0;
}

input : HLL

output : D

반응형