[Java] Stack in Java – Stack in Java
Stack is a data structure to store multiple data elements. Stack operations according to the following mechanism before Last In / First Out (LIFO).
In the Stack with the basic operations:
+ Push : more 1 element on top of the stack
+ Pop : taken 1 elements from the top of the stack
+ Peek: Stack returns the first element without removing it from the Stack
+ isEmpty: Check Stack has not empty?
+ Search: returns the position of elements in the stack from the top of the stack if you do not see returns -1
import java.util.Stack;
class MyStack{
public static void main(String[] agrs){
Stack <Integer> s = new Stack<Integer>();
for (int i=0; i<10; i++)
s.push(i);
System.out.println("Index of number 6 in Stack : " + s.search(6));
System.out.println("Index of number 15 in Stack : " + s.search(15));
while(!s.isEmpty()){
System.out.print(s.peek() + " ");
s.pop();
}
}
}



Recent Comments