interface Stack {
void push(int data);
int pop();
void printStack();
}
public class StackUseArray implements Stack{
private int top;
private int stackArr[];
private int arrSize;
public StackUseArray(int arrSize) {
this.top = -1;
this.arrSize = arrSize;
stackArr = new int[arrSize];
}
@Override
public void push(int data) {
stackArr[++top] = data;
}
@Override
public int pop() {
if (top == -1) {
System.out.println("empty");
return 0;
}
else {
int res = stackArr[top--];
return res;
}
}
@Override
public void printStack() {
for(int i=0; i<=top; i++) {
System.out.print(stackArr[i]);
}
System.out.println();
}
public static void main(String[] args) {
int arrSize = 10;
Stack stack = new StackUseArray(arrSize);
stack.push(1);
stack.push(2);
stack.push(3);
stack.printStack();
stack.pop();
stack.pop();
stack.printStack();
stack.pop();
}
}
출력)