Open In App

How to clear elements from a Stack efficiently?

Last Updated : 24 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Stacks are a type of data structure that follows the LIFO(Last In First Out) working principle, where a new element is added at one end (top) and a part is removed from that end only.

Here we will be discussing some processes of clearing a stack as there is not default clear() function to remove elements from the stack.

Clear Stack using a loop:

The Basic idea is to iterate over the stack and pop out all elements until the stack is empty.

  •  Check whether the stack is empty or not.
    •  Pop the element out.
  •  Repeat the above two steps until the stack is not empty.

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
 
// Function to clear stack
void clearStack(stack<int>& s)
{
    while (s.size() != 0) {
        s.pop();
    }
    return;
}
 
// Driver Code
int main()
{
    // Initializing a stack
    stack<int> s;
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);
    s.push(50);
 
    // Calling clearStack function to clear the stack
    clearStack(s);
 
    cout << s.size() << endl;
 
    return 0;
}


Java




// Java code for above approach
 
import java.io.*;
import java.util.*;
 
class GFG {
 
    // Function to clear stack
    static void clearStack(Stack<Integer> s)
    {
        while (s.size() != 0) {
            s.pop();
        }
        return;
    }
 
    public static void main(String[] args)
    {
 
        // Initializing a stack
        Stack<Integer> s = new Stack<>();
        s.push(10);
        s.push(20);
        s.push(30);
        s.push(40);
        s.push(50);
 
        // Calling clearStack function to clear the stack
        clearStack(s);
 
        System.out.println(s.size());
    }
}
 
// This code is contributed by lokesh.


Python3




# Python code for above approach
 
# Function to clear stack
def clearStack(s):
    while(len(s)!=0):
        s.pop()
    return
 
# Driver Code
 
# Initializing a stack
s=[]
s.append(10)
s.append(20)
s.append(30)
s.append(40)
s.append(50)
 
# Calling clearStack function to clear the stack
clearStack(s)
print(len(s))
 
# This code is contributed by Pushpesh Raj.


C#




using System;
using System.Collections.Generic;
 
public class GFG{
  // Function to clear stack
  static void ClearStack(Stack<int> s)
  {
    while (s.Count != 0)
    {
      s.Pop();
    }
    return;
  }
 
  static public void Main ()
  {
     
    // Initializing a stack
    Stack<int> s = new Stack<int>();
    s.Push(10);
    s.Push(20);
    s.Push(30);
    s.Push(40);
    s.Push(50);
 
    // Calling clearStack function to clear the stack
    ClearStack(s);
 
    Console.WriteLine(s.Count);
 
    Console.ReadLine();
  }
}
 
// This code is contributed by akashish__


Javascript




// Function to clear stack
function clearStack(s)
{
    while (s.length != 0) {
        s.shift();
    }
    return;
}
 
// Driver Code
 
// Initializing a stack
let s = [];
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
 
// Calling clearStack function to clear the stack
clearStack(s);
 
console.log(s.length);
 
// This code is contributed by akashish__


Output

0

Time Complexity: O(N) where N is the size of the stack
Auxiliary Space: O(1)

Clear Stack by assigning a new empty Stack:

We can assign a new empty stack to the same declared variable. It uses move assignment rather than removing elements one by one. But the problem with this one is, the old stack is not actually removed and that is still residing in the memory. So it wastes some memory.

Below is the implementation of the idea.

C++




#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    // Initializing a stack
    stack<int> s;
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);
    s.push(50);
 
    // By assigning new empty stack
    s = stack<int>();
 
    // Printing the size of stack
    cout << s.size() << endl;
 
    return 0;
}


Java




/*package whatever //do not write package name here */
import java.util.Stack;
 
class GFG {
  public static void main(String[] args)
  {
 
    // Initializing a stack
    Stack<Integer> s = new Stack<Integer>();
    s.push(10);
    s.push(20);
    s.push(30);
    s.push(40);
    s.push(50);
 
    // By assigning new empty stack
    s = new Stack<Integer>();
 
    // Printing the size of stack
    System.out.println(s.size());
  }
}
 
// This code is contributed by akashish__


Python3




# Python code for above approach
 
# Driver Code
 
# Initializing a satck
s=[]
s.append(10)
s.append(20)
s.append(30)
s.append(40)
s.append(50)
 
# By assigning new empty stack
s=[]
 
# Printing the size of stack
print(len(s))
 
# This code is contributed by Pushpesh Raj.


C#




using System;
using System.Collections.Generic;
 
public class GFG{
 
  static public void Main (){
 
    // Initializing a stack
    Stack<int> s = new Stack<int>();
    s.Push(10);
    s.Push(20);
    s.Push(30);
    s.Push(40);
    s.Push(50);
 
    // By assigning new empty stack
    s = new Stack<int>();
 
    // Printing the size of stack
    Console.WriteLine(s.Count);
 
  }
}
 
// This code is contributed by akashish__


Javascript




// Initializing a stack
let s = [];
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
 
// By assigning new empty stack
s= [];
 
// Printing the size of stack
console.log(s.length);
 
 
// This code is contributed by akashish__


Output

0

Time Complexity: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads