/*
Stack Is a Linear Data Structure.FIFO(First In First Out) or LIFO(Last In First Out).
[ Elements ]
[ Elements ]
[ Elements ]
[ Elements ]
*/
import java.util.*;
class stack{
public static void main(String[] args){
Stack<Integer> s=new Stack<Integer>();
//Push Element To Stack
s.push(10);
s.push(5);
s.push(2);
s.push(1);
System.out.println(s);
/*
After Push The Stack Like
[ 1 ] <---Push And Pop In Top Of The Statck
[ 2 ]
[ 5 ]
[ 10]
*/
//Pop Element From Statck
s.pop();
System.out.println(s);
/*
After Pop From Stack
[ 1 ]--> Pop From Top Of The Stack
[ 5 ]
[ 10]
*/
System.out.println(s.peek());
/*
s.peek();
Shows Top Of The Stack
*/
/*
s.empty()
returns boolean Value If stack Is empty or not
*/
System.out.println(s.empty());
}
}
/*
Java Stack Functions And Usages
===============================
pop() - It Returns And Remove the Top Of Element From Top Of The Stack
push() - Push New Element To The Stack
peek() - Shows(Return) Top Of The Element In Stack
empty() - Remove Element From Statck
search() - Search Is The Element Is Found On The Stack.
*/
/*
Output:
[10, 5, 2, 1]
[10, 5, 2]
2
false
*/
Comments
Post a Comment