C ++ STL pila función :: vacía () con el ejemplo : En este artículo, vamos a ver cómo comprobar si una pila está vacía o no usar C STL ++ ?
Prototipo:
stack<T> st; //declaration
st.empty();
Parámetro:
No parameter passed
class escribir: Bool (Verdadero o Falso)
- Verdadero: La pila está vacía
- Falso: La pila es no vaciar
archivo
encabezado que se incluirá:
#include <iostream>
#include <stack>
OR
#include <bits/stdc++.h>
Uso:
Los
función comprueba si una pila está vacía o no.
complejidad Tiempo: O (1)
Ejemplo:
For a stack of integer,
stack<int> st;
st.push(4);
st.push(5);
stack content:
5 <-- TOP
4
IF (st.empty())
Print "Stack is empty"
Else
Print "Stack is not empty"
Output:
Prints "Stack is not empty"
st.pop()
st.pop()
Stack content:
Empty stack
IF (st.empty())
Print "Stack is empty"
Else
Print "Stack is not empty"
Output:
Prints "Stack is empty"
C ++ aplicación:
#include <bits/stdc++.h>
using namespace std;
int main(){
cout<<"...use of empty function...n";
int count=0;
stack<int> st; //declare the stack
st.push(4); //pushed 4
st.push(5); //pushed 5
st.push(6);
cout<<"stack elements are:n";
while(!st.empty()){//stack not empty
cout<<"top element is:"<<st.top()<<endl;//print top element
st.pop();
count++;
}
if(st.empty()) //to check for empty stack
cout<<"stack emptyn";
cout<<count<<" pop operation performed total to make stack emptyn";
return 0;
}
salida
...use of empty function...
stack elements are:
top element is:6
top element is:5
top element is:4
stack empty
3 pop operation performed total to make stack empty