Stack Class in Java

Stack:-


  • It is the child class of vector contains only one constructor
  Stack s = new Stack();

Methods of Stack:

  • Object push(object o)

To insert an object into the stack

  • Object pop();

To remove and returns top of stack

  • Object peek();

To return top of the stack

  • Boolean empty();

Return true when stack is empty

  • Int search (object o)

return the offset from top of the stack if the object is available, otherwise return – 1


Example:-


Class StackDemo

{

public static void main(String[] args)

{

Stack s =new stack();

S.push(“A”);

S.push(“B”);

S.push(“C”);

System.out.println(s);

System.out.println(s.search(“A”));

System.out.println(s.search(“Z”));

}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *