importjava.util.Stack;classCodeaft{publicstaticvoidmain(String[]args){Stack<String>s=newStack<>();s.push("Apple");//First In Last Outs.push("Orange");s.push("Mango");s.push("Grapes");s.push("Cherry");s.push("Apple");s.push("Blueberry");//Last In First OutSystem.out.println("Peek element: "+s.peek());System.out.println("Popping: "+s.pop());System.out.println("\nStack elements: ");while(s.empty()==false){System.out.println(s.pop());}}}
Output
codeaft@codeaft:~$ javac Codeaft.java codeaft@codeaft:~$ java Codeaft
Peek element: Blueberry
Popping: Blueberry
Stack elements:
Apple
Cherry
Grapes
Mango
Orange
Apple
codeaft@codeaft:~$
Dear User, Thank you for visitng Codeaft. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.