Python으로 구현

List로 Stack을 구현할 수 있다.

# 스택 생성
stack = []

# push
stack.append(1)

# pop
stack.pop()

# size
len(stack)

# empty
if stack:
    ...

# top
stack[-1]

C를 이용한 구현

#include <stdio.h>

#define MAX 10000

int stack[MAX];
int top;

void init() {
    top = -1;
}

int push(int value) {
    if (top >= MAX)
        return 0;
    else
        stack[++top] = value;
    return 1;
}

int pop() {
    if (top < 0)
        return -1;
    else
        return stack[top--];
}

int isEmpty() {
    if (top == -1)
        return 1;
    else
        return 0;
}

int main() {
    init();

    push(1);
    push(2);
    push(3);
    pop();
    push(4);

    printf("is Empty? -> %d\\n", isEmpty());
    int i;
    for (i = 0; i <= top; i++) {
        printf("%d \\n", stack[i]);
    }
    return 0;
}

C++에서 STL로 구현

vector를 사용하던 stack을 사용하던 상관없지만 가독성을 위해 stack을 쓴다고 카더라.

std::vector를 이용

#include <iostream>
#include <vector>

using namespace std;

vector<int> stack;

int main() {
    // push
    stack.push_back(1);
    stack.push_back(2);
    stack.push_back(3);

    // pop
    stack.back();
    stack.pop_back();

    // size
    stack.size();

    // empty
    stack.empty();
}

std::stack을 이용

#include <iostream>
#include <stack>

using namespace std;

stack<int> s;

int main() {
    // push
    s.push(1);
    s.push(2);
    s.push(3);

    // pop
    s.top();
    s.pop();

    // size
    s.size();

    // empty
    s.empty();
}