Java Stack Program

Java Stack Program


Two programs are given on stack.

  1. The present one is general, storing any data type. The methods push, peek, pop, size etc. are used.
  2. 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


Java Stack Program
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.

Collection –> List –> Vector –> 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".

14 thoughts on “Java Stack Program

    1. S. Nageswara Rao, Corporate Trainer Post author

      Enums

      JDK 1.5 supports enumerated types. The “enum” is a special type of class. The enum type puts restriction on the instance variables. It is a keyword of Java from JDK 1.5. The variables of enum are by default integer values and also static and final. Because variables are final (constants), they are written in uppercase, by convention. Enums are type-safe.

      enum StudentGrades
      {
      A, B, C, D
      }
      public class EnumDemo
      {
      public static void main(String args[])
      {
      StudentGrades sg;
      sg = StudentGrades.C;
      // printing enum value
      System.out.println(“Value of sg: ” + sg + “\n”);
      // assign one more value
      sg = StudentGrades.A;
      // comparing two enum values
      if(sg == sg.A)
      {
      System.out.println(“Student is intelligent\n”);
      }
      // using switch statement with enum
      switch(sg)
      {
      case A:
      System.out.println(“Intelligent”); break;
      case B:
      System.out.println(“Clever”); break;
      case C:
      System.out.println(“Medium”); break;
      case D:
      System.out.println(“Poor”);
      }
      }
      }

      Because enum values are integer values, enum can be used in combination with switch statement as in the above program.

      Programming Tip: Do not confuse enum with java.util.Enumeration interface (exists from JDK 1.0). Both are very different.

Leave a Reply

Your email address will not be published. Required fields are marked *