Program to implemnet stack using array
public class StackImpWithArray { static final int MAX=2; int top; int bucket[]=new int[MAX]; StackImpWithArray(){ top=-1; } boolean push(int val){ if(top<MAX-1){ top++; bucket[top]=val; System.out.println("Value Pushed"); return true; } else{ System.out.println("Stack Overflow"); return false; } } int pop(){ if(top<0){ System.out.println("Stack Underflow"); return 0; } else{ int val=bucket[top]; top--; System.out.println("Value Removed"); return val; } } int peek(){ if(top<0){ System.out.println("Stack Underflow"); return 0; } else{ return bucket[top]; } } boolean isEmpty(){ return (top<0); } public static void main(String[] args) { StackImpWithArray s=new StackImpWithArray(); s.push(1); s.push(2); s.push(3); // Stack Overflow System.out.println("Stack Peek : "+s.peek()); System.out.println("Stack is empty : "+s.isEmpty()); s.pop(); s.pop(); s.pop(); // Stack Underflow System.out.println("Stack is empty : "+s.isEmpty()); System.out.println("Stack Peek : "+s.peek()); } }