public
synchronized int search(Object o)
Search
for an element from top and returns the distance of the element from
the top of the stack. The topmost item on the stack is considered to
be at distance 1. Returns -1 if the object is not in the stack.
import java.util.*; class StackSearch{ public static void main(String args[]){ Stack<Integer> myStack = new Stack<> (); /* Add Elements to myStack */ myStack.push(10); myStack.push(20); myStack.push(30); myStack.push(40); myStack.push(10); myStack.push(50); System.out.println("Elements in myStack are"); System.out.println(myStack); System.out.print("Data 10 at the position "); System.out.println(myStack.search(10)); System.out.print("Data 60 at the position "); System.out.println(myStack.search(60)); } }
Output
Elements in myStack are [10, 20, 30, 40, 10, 50] Data 10 at the position 2 Data 60 at the position -1
No comments:
Post a Comment