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 stackvoid clearStack(stack<int>& s){Â Â Â Â while (s.size() != 0) {Â Â Â Â Â Â Â Â s.pop();Â Â Â Â }Â Â Â Â return;}Â
// Driver Codeint 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 stackdef clearStack(s):Â Â Â Â while(len(s)!=0):Â Â Â Â Â Â Â Â s.pop()Â Â Â Â returnÂ
# Driver CodeÂ
# Initializing a stacks=[]s.append(10)s.append(20)s.append(30)s.append(40)s.append(50)Â
# Calling clearStack function to clear the stackclearStack(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 stackfunction clearStack(s){Â Â Â Â while (s.length != 0) {Â Â Â Â Â Â Â Â s.shift();Â Â Â Â }Â Â Â Â return;}Â
// Driver CodeÂ
// Initializing a stacklet s = [];s.push(10);s.push(20);s.push(30);s.push(40);s.push(50);Â
// Calling clearStack function to clear the stackclearStack(s);Â
console.log(s.length);Â
// This code is contributed by akashish__ |
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 codeint 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 satcks=[]s.append(10)s.append(20)s.append(30)s.append(40)s.append(50)Â
# By assigning new empty stacks=[]Â
# Printing the size of stackprint(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 stacklet s = [];s.push(10);s.push(20);s.push(30);s.push(40);s.push(50);Â
// By assigning new empty stacks= [];Â
// Printing the size of stackconsole.log(s.length);Â
Â
// This code is contributed by akashish__ |
0
Time Complexity: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
