CSE/Data Structure

C++ 스택

언제나 변함없이 2019. 11. 6. 15:50
반응형
#include <cstdio>
#include <cstdlib>
const int MAX_STACK_SIZE = 20;
//data[]배열로 구현한 stack

class Stack {
private:
    int top;
    int data[MAX_STACK_SIZE];

public:
    Stack() {
        top = -1;
    }
    ~Stack() {

    }

    void push(int element) {
        if (!isFull())
            data[++top] = element;
    }

    bool isFull() {
        return top == MAX_STACK_SIZE - 1;
    }

    int pop() {
        if (!isEmpty())
            return data[top--];
    }

    bool isEmpty() {
        return top == -1;
    }

    int peek() {
        if (!isEmpty())
            return data[top];
    }
    void display() {
        printf("스택 내부 %2d개 존재 ", top + 1);
        for (int i = 0; i <= top; i++)
            printf("%2d /", data[i]);
    }
};

void main() {
    Stack s;

}

반응형