DEV Community

Dhruv Panchal
Dhruv Panchal

Posted on

Explaining Stack in Detail

Stack

Stacks are the result of approaching a LIFO (Last in, First out) ordering with list node connection behavior. This means that element inside the stack are connected like a list but are added and extracted approaching LIFO.

Stack

[1] Lifo stack | https://commons.wikimedia.org/wiki/File:Lifo_stack.png

Based on that, the to methods that a stack should have to be called that way are pop which remove the top element from the stack and returns it, and push which adds a new element on top of the stack.

Minimal methods to work with Stacks

- Pop

Java implementation of the pop method in an array based stack

public Object pop() {
        if (!isEmpty()) {
            Object object = array[top];
            array[top--] = null;
            return object;
        } else {
            return null;
        }
    }
Enter fullscreen mode Exit fullscreen mode

As you see, after obtaining the element in top of the stack, it is immediately removed from it.

- Push

Java implementation of the push method in an array based stack

public boolean push(Object object) {
    if (top + 1 < size) {
        try {
            array[++top] = object;
            return true;
        } catch (Exception e) {
            System.out.println(e);
            return false;
        }
    } else {
        return false;
    }
}
Enter fullscreen mode Exit fullscreen mode

- Peek

In some circumstances the user may want to check the value stored in the top of the stack without removing it. In this scenarios is where peek method is useful, since it return the value without removing it from the stack. This way and extra push is not necessary.

Java implementation of peek in a Array based stack

public Object peek() {
    return (!isEmpty()) ? array[top] : null;
}
Enter fullscreen mode Exit fullscreen mode

Array and List based stacks

Example of an array based stack, of four elements. It will only receive four elements, if it reach it's max length, incoming values were not be added.

Stack

[2] Stack (data structure) LIFO | https://commons.wikimedia.org/wiki/File:Stack_(data_structure)_LIFO.svg

The mayor difference between an array based stack and one implemented using lists is that the first one is usually implemented with a static size, this means that it will only accept a specific number of element, the other one, will allow the user to add elements until memory is full.

References

Identifier Author Source
1 Maxtremus https://commons.wikimedia.org/wiki/File:Lifo_stack.png
2 Original uploader: Fibi
Derivative work: Marek M
https://commons.wikimedia.org/wiki/File:Stack_(data_structure)_LIFO.svg

Top comments (0)