class Stack{
int top;
int capacity;
int[] array;
public Stack(int capacity) {
this.capacity=capacity;
this.top=-1;
this.array=new int[this.capacity];
}
public boolean isEmpty() {
return this.top==-1?true:false;
}
public boolean isFull() {
return this.top==this.capacity-1?true:false;
}
public void push(int data) {
if(isFull()){
System.out.println("Stack Overflow");
return;
}else {
System.out.println(data+" is Pushed");
this.array[++this.top]=data;
}
}
public int pop() {
if(isEmpty()) {
System.out.println("Stack is Empty");
return 0;
}else {
System.out.println(array[top]+" is Poped");
return this.array[top--];
}
}
public int top() {
if(isEmpty()) {
System.out.println("Stack is Empty");
return 0;
}else {
System.out.println("top is "+array[top]);
return this.array[top];
}
}
}
サンプルコード