Customized stack
{
private int arr[];//Array to store stack elements
private int top; //acts as a cursor for stack
private int capacity;//maximum size of stack
//1.constructor to initialize stack
public Customized stack(int size)//size=5
{
arr=new int[size];
capacity=size;//capacity=5
top=-1;
}
//2.method for push operation(inserting data)
public void push(int data)//data=20
{
if(isFull())
{
System.out.println(“stack is full cannot push the elements”);
}
arr[++top]=data;//top=top+1
System.out.println(“Data inserted are=” +data);
}
public boolean isFull()
{
return top==capacity-1;//5-1=>4
}
//3.Removing a top most element(pop operation)
public boolean isEmpty()
{
return top==-1;
}
public int pop()
{
if(isEmpty())
{
System.out.println(“stack is empty unable to remove elements”);
return -1;
}
return arr[top–];//return top element and going to decrement top
}
//method to display
public void display()
{
if(isEmpty())
{
System.out.println(“stack is empty unable to display the elements”);
}
System.out.println(“The stack elements are=”);
for(int i=0; i<=top; i++)
{
System.out.println(arr[i]+” “);
}
}
//method to display the top most element using peek
public int peek()
{
if(isEmpty())
{
System.out.println(“The stack is empty unable to display the top most element”);
return -1;
}
return arr[top];
}
}
class Mystacks
{
public static void main(String args[])
{
Customizedstack c=new Customizedstack(5);
c.push(10);
c.push(20);
c.push(30);
c.pop();
c.display();
System.out.println(“Top most element right now=” +c.peek());
}
}