Stack implementation : Using Swing for GUI Java program Source Code

Stack is a simple abstract data type or data structure . It is based on LIFO property means Last in first out .
It uses two operations on elements in the stack . Push for adding  the elements in to the stack . Pop for removing the elements from the stack . As you understand the lst element added was the first one to show the door or exit while popping from the stack .

The most important thing is to consider stack as the linear data structure as the operations push or pop are implemented at one end  of the structure  known as the top of the stack .

There are two states a stack can be in :

Overflow State :  If all the elements of the stack are filled and then we try to push  another element in to the
                             top of the stack , then the stack is said to be in the overflow state .

Underflow State : If there is no element in the stack , that is the stack is empty and we try to pop element
                               from it , then the stack is in underflow state .

Often stack is termed as restricted data structure as the small number of operations can be performed on it .

The real world example of the stack is the CD case . How you retrieve cd from the cd case . If you think carefully then you can understand the stack functionality.  In java programs , the local variables and local methods all are stored in stacks .

For example as we know that all the java applications start from the main method . Suppose then after any subsequent call to  the other methods from the main function  has been done . First we call A method , then in A we call B , in B we call C .  So first B waits for the completion of C method , when C completes then remaining of B method starts executing , and then A and in last the main method completes .

Stack  was first proposed by Alan M Turing in 1946.

So in pictorial representation it looks like this :

                  |                C()                     |
                  |                 B()                    |
                  |                 A()                    |
                  |                main()                 |

Here in the below example :
The size is set for the stack by using setSize. Then put the values or elements you want to add to the stack.
If you want to pop elements from the stack then click on the pop button .

Pseudo Code :

1.  Set the maximum size of the stack 
2.  Select an operation : push or pop 
       if push 
              add element to the stack that is at top 
       if pop 
              remove element from the stack 

The famous application of the stack is the tower of honoi problem and besides that it found its usage in the expression evaluation and syntax parsing , rearranging rail road cars , backtracking ,quick sort , the stack span problem and runtime memory management .

Demo here :

stack implementation in java program code

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry