DEV Community

Cover image for Introduction to Stack
Diya Vijay
Diya Vijay

Posted on • Updated on

Introduction to Stack

Stack is a linear data structure in which data can be added or removed only at one end. This end is generally known as "TOP" of the stack.
let's take one example for better understanding. It is like stack of plates in cafe, where every new plate is added to the top of the stack and removed from the top as well. This simply means that the last plate added to a stack is the first plate to be removed. Hence stacks are also called Last in First Out (LIFO)structure.
Let's take another example. Look in this picture, it looks yummy right? haha....it's a stack of oreo. Now if you want to build the Eiffel Tower then you have to add another Oreo on top of this Oreo stack. And now imagine you want to eat an Oreo (read the second word after "and" again, just imagine or buy your own Oreo cause obviously I won't share it with you, LOL) then you have to take it from the top of the Oreo stack.

Image description

Now let's talk about implementation of stack. well, stack can be implemented in two ways.
1) Static implementation
2) Dynamic implementation

  1. Static implementation : static implementation uses array to creat stack.

  2. Dynamic implementation : It uses linked list that uses pointers to implement the stack.

# Terminology of Stack

  • PUSH: when an item is added or inserted in a stack, the operation is known as push.

  • POP: when an item is removed or deleted from a stack, the operation is known as pop.

  • TOP: this term refers to the top of stack. top is used to check stack's overflow or underflow condition. top is initialized by -1.
    if TOP = -1 // indicates the stack is empty
    if TOP = MAXSIZE-1 // indicates the stack is full

Top comments (0)