Stack Class :

  • It is child class of vector
  • It is specially designed class for last in first out order(LIFO)

CONSTRUCTOR

Stack s = new stack();

Operations:

  • push(Object o)
  • pop() offset
  • peek()

java stack example
 
 
 
 
 
s.search(A) //3
s.search(Z) ;//-1

 

 

Methods:

  • Object push(Object obj):-For inserting an object to the stack.
  • Object pop():-To removes and returns top of the stack.
  • Object peek():-To returns the top of the stack without removal of the object.
  • int search(Object obj):-If the specified object is available it returns its offset from the top of the stack. If the object is not available then it returns -1.
  • Object top():- For inserting an object to the stack.

Example of stack:

Output :

[A, B, C]
-1

Leave a Reply