public class Stack {
int[] arr;
int top;
int capacity;
public Stack(int size){
arr = new int[size];
top = -1;
capacity = size;
}
//check stack is empty or not
public boolean isEmpty(){
return top == -1;
}
//check stack is full or not
public boolean isFull(){
return top == capacity-1;
}
//push element
public void push(int element){
if(isFull()){
[Link]("Stack is full !!!");
return;
}
arr[++top] = element;
[Link]("element pushed successfully !!!" + element);
}
//pop the element
public int pop(){
if(isEmpty()){
[Link]("Stack is empty !!!");
return -1;
}
else{
return arr[top--];
}
}
//peek operation
public int peek(){
if(isEmpty()){
[Link]("Stack is empty !!!");
return -1;
}else{
return arr[top];
}
}
//display
public void display(){
if(isEmpty()){
[Link]("Stack is empty !!!");
return;
}else{
for(int i=0;i<=top;i++){
[Link](arr[i]);
}
}
}