

The empty() method checks if the topIndex is at -1 which implies, the stack is empty. The size() method returns the size of the stack i.e. We initialize the data array and topIndex in the constructor method. Default Array capacity is 1000 for the stack where capacity is not mentioned.

There are two constructor methods, one with capacity customed by the user and the other default. We declare an array named data to store the values and topIndex to keep track of the top element. We can make a stack of String datatype, of Integer datatype, or Character datatype. It enables the user to use any data type. We name our class as ArrayStack as we are using Array to store our data.Į in Angular brackets () makes our class Generic. It will have the same functionality as we saw in the above examples. We are implementing our own stack using class and methods. Implementation of Stack Using Array in Java These operations are also performed using stacks. You must have also observed the Back and Forward buttons on browsers. When you make changes, it pushes changes onto the stack. When you undo something, it pops the most recent action. One such application is the undo mechanism in text editors. No element can be retrieved, removed, or added from the middle of the stack. Likely, elements can be added to the stack at the top, and they can be seen or removed from the top.

We cannot add or remove a plate at the middle of the stack. So, when we need a plate, we take (pop) from the top of the stack, and when we want to add a plate, we put (push) it at the top as well. The name “stack” is derived from a stack of plates.

Implementation of Stack and Queue using Array as well as LinkedList.Ī stack is a collection of objects that are inserted and removed in a LIFO(Last In First Out) fashion.Introduction to Stack and Queue data Structures in detail and their differences.Stack and Queue both are Linear Data Structures. They are used to store the same type of data and retrieve the data in a specific order. Do you want to be informed about new tutorials and articles? Then click here to sign up for the Happ圜 newsletter.Stack and Queue are fundamental data structures in Java Collections Framework. If you still have questions, please ask them via the comment function. In the following parts of this tutorial, I will present various stack implementations: However, keep in mind that deques also provide operations that a stack should not offer, such as inserting and removing elements at the bottom of the stack.Īlternatively, you can implement your own stack class.
Java stack code#
( "stack.pop() = " + stack.pop()) Īs you can see, the code is almost identical to the previous example. ( "stack.isEmpty() = " + stack.isEmpty()) The following code ( ArrayDequeDemo in the GitHub repo) shows the exemplary application of ArrayDeque as a stack: Instead of empty(), you have to call isEmpty().We have the methods push(), pop(), and peek().The interface is similar to that of Stack: Instead, the Java developers recommend using one of the Deque implementations, such as ArrayDeque. Better is usually optimistic locking by CAS ("compare-and-swap") operations as found in the concurrent queue and deque implementations. Using synchronized on every method call is not a particularly performant means of making a data structure thread-safe.So by using Stack, you are committing to a specific implementation. Stack does not implement an interface.By extending Vector, Stack provides operations that have no place in a stack, such as accessing elements by their index or inserting and deleting elements at arbitrary positions.What exactly does this mean? In my opinion, Stack should not be used for the following reasons: "A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class." The Java developers recommend not to use anymore. Just like pop(), also peek() would throw an EmptyStackException if the stack is empty. Exception in thread "main" Īt java.base/(Stack.java:101)Īt java.base/(Stack.java:83)Īt eu.main(JavaStackDemo.java:28) Code language: plaintext ( plaintext )
