Algorithm
[자료구조] 스택
테니드2
2025. 4. 2. 11:15

#include <iostream>
#include <string>
using namespace std;
class stack {
private:
int arr[10000];
int cursor;
public:
void init() {
cursor = -1;
}
void push(int data) {
arr[++cursor] = data;
}
bool empty() {
return cursor < 0;
}
int pop() {
if (empty())
return -1;
return arr[cursor--];
}
int size() {
return cursor + 1;
}
int top() {
if (empty()) {
return -1;
}
return arr[cursor];
}
};
int main()
{
int N;
cin >> N;
stack s;
s.init();
string command;
for (int i = 0; i < N; i++)
{
cin >> command;
if (command == "push")
{
int data;
cin >> data;
s.push(data);
}
else if (command == "top")
{
cout << s.top() << endl;;
}
else if (command == "size")
{
cout << s.size() << endl;
}
else if (command == "empty")
{
cout << s.empty() << endl;
}
else if (command == "pop")
{
cout << s.pop() << endl;;
}
}
}