Tuesday, June 24, 2014

Linked List implementation of Stack Data Structure in Java

A stack is a simple data structure for storing data. In stack, the order in which the data arrives is important. A pile of books is a good example of stack. A stack is an ordered list in which insertion and deletion are done at one end, where end is called top. The last element inserted is the first one to be deleted. Hence, it is called Last in First Out (LIFO) or First in Last Out (FILO) list

In this post, we'll see the other way of implementing Stack is by Linked List. This is continuation of the previous post about Array implementation of Stack
In this implementation, push operation is implemented by inserting element at the beginning of the list. Pop operation is implemented by deleting the node from the beginning.
Even you can throw exception instead of printing message in case of pop operation when stack is empty.


Generic Implementation of Stack as Linked List

Generic Stack


Performance and Limitations
Performance : Let n be the number of elements in the stack.

  • Space Complexity  for n push operations : O(n)
  • Time Complexity of push : O(1)
  • Time Complexity of pop : O(1)
  • Time Complexity of isEmpty : O(1)
  • Time Complexity of isStackFull : O(1)
  • Time Complexity of deleteStack : O(1)

Limitations : The size of the stack must be defined in prior and cannot be changed.

Download code : ListNode LinkedStack LinkedStackDemo  GenericListNode GenericLinkedStack GenericStackDemo

If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...


  1. good explanation...But i am not able to copy the code in your post. I think you have used the image for the program. Could you please post the code instead of image..


    1. Code is already available at the end of the post.

  2. Thanks for the post, I am techno savvy. I believe you hit the nail right on the head.
    I am highly impressed with your blog. It is very nicely explained.
    Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students.

  3. Great blog.
    Thank you for written this blog regarding to core technology.This is very Helpful and informative blog.
    mobile app training institutes
    iphone job oriented course

  4. Great blog.Thank you for written this blog regarding software.This is very Helpful and informative blog.
    asp.net development company

  5. Nice Blog, When i was read this blog i learnt new things & its truly have well stuff related to developing technology, Thank you for sharing this blog.
    iphone job training center
    ios app development course