What is Stack in Data Structures?

    Like an array, a stack is an ordered collection of elements. Let's learn more about what is a stack in detail.

    A stack is one of the most common abstract data structures we use in programming. In the real world, we often use stack data structures. For example, when we use a browser to surf the internet, we get two buttons, forward and backward icons. They help us to navigate to the previous URLs. This structure uses the Stack concept. In our browser, when we use the back icon to visit the previous page, the backward stack pops out the previous page and pushes the current page to the forward stack.

    When we want to go back to the forward page, the browser pops the page from the forward stack and pushes the current page to the backward stack.

    What is the Stack Principle?

    Stack works on the LIFO principle, which stands for Last In First Out.

    The element pushed first in the stack would be accessed last, and the element pushed last would be accessed first. For example, in the real world, if there is a pile of plates, we will choose that plate which would be on the top of the pile.

    Stack Representation

    • A stack is an ordered collection of elements.
    • We insert and delete elements from the top of a stack.
    • The first element of the stack lay on the base of that stack.
    • The first element pushed in a stack is known as the base element.
    • The last element lay on the top of that stack.
    • The recently added element would be the first element on which the removal operation works first.
    • When we pop out elements from a stack, it would be popped out in reverse order.
    • In stack, the order of insertion is the reverse of the order of removal.

    Basic Operations on Stack

    Here are some basic operations we apply on a stack are:

    • Push
    • Pop
    • Peek
    • is_Empty
    • is_full


    The push operation pushes or inserts elements in the stack.


    The pop operation removes the last added element of the stack.


    The peek operation is used to display the topmost data of the stack.


    The Is_Empty function tells whether the stack is empty or not. If the stack consists of even a single data element, it returns True; else, it returns false.


    Is_ Full operation test whether the stack is full or not. If the stack is full, we will not be able to push more elements in the stack.

    Advantages and Disadvantages of Stack


    • In an array, we can not allocate memory dynamically, but in a stack, we can.
    • In stack, we can easily remove and insert elements.


    • A stack is not that flexible.

    Implementation of Stack Using Python

    class Stack(object):
        def __init__(self,size):
            self.size =size
        def push(self,data):       # Push operation inset element in the stack
            if len(self.items) < self.size: 
                print(data,"has pushed")
                print("Stack is full")
        def pop(self):         # pop function will remove the last added element 
            print("Element has been poped")
        def isempty(self):           # isempty function will check whether the stack is empty or not
        def isfull(self):               # isfull function tell whether the stack is full or not
            print(len(self.items) ==self.size)
        def peek(self):              # peek fucntion will return the top most element of the stack
        def display(self):        # display will show all the elements present in stack
            for i in self.items[::-1]:
    size = int(input("How many elements you want to enter in your stack: "))
    stack = Stack(size)


    How many elements you want to enter in your stack: 3
    10 has pushed
    30 has pushed
    22 has pushed
    Element has been poped
    | 30 |
    | 10 |


    In conclusion, a stack is a fundamental data structure that means the Last In, First Out (LIFO) principle. They are excellent in tasks such as task management of function calls, memory allocation, and expression parsing. We hope that the above tutorial helps you understand the basics of a stack and help you further in programming.

    If there are any queries, do let us know in the comment section below.

    Good luck!

    People are also reading: