Stack Program in Java

The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector class. It implements the stack data structure that works on LIFO (Last In First Out) principle. The stack program in Java shows the usage of the stack in the program.

Syntax to Create a Stack

type is the kind of object we are going to create, and identifier is the reference variable.

Adding Elements to the Stack

In order to add elements to the stack, the push() method is invoked. Consider the following program.

FileName: StackExample.java

Output:

Explanation: The method push() adds elements to the stack. In the output, element 29 sits at the top of the stack since it is added at last, whereas element 9 is added at the beginning; therefore, element 9 sits at the bottom.

Removing Elements from the Stack

To remove elements from a stack, the pop() method is used. Observe the following program.

FileName: StackExample1.java

Output:

Explanation: In the following figure, we see that 29 is the topmost element. Therefore, when the pop() method is invoked, the topmost element gets removed, and the stack now looks like the following. Now, 14 has become the topmost element. Therefore, the next calling of the pop() method removes the element 14. Then, the next calling of the pop() method removes 7, and so on. At the end, the stack becomes empty.

Stack Implementation using LinkedList

We can also implement a stack using LinkedList. The following program does the same.

FileName: StackExample2.java

Output:

Explanation: By selecting the appropriate methods of the LinkedList class, the above program successfully imitates a stack. After the addition of elements, the linked list looks like the following

Stack Program in Java

If we see the top-down view, the list looks like the following:

Stack Program in Java

Now, element 29 sits at the top and 9 at the bottom. When the removeLast() method gets invoked, it removes the last element, i.e., element 29, from the list.

Stack Program in Java

Since, the removed element 29 is also the last element added to the list, we find that LIFO (Last-In-First-Out) principle is working properly. Now, element 14 occupies the topmost position. Therefore, on calling the removeLast() method removes element 14 from the list.

Stack Program in Java

Thus the removeLast() method keeps on removing the topmost element eventually, resulting in an empty list. We see that the LinkedList in the above program imitates a stack.

Note: We can even implement a queue from the above-mentioned doubly linkedList. The other top-down view gives the following.

Stack Program in Java

We see that element 9 is at the top of the stack. Therefore, if we start removing elements from the top, we end up following the FIFO (First-In-First-Out) approach. Because element 9 was added in the beginning. Then 5 got added after that 7 was added eventually 29 was added. During the removal of element 9 is removed; after that, element 5 is removed. Then, 7 is removed, and eventually, 29 is removed. Thus, the element that is added in the beginning is the first one that got removed and the element that was added at the end removed at the end. Thus, following the FIFO fashion which is nothing but a queue.

In the above code, replace the method removeLast() with removeFirst(), and the code starts behaving as a queue.

Pin It on Pinterest

Share This