perform following Operation on stack of size 5. push(30). push(12). push(10). pop(). push(45). pop(70)
Answers
Answered by
7
// output: 70 45 12 30
#include <iostream>
using namespace std;
template<class T>
class stack{
int top;
public:
T a[5];
stack(){
top=-1;
}
void push(T x);
T pop();
void isEmpty();
void display();
};
template<class T>
void stack<T>::push(T x){
if(top>=5){
cout<<"stack is overflow \n";
}
else{
a[++top]=x;
cout<<"Element Inserted "<<x<<"\n";
}
}
template<class T>
T stack<T>::pop(){
if(top<0){
cout<<"stack underflow \n";
}
else{
T d=a[top--];
return d;
}
}
template<class T>
void stack<T>::isEmpty(){
if(top<0){
cout<<"stack is empty \n";
}
else{
cout<<"Stack is not empty";
}
}
template<typename T>
void stack<T>::display(){
if(top<0){
cout<<"Stack is xempty";
}
else{
for(int x=top;x>=0;x--){
cout<<"stack content\n";
cout<<a[x]<<endl;
}
}
}
int main(){
// integer type stack built
stack<int> s1;
s1.push(30);
s1.push(12);
s1.push(10);
s1.pop();
s1.push(45);
s1.pop(70);
s1.display();
}
#include <iostream>
using namespace std;
template<class T>
class stack{
int top;
public:
T a[5];
stack(){
top=-1;
}
void push(T x);
T pop();
void isEmpty();
void display();
};
template<class T>
void stack<T>::push(T x){
if(top>=5){
cout<<"stack is overflow \n";
}
else{
a[++top]=x;
cout<<"Element Inserted "<<x<<"\n";
}
}
template<class T>
T stack<T>::pop(){
if(top<0){
cout<<"stack underflow \n";
}
else{
T d=a[top--];
return d;
}
}
template<class T>
void stack<T>::isEmpty(){
if(top<0){
cout<<"stack is empty \n";
}
else{
cout<<"Stack is not empty";
}
}
template<typename T>
void stack<T>::display(){
if(top<0){
cout<<"Stack is xempty";
}
else{
for(int x=top;x>=0;x--){
cout<<"stack content\n";
cout<<a[x]<<endl;
}
}
}
int main(){
// integer type stack built
stack<int> s1;
s1.push(30);
s1.push(12);
s1.push(10);
s1.pop();
s1.push(45);
s1.pop(70);
s1.display();
}
nitish8089:
pop 10 hua hai...
Similar questions