Open In App

How to Clear a Stack in C++?

Last Updated : 07 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, stacks are a type of container adaptor with a LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. In this article, we will learn to clear all the elements from a stack in C++.

Example:

Input:
myStack = 40, 30, 20, 10

Output:
After clearing - Updated Stack: [Empty]

Clear a Stack in C++

To clear all elements from a stack in C++, we can use the std::stack::pop() function in the while loop till the stack becomes empty. When the stack empty condition is satisfied, it indicates that all the elements from a stack are cleared.

C++ Program to Clear a Stack

C++
// C++ Program to illustrate how to clear all elements from 
// a stack 
#include <iostream> 
#include <stack> 
using namespace std; 

// Driver Code 
int main() 
{ 
    // Creating a stack of elements 
    stack<int> stackData; 

    // Adding elements to the stack 
    stackData.push(10); 
    stackData.push(20); 
    stackData.push(30); 
    stackData.push(40); 

    // Printing the original stack 
    cout << "Original Stack: "; 
    while (!stackData.empty()) { 
        cout << stackData.top() << " "; 
        stackData.pop(); 
    } 
    cout << endl; 

    // Clearing all elements from the stack 
    cout << "After clearing - Updated Stack: "; 
    while (!stackData.empty()) { 
        stackData.pop(); 
    } 

    // Printing the updated stack. Empty 
    cout << "[Empty]" << endl; 

    return 0; 
}

Output
Original Stack: 40 30 20 10 
After clearing - Updated Stack: [Empty]

Time Complexity: O(N), where N is the number of elements.
Auxiliary Space: O(1)

Clear Stack by Assigning a New Empty Stack in C++

We can also assign a new empty stack to your stack to clear all its elements in one go.

C++ Program to Clear a Stack

C++
// C++ Program to illustrate how to clear all elements from
// a stack
#include <iostream>
#include <stack>
using namespace std;

// Driver Code
int main()
{
    // Creating a stack of elements
    stack<int> stackData;

    // Adding elements to the stack
    stackData.push(10);
    stackData.push(20);
    stackData.push(30);
    stackData.push(40);

    // Printing the original stack
    cout << "Original Stack: ";
    while (!stackData.empty()) {
        cout << stackData.top() << " ";
        stackData.pop();
    }
    cout << endl;

    // Clearing all elements from the stack
    // By assigning new empty stack
    stackData = stack<int>();

    // Printing the size of updated stack
    cout << "After clearing - Size of Updated Stack: "
         << stackData.size() << endl;

    return 0;
}

// This code is contributed by Susobhan Akhuli

Output
Original Stack: 40 30 20 10 
After clearing - Size of Updated Stack: 0

Time Complexity: O(1)
Auxiliary Space: O(1)





Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads