Two programs are given on stack.
- The present one is general, storing any data type. The methods push, peek, pop, size etc. are used.
- The other one is generics stack storing similar or specific type of elements. Enhanced for loop (foreach) is used to iterate the elements.
Java Stack Program
public class GeneralStack
public static void main(String args)
Stack st = new Stack();
System.out.println("Calling empty() before elements are added: " + st.empty());
System.out.println("Calling empty() after elements are added: " + st.empty());
System.out.println("Elements in the stack: " + st);
System.out.println("Index number of 50 counted from top: " + st.search(50));
System.out.println("Index number of SNRao from top: " + st.search("SNRao"));
System.out.println("Element at the top: " + st.peek());
System.out.println("No. of elements before pop called: " + st.size());
System.out.println("Popped element: " + st.pop());
System.out.println("No. of elements after pop is called: " + st.size());
System.out.print("Technique of printing elements with empty():\n\t");
while( ! st.empty() )
System.out.print(st.pop( ) + " ") ;
System.out.println("\nNo. of elements after while loop: " + st.size());
Output of Java Stack Program
System.out.println(“Calling empty() before elements are added: ” + st.empty());
The empty() method returns true if no elements exist in the stack. As no elements are added still, the above statement returns true.
We know Stack is a legacy class and originally not part of collections framework. As Stack extends Vector and indirectly List also, Stack can make use of all the methods of Collection interface. add() is a method of Collection interface inherited by Stack. The original method of adding, defined in Stack class, is push(). Both methods are used in the program. This is how, the designers, gave compatibility to legacy classes with collection classes.
Following is the hierarchy of Stack.
System.out.println(“Index number of A counted from top: ” + st.search(50));
System.out.println(“Index number of SNRao from top: ” + st.search(“SNRao”));
Using search method, the programmer can know at what position or index number, a particular element exist in the stack. The counting starts from the top element (added last). As 50 is added last, serach(50) returns 1. search("SNRao") returns 6. Observe the screenshot.
System.out.println(“Element at the top: ” + st.peek());
The peek() method returns the element at the top; that is 50. But the element is not removed from the stack. Observe the code, 50 is again printed with pop() method.
System.out.println(“No. of elements before pop called: ” + st.size());
The size() method returns the number of elements present in the stack. The same size() method when called after pop(), prints one less, 8 (original number is 9).
System.out.println(“Popped element: ” + st.pop());
The pop() method returns the top element. The element is permanently comes out and cannot be obtained again.
Observe when size() is called after while loop, it prints 0 as all the elements are removed from stack with iteration using pop() method; now stack is empty. Rewrite this Java stack program with different iterations to get command over the subject.
Real-time Examples of Stack Implementations
Following are a few examples of Stack in realtime.
- Cement bags in a godown.
- Clothes in an almyrah.
- Bullets loaded in a gun.
With this knowledge of general Java stack program, now you can understand the second Java Stack program, Generics Stack.
Pass your comments to improve the quality of this tutorial" Java Stack Program".