DEV Community

Cover image for Stacks
sndp
sndp

Posted on

Stacks

What are stacks ?

A stack is a data structure that serves according to last-in first-out (LIFO) mechanism. To understand this, imagine a scenario where someone putting some objects sequentially in some container to retreive at a later time. But they will happen to take the last object first, and then second to last and so on.

Can we imagine something similar?

A pistol magazine (in a video game) is a correct scenario because last round will be fired first. We can see it works according to stacks.

In programming scene, a stack has many uses, including,
i. Memory management.
ii. Reversing an array/reordering an array.
iii. To check the validation of an expression.
iv. Implementing rollback option as a function.

In addition, web browser and windows in operating system also uses stacks to manage memory.

A stack is an abstract data type which means the logic can be applied to user-defined scenarios by the user itself.
This is similar to implementing an abstract method definition as a concrete function. Therefore imagine abstract data type is a logic and we implement that logic ourself in code for our scenario.

A stack design has the following methods need to be implemented.
i. push
ii. pop
iii. peek
iv. isEmpty

We use arrays to implement these methods. I'll refer the array as container and its values as objects.

push
Enter fullscreen mode Exit fullscreen mode

When called, the object inside the parameters will be added to the container. Push is like inserting an object.

pop
Enter fullscreen mode Exit fullscreen mode

When called, the last item entered to the container will be removed and will be sent to user. Pop is like removing an object.

seek
Enter fullscreen mode Exit fullscreen mode

When called, the last item will be returned to the user. It will not get removed from the container. Seek is for displaying the last item in the container when user needed to check.

isEmpty
Enter fullscreen mode Exit fullscreen mode

When called, the user will know whether the container is empty or not.

Know that a container's size can be user-defined or it can be a value of a predefined constant when implemented using arrays.

Let's assume we have implemented a MyStack class with a container's object type of int to save numbers with a user-defined container size of five.

So it will look like this.

MyStack stack = new MyStack(5);
Enter fullscreen mode Exit fullscreen mode

We'll add some random five numbers and another number.

stack.push(11);
stack.push(87);
stack.push(43);
stack.push(117);
stack.push(-96);

stack.push(68);
Enter fullscreen mode Exit fullscreen mode

It should be possible to insert first five values and after that it will show a message that saying we cannot add further objects to our container. Therefore value 68 will not get pushed.

Now let's try popping an item.

int popped = stack.pop();
Enter fullscreen mode Exit fullscreen mode

If we print the variable defined as popped above, it will return the last number inserted which is -96.

Now remove the next remaining items also and try popping one more time.
Therefore if we called pop four and one more time it will show a message that says we cannot pop any further. At this time our stack is empty and isEmpty will return true.

If printed the values returned at each time you will see it is the opposite of the inserted order which means we have our stack working correctly and last-in first-out rule is in effect.

Learn more about stack implementation using following links

https://www.geeksforgeeks.org/stack-data-structure-introduction-program/

Oldest comments (0)