Open In App
Related Articles

Basic Operations in Stack Data Structure with Implementations

Improve Article
Improve
Save Article
Save
Like Article
Like

In order to make manipulations in a stack, there are certain operations provided to us for Stack, which include:

  • push() to insert an element into the stack
  • pop() to remove an element from the stack
  • top() Returns the top element of the stack.
  • isEmpty() returns true if the stack is empty else false.
  • size() returns the size of the stack.

In this post, we will see how to perform these operations on Stack.

Push Operation in Stack:

Push operation adds an item to the stack.

If the stack is full, then it is said to be an Overflow condition.

Below is a sample program to show Push operation in Stack.

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    stack<int> s; // creating a stack of integers
 
    s.push(1); // This pushes 1 to the stack top
 
    s.push(2); // This pushes 2 to the stack top
 
    s.push(3); // This pushes 3 to the stack top
 
    s.push(4); // This pushes 4 to the stack top
 
    s.push(5); // This pushes 5 to the stack top
 
    // printing the stack
 
    while (!s.empty()) {
        cout << s.top() << " ";
        s.pop();
    }
 
    // The above loop prints "5 4 3 2 1"
}


Java




import java.util.Stack;
 
public class StackExample {
    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>(); // Creating a stack of integers
 
        s.push(1); // This pushes 1 to the stack top
        s.push(2); // This pushes 2 to the stack top
        s.push(3); // This pushes 3 to the stack top
        s.push(4); // This pushes 4 to the stack top
        s.push(5); // This pushes 5 to the stack top
 
        // Printing the stack in reverse order
        while (!s.isEmpty()) {
            System.out.print(s.pop() + " ");
        }
 
        // The above loop prints "5 4 3 2 1"
    }
}


Python3




# Python Code:
stack = []
 
stack.append(1) # This pushes 1 to the stack top
 
stack.append(2) # This pushes 2 to the stack top
 
stack.append(3) # This pushes 3 to the stack top
 
stack.append(4) # This pushes 4 to the stack top
 
stack.append(5) # This pushes 5 to the stack top
 
# printing the stack
 
while stack:
    print(stack[-1], end=" ")
    stack.pop()
 
# The above loop prints "5 4 3 2 1"
 
# This code is contributed by Sakshi


Output

5 4 3 2 1 





Pop Operation in Stack:

Pop operation is used to remove an item from the stack.

The items are popped in the reversed order in which they are pushed. If the stack is empty, then it is said to be an Underflow condition.

Below is a sample program to show Pop operation in Stack.

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    stack<int> s; // creating a stack of integers
 
    s.push(1); // This pushes 1 to the stack top
 
    s.push(2); // This pushes 2 to the stack top
 
    s.push(3); // This pushes 3 to the stack top
 
    s.push(4); // This pushes 4 to the stack top
 
    s.push(5); // This pushes 5 to the stack top
 
    // printing the stack
 
    while (!s.empty()) {
        cout << s.top() << " ";
        s.pop();
    }
 
    // The above loop prints "5 4 3 2 1"
 
    // Now, let us remove elements from the stack using pop
    // function
 
    s.pop(); // removes 5 from the stack since 5 was present
            // at the top. Now, the new stack top
            // becomes 4.
    s.pop(); // removes 4 from the stack since 4 was present
            // at the top. Now, the new stack top
            // becomes 3.
    s.pop(); // removes 3 from the stack since 3 was present
            // at the top. Now, the new stack top
            // becomes 2.
    s.pop(); // removes 2 from the stack since 2 was present
            // at the top. Now, the new stack top
            // becomes 1.
    s.pop(); // removes 1 from the stack since 1 was present
            // at the top. Now, the stack becomes empty.
}


Python3




# Python Code:
stack = []
 
stack.append(1) # This pushes 1 to the stack top
 
stack.append(2) # This pushes 2 to the stack top
 
stack.append(3) # This pushes 3 to the stack top
 
stack.append(4) # This pushes 4 to the stack top
 
stack.append(5) # This pushes 5 to the stack top
 
 
# Now, let us remove elements from the stack using pop
# function
print(stack[-1], end=" ")
stack.pop() # removes 5 from the stack since 5 was present
            # at the top. Now, the new stack top
            # becomes 4.
print(stack[-1], end=" ")
stack.pop() # removes 4 from the stack since 4 was present
            # at the top. Now, the new stack top
            # becomes 3.
     
print(stack[-1], end=" ")
stack.pop() # removes 3 from the stack since 3 was present
            # at the top. Now, the new stack top
            # becomes 2.
     
print(stack[-1], end=" ")
stack.pop() # removes 2 from the stack since 2 was present
            # at the top. Now, the new stack top
            # becomes 1.
     
print(stack[-1], end=" ")
stack.pop() # removes 1 from the stack since 1 was present
            # at the top. Now, the stack becomes empty.
   
# This code is contributed by Sakshi


Output

5 4 3 2 1 





Top Operation in Stack:

Top operation is used to return the top element of the stack.

Below is a sample program to show Pop operation in Stack.

C++




#include <bits/stdc++.h>
using namespace std;
 
int topElement(stack<int>& s) { return s.top(); }
 
int main()
{
 
    stack<int> s; // creating a stack of integers
 
    s.push(1); // This pushes 1 to the stack top
    cout << topElement(s)
        << endl; // Prints 1 since 1 is present at the
                // stack top
 
    s.push(2); // This pushes 2 to the stack top
    cout << topElement(s)
        << endl; // Prints 2 since 2 is present at the
                // stack top
 
    s.push(3); // This pushes 3 to the stack top
    cout << topElement(s)
        << endl; // Prints 3 since 3 is present at the
                // stack top
}


Python3




# Python code:
 
def topElement(s):
    return s[-1]
 
s = [] # creating a stack of integers
 
s.append(1) # This pushes 1 to the stack top
print(topElement(s)) # Prints 1 since 1 is present at the stack top
 
s.append(2) # This pushes 2 to the stack top
print(topElement(s)) # Prints 2 since 2 is present at the stack top
 
s.append(3) # This pushes 3 to the stack top
print(topElement(s)) # Prints 3 since 3 is present at the stack top
 
# This code is contributed by Sakshi


Output

1
2
3






isEmpty Operation in Stack:

isEmpty operation is a boolean operation that is used to determine if the stack is empty or not.

This operation will return true if the stack is empty, else false.

Below is a sample program to show Pop operation in Stack.

C++




#include <bits/stdc++.h>
using namespace std;
 
bool isEmpty(stack<int>& s)
{
 
    bool isStackEmpty
        = s.empty(); // checking whether stack is empty or
                    // not and storing it into isStackEmpty
                    // variable
 
    return isStackEmpty; // returning bool value stored in
                        // isStackEmpty
}
 
int main()
{
 
    stack<int> s;
 
    // The if - else conditional statements below prints
    // "Stack is empty."
    if (isEmpty(s)) {
        cout << "Stack is empty." << endl;
    }
    else {
        cout << "Stack is not empty." << endl;
    }
 
    s.push(1); // Inserting value 1 to the stack top
 
    // The if - else conditional statements below prints
    // "Stack is not empty."
    if (isEmpty(s)) {
        cout << "Stack is empty." << endl;
    }
    else {
        cout << "Stack is not empty." << endl;
    }
}


Java




import java.util.Stack;
 
public class Main {
    public static boolean isEmpty(Stack<Integer> s) {
        return s.empty();
    }
 
    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>();
 
        if (isEmpty(s)) {
            System.out.println("Stack is empty.");
        } else {
            System.out.println("Stack is not empty.");
        }
 
        s.push(1);
 
        if (isEmpty(s)) {
            System.out.println("Stack is empty.");
        } else {
            System.out.println("Stack is not empty.");
        }
    }
}


Python3




# Python Code:
def isEmpty(s):
    isStackEmpty = len(s) == 0 # checking whether stack is empty or
                               # not and storing it into isStackEmpty variable
    return isStackEmpty # returning bool value stored in isStackEmpty
 
s = []
 
# The if - else conditional statements below prints "Stack is empty."
if isEmpty(s):
    print("Stack is empty.")
else:
    print("Stack is not empty.")
 
s.append(1) # Inserting value 1 to the stack top
 
# The if - else conditional statements below prints "Stack is not empty."
if isEmpty(s):
    print("Stack is empty.")
else:
    print("Stack is not empty.")
     
# This code is contributed by Sakshi


Output

Stack is empty.
Stack is not empty.






size() Operation in Stack:

Size operation in Stack is used to return the count of elements that are present inside the stack

Below is a sample program to show Pop operation in Stack.

C++




#include <bits/stdc++.h>
using namespace std;
 
int main()
{
 
    stack<int> s; // creating a stack of integers
 
    cout << s.size()
        << endl; // Prints 0 since the stack is empty
 
    s.push(1); // This pushes 1 to the stack top
    s.push(2); // This pushes 2 to the stack top
    cout << s.size() << endl; // Prints 2 since the stack
                            // contains two elements
 
    s.push(3); // This pushes 3 to the stack top
    cout << s.size() << endl; // Prints 3 since the stack
                            // contains three elements
}


Java




import java.util.Stack;
 
public class Main {
    public static void main(String[] args) {
        Stack<Integer> s = new Stack<>(); // creating a stack of integers
 
        System.out.println(s.size()); // Prints 0 since the stack is empty
 
        s.push(1); // This pushes 1 to the stack top
        s.push(2); // This pushes 2 to the stack top
        System.out.println(s.size()); // Prints 2 since the stack contains two elements
 
        s.push(3); // This pushes 3 to the stack top
        System.out.println(s.size()); // Prints 3 since the stack contains three elements
    }
}


Python3




# PYthon Code:
stack = [] # creating an empty list as a stack
 
print(len(stack)) # Prints 0 since the stack is empty
 
stack.append(1) # This appends 1 to the stack
stack.append(2) # This appends 2 to the stack
print(len(stack)) # Prints 2 since the stack contains two elements
 
stack.append(3) # This appends 3 to the stack
print(len(stack)) # Prints 3 since the stack contains three element
 
# This code is contributed by Sakshi


Output

0
2
3







Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 02 Nov, 2023
Like Article
Save Article
Similar Reads
Related Tutorials