In this post, we will see Stack Data Structure using Java | Stack Data Structure Implementation in Java
Watch this video to know about Stack Data Structure in Java:
Watch on YouTube: https://www.youtube.com/watch?v=V1VGGHt1kRE
Program Code: (StackProg.java)
Watch this video to know about Stack Data Structure in Java:
Watch on YouTube: https://www.youtube.com/watch?v=V1VGGHt1kRE
Program Code: (StackProg.java)
import java.util.*; public class StackProg { public static void main(String args[]) { Stack st = new Stack(); int top; System.out.println("stack: " + st); st.push(42); System.out.println("stack: " + st); st.push(66); System.out.println("stack: " + st); st.push(99); System.out.println("stack: " + st); top=(int)st.pop(); System.out.println("stack: " + st); top=(int)st.pop(); System.out.println("stack: " + st); top=(int)st.pop(); System.out.println("stack: " + st); try { top=(int)st.pop(); } catch(EmptyStackException e) { System.out.println("Empty Stack"); } } } |
Output:
parag@parag-Inspiron-N4010:~/Desktop/prog$ javac StackProg.java
Note: StackProg.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. parag@parag-Inspiron-N4010:~/Desktop/prog$ java StackProg stack: [] stack: [42] stack: [42, 66] stack: [42, 66, 99] stack: [42, 66] stack: [42] stack: [] Empty Stack |
No comments:
Post a Comment