Stack

  • It is the child class of Vector.
  • It is a specially designed class for LIFO(Last in First Out) order. That means what ever the last object will be inserted that object will be deleted first.

Constructor :

Stack s=new Stack();

Creates an empty Stack Object.

Methods :

  • For inserting an object to the stack.

    Object push(Object o);

  • To remove and returns top of the stack.

    Object pop();

  • To return the top of the stack without removal of object.

    Object peek();

  • if the specified object is available, then it return its offset from top of the stack and if the object is not available then it returns -1.

    int search(Object o);

Example: StackDemo.java:

importjava.util.*;
classStackDemo
{
public static void main(String args[])
{
Stack s=new Stack();
s.push("silan");
s.push("tilan");
s.push("tapu");
System.out.println(s);
System.out.println(s.search("silan"));
System.out.println(s.search("kahnu"));
}
}

Output:

[silan,tilan,tapu]
3
-1

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects

SILAN Technology, BBSR
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment