스택
by
Stack
배열을 이용한 stack입니다.
#include <iostream>
using namespace std;
class Stacks {
int top, maxsize;
int *stack;
public:
Stacks(int size) {
maxsize = size;
stack = new int[maxsize];
top = -1;
}
bool isFull() {
if (top == maxsize - 1)
return true;
else
return false;
}
bool isEmpty() {
if (top == -1)
return true;
else
return false;
}
int pop() {
if (!isEmpty())
{
int tmp = stack[top];
top--;
return tmp;
}
}
void push(int element) {
if (!isFull())
{
top++;
stack[top] = element;
}
else
cout << "꽉참";
}
};
int main(void)
{
Stacks A(100);
A.push(2);
A.push(3);
A.push(4);
cout << A.pop() << endl;
cout << A.pop() << endl;
cout << A.pop() << endl;
return 0;
}
2 3 4를 차례대로 push하고
pop하면 4 3 2 가 순서대로 나오는 것을 확인할 수 있습니다.
Subscribe via RSS