importjava.util.Stack;classKW{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
kodingwindow@kw:~$ javac KW.java kodingwindow@kw:~$ java KW
Peek element: Blueberry
Popping: Blueberry
Stack elements:
Apple
Cherry
Grapes
Mango
Orange
Apple
kodingwindow@kw:~$
Dear User, Thank you for visitng KodingWindow. If you are interested in technical articles, latest technologies, and our journey further, please follow us on LinkedIn.