본문 바로가기
백준

[10828번] 스택

by 2744m 2019. 3. 12.
/*10828번 스택*/
#include <iostream>
#include <string>
using namespace std;
int N, top_pointer;
string command;
int stack_arr[10000];

void push(int n) {
	stack_arr[++top_pointer] = n;
}
void pop() {
	if (top_pointer >= 0) {
		int return_value = stack_arr[top_pointer];
		stack_arr[top_pointer--] = 0;
		cout << return_value << '\n';
	}
	else cout << -1 << '\n';
}
void size() {
	cout << top_pointer + 1 << '\n';
}
void empty() {
	if (top_pointer >= 0) cout << 0 << '\n';
	else cout << 1 << '\n';
}
void top() {
	if (top_pointer >= 0) cout << stack_arr[top_pointer] << '\n';
	else cout << -1 << '\n';
}

int main() { 
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> N;
	top_pointer = -1;
	for (int i = 0; i <= N; i++) {
		getline(cin, command);
		if (command.size() == 0) continue;
		if (command[1] == 'u') {
			command.erase(0, 5);
			int put_num = stoi(command);
			push(put_num);
		}
		else if (command == "pop") pop();
		else if (command == "size") size();
		else if (command == "empty") empty();
		else if (command == "top") top();
	}
}

'백준' 카테고리의 다른 글

[1181번] 단어 정렬  (0) 2019.03.13
[1026번] 보물  (0) 2019.03.12
[10815번] 숫자카드  (0) 2019.03.11
[10989]수 정렬하기 3  (0) 2019.03.11
[2750,2751번] 수 정렬하기  (0) 2019.03.11

댓글