what is wrong in my code:
#include
#include
using namespace std;
int main () {
stack s;
long int n;cin>>n;
for(long int i=0;i<n;i++){
int data;cin>>data;
if(data==2){
long int d;cin>>d;s.push(d);
}
else{
s.empty() ? cout<<“No Code”:cout<<s.top();cout<<endl;
s.pop();
}
}
return 0;
}
Prateek sir and coding
Hello @sktg99,
There are two mistakes in your code:
-
The instantiation of stack object is wrong.
Reason:
It is a templated class. So, you have to specify the datatype also.
Solution:
stack s; -
There would be a run-time error for the case of query 1 when stack is empty.
Reason:
s.pop();
You cannot pop an empty stack
Solution:
if(!s.empty())
s.pop();
Hope, this would help.
Give a like if you are satisfied.