티스토리 뷰


Code

#include <iostream>
using namespace std;

#define MAX 1000000

int arr[MAX], point;

class stack
{
public:
    void push(int data)
    {
        arr[point++] = data;
    }
    void pop()
    {
        cout << arr[point - 1] << "\n";
        arr[point - 1] = 0;
        point--;
    }
    int size()
    {
        return point;
    }
    bool empty()
    {
        if (point == 0)
            return true;
        else
            return false;
    }
    void top()
    {
        if (point <= 0)
            cout << -1 << "\n";
        else
            cout << arr[point - 1] << "\n";
    }
};

int main()
{
    stack s;
    string command;

    int count = 0;

    cin >> count;

    while (count--)
    {
        cin >> command; // 명령어 입력

        if (command == "push")
        {
            int data = 0;
            cin >> data;
            s.push(data);
        }
        else if (command == "pop")
        {
            if (s.empty() == 1)
                cout << -1 << endl;
            else
                s.pop();
        }
        else if (command == "size")
        {
            cout << s.size() << "\n";
        }
        else if (command == "empty")
        {
            cout << s.empty() << "\n";
        }
        else if (command == "top")
        {
            s.top();
        }
    }
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함