Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include<iostream>

#include<stdlib.h>
using namespace std;
struct node
{
int info;
node*next;
};
class stack
{
private:
node*top;
public:
stack();
~stack();
void push(int ele);
int pop();
void display();
bool is_stack_empty();
};
stack::stack()
{
top=NULL;
}
bool stack::is_stack_empty()
{
if(top=NULL)
return 0;
else
return false;
}
void stack::push(int ele)
{
node*p;
p=new node;
if(p==NULL)
{
cout<<"stack is full"<<endl;
return;
}
p->info=ele;
p->next=NULL;
p->next=top;
top=p;
}
int stack::pop()
{
int x;
node*p;
p=top;
top=p->next;
x=p->info;
delete p;
cout<<"deleted"<<endl;
return x;
}
stack::~stack()
{
cout<<"stack is destroyed"<<endl;
if(!is_stack_empty() )
{
node*p;
p=top;
while(p!=NULL)
{
top=p->next;
delete p;
p=top;
}
}
}
int main()
{
stack s;
s.push(11);
s.push(22);
s.push(33);
s.display();
cout<<s.pop()<<endl;
cout<<s.pop()<<endl;
s.display();
return 0;
}

You might also like