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.
- 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;
void clearStack(stack< int >& s)
{
while (s.size() != 0) {
s.pop();
}
return ;
}
int main()
{
stack< int > s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
clearStack(s);
cout << s.size() << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void clearStack(Stack<Integer> s)
{
while (s.size() != 0 ) {
s.pop();
}
return ;
}
public static void main(String[] args)
{
Stack<Integer> s = new Stack<>();
s.push( 10 );
s.push( 20 );
s.push( 30 );
s.push( 40 );
s.push( 50 );
clearStack(s);
System.out.println(s.size());
}
}
|
Python3
def clearStack(s):
while ( len (s)! = 0 ):
s.pop()
return
s = []
s.append( 10 )
s.append( 20 )
s.append( 30 )
s.append( 40 )
s.append( 50 )
clearStack(s)
print ( len (s))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void ClearStack(Stack< int > s)
{
while (s.Count != 0)
{
s.Pop();
}
return ;
}
static public void Main ()
{
Stack< int > s = new Stack< int >();
s.Push(10);
s.Push(20);
s.Push(30);
s.Push(40);
s.Push(50);
ClearStack(s);
Console.WriteLine(s.Count);
Console.ReadLine();
}
}
|
Javascript
function clearStack(s)
{
while (s.length != 0) {
s.shift();
}
return ;
}
let s = [];
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
clearStack(s);
console.log(s.length);
|
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;
int main()
{
stack< int > s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s = stack< int >();
cout << s.size() << endl;
return 0;
}
|
Java
import java.util.Stack;
class GFG {
public static void main(String[] args)
{
Stack<Integer> s = new Stack<Integer>();
s.push( 10 );
s.push( 20 );
s.push( 30 );
s.push( 40 );
s.push( 50 );
s = new Stack<Integer>();
System.out.println(s.size());
}
}
|
Python3
s = []
s.append( 10 )
s.append( 20 )
s.append( 30 )
s.append( 40 )
s.append( 50 )
s = []
print ( len (s))
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static public void Main (){
Stack< int > s = new Stack< int >();
s.Push(10);
s.Push(20);
s.Push(30);
s.Push(40);
s.Push(50);
s = new Stack< int >();
Console.WriteLine(s.Count);
}
}
|
Javascript
let s = [];
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s= [];
console.log(s.length);
|
Time Complexity: O(1)
Please Login to comment...