/*
20240220 배열을 이용한 기본 스택 구현
*/
class MyStack{
int[] arr;
int top = -1;
MyStack(int size){ //생성자
arr = new int[size];
}
public boolean isEmpty(){
if(this.top == -1){
return true;
}else {
return false;
}
}
public boolean isFull(){
if(this.top == this.arr.length -1){
return true;
}else {
return false;
}
}
public void push(int data){
if(this.isFull()){
System.out.println("Stack is full!");
return;
}
this.top += 1;
this.arr[this.top] = data;
}
public Integer pop(){
if(this.isEmpty()){
System.out.println("Stack is empty!");
return null;
}
int data = this.arr[this.top];
this.top -= -1;
return data;
}
public Integer peek(){
if(this.isEmpty()){
System.out.println("Stack is empty!");
return null;
}
return this.arr[this.top];
}
public void printStack(){
for (int i = 0; i < this.top +1 ; i++) {
System.out.println(this.arr[i] + " ");
}
System.out.println();
}
}
public class stackPractice01 {
public static void main(String[] args){
//test code
MyStack myStack = new MyStack(5);
System.out.println(myStack.isEmpty());
myStack.push(1);
myStack.push(2);
myStack.push(3);
myStack.push(4);
myStack.push(5);
myStack.push(6);
myStack.printStack();
System.out.println(myStack.peek());
myStack.printStack();
System.out.println(myStack.pop());
System.out.println(myStack.pop());
myStack.printStack();
System.out.println(myStack.pop());
System.out.println(myStack.pop());
System.out.println(myStack.pop());
myStack.printStack();
}
}