본문 바로가기

Programming/BaekJoon

[C++] 백준 10828번 : 스택


https://www.acmicpc.net/problem/10828




#include <iostream>

#include <vector>

#include <string>

using namespace std;


#define FOR(b) for(int i=0; i<b; i++)


class St {

public:

int head;

vector<int> data;

St() {

head = 0;

}

void push(int n) {

head++;

data.push_back(n);

}

int pop() {

if (head == 0)

return -1;

else {

int tmp = data[--head];

data.pop_back();

return tmp;

}

}

int size() {

return head;

}


int empty() {

if (head == 0)

return 1;

else

return 0;

}

int top() {

if (head == 0)

return -1;

else

return data.at(head-1);

}

};


int main() {

St k1;

int n;

cin >> n;

int n1;

string a;

FOR(n) {

cin >> a;

if (a == "push")

{

cin >> n1;

k1.push(n1);

}

else if (a == "pop") 

cout << k1.pop() << "\n";

else if (a == "size")

cout << k1.size() << "\n";

else if (a == "empty")

cout << k1.empty() << "\n";

else if (a == "top") 

cout << k1.top() << "\n";

}

}