![](/uploads/1/2/5/7/125796542/422443317.jpg)
Stack implementation in Java A stack is a linear data structure which follows the LIFO (last-in first-out) principle. That means the objects can be inserted or removed only at one end of it also called as top.
Output:Pop:43210Element on stack top: 4Element is found at position 3Element not foundMethods in Stack class.: Pushes an element on the top of the stack.: Removes and returns the top element of the stack. An ‘EmptyStackException’ exception is thrown if we call pop when the invoking stack is empty.: Returns the element on the top of the stack, but does not remove it.: It returns true if nothing is on the top of the stack. Else, returns false.: It determines whether an object exists in the stack.
If the element is found, it returns the position of the element from the top of the stack. Else, it returns -1.This article is contributed by Mehak Narang.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
The stack is a fundamental data structure used in computer science. In my opinion, it is one of the easier data structures to conceptually grasp and understand. Knowing and understanding how a stack works will not only make you a better programmer, it will also help you conceptualize problems in the future.
![Push And Pop Java Push And Pop Java](/uploads/1/2/5/7/125796542/851053768.jpg)
It will become a great addition to your arsenal of data structures. The implementation details will be written in Java.Before continuing, readers should know about the linked list data structure. If you don’t know what a linked list is, please read my on it before proceeding.If there are any sections that lack detail, please shoot me a message or leave a comment. Lets dive straight into the meat! Features of the Stack Data StructureThe stack data structure is a LIFO (Last-in, first-out) data structure, meaning that the last element that was placed onto the stack is the last to go out. It is similar to a deck of cards.
When playing our favorite card games (such as Poker, Big two, solitaire, etc.), the dealer often draws the card from the top of the deck. The top card is always the first to go. Push method flow diagramCopyright © thecodingdelight.com The Pop Method ExplainedThe pop method works in the opposite way from the push method. It removes the last item from the stack and returns the value.
If the audience has experience coding in JavaScript, it is very similar to how to built-in array’s pop method works. Below are the steps on how the pop method works in our implementation. Check if items exists (I.E. LastItem null). If it is empty, throw EmptyStackException. Otherwise do the following.
![Stack java 8 Stack java 8](http://introcs.cs.princeton.edu/java/11cheatsheet/images/stack-api.png)
Decrement the size of the stack. Check if a second last element exists. In another words, check if there are more than one items.
If there are more than one items, point last item reference of the instance to the previous last item. If we are dealing with “A” – “B” with B being the lastItem the following will happen:lastItem will point from B to A. Since nothing is pointing at B, it will be garbage collected. Otherwise, simply set lastItem to null. This will remove the one and only item from the stack.
![](/uploads/1/2/5/7/125796542/422443317.jpg)