#include<iostream.h>
#include<conio.h>
const int size=10;
class stack
{
int arr[size];
int top;
public:
void initialize();
void push(int);
int pop();
int stack_top();
void show();
};
void stack::initialize()
{
top=-1;
}
void stack::push(int n)
{
if(top!=size-1)
arr[++top]=n;
else
cout<<"\nOverflow!!!\n";
}
int stack::pop()
{
if(top!=-1)
return arr[top--];
else
{
cout<<"\nUnderflow!!!\n";
return NULL;
}
}
int stack::stack_top()
{
if(top==-1)
{
cout<<"\nStack is Empty!\n";
return NULL;
}
else
return arr[top];
}
void stack::show()
{
if(top==-1)
cout<<"\nEmpty Stack!!!\n";
else
{
for(int i=0;i<=top;i++)
cout<<arr[i]<<" ";
cout<<"<--TOP\n";
}
}
int main()
{
int p;
stack s1;
s1.initialize();
//Pushing Values
cout<<"\nPUSHING 3,5,7 onto stack\n";
s1.push(3);
s1.push(5);
s1.push(7);
cout<<"\nStack is : ";
s1.show();
//Show Top Value
cout<<"\nTop of Stack is :"<<s1.stack_top()<<endl;
//Popping Values
cout<<"\nPOPPING\n";
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
//Show Top of Stack
p=s1.stack_top();
if(p!=NULL)
cout<<"Top of Stack is : "<<p<<endl;
return 0;
}
Output:
PUSHING 3,5,7 onto stack
Stack is : 3 5 7 <--TOP
Top of Stack is :7
POPPING
Popped out value is : 7
Popped out value is : 5
Popped out value is : 3
Stack is Empty!
#include<conio.h>
const int size=10;
class stack
{
int arr[size];
int top;
public:
void initialize();
void push(int);
int pop();
int stack_top();
void show();
};
void stack::initialize()
{
top=-1;
}
void stack::push(int n)
{
if(top!=size-1)
arr[++top]=n;
else
cout<<"\nOverflow!!!\n";
}
int stack::pop()
{
if(top!=-1)
return arr[top--];
else
{
cout<<"\nUnderflow!!!\n";
return NULL;
}
}
int stack::stack_top()
{
if(top==-1)
{
cout<<"\nStack is Empty!\n";
return NULL;
}
else
return arr[top];
}
void stack::show()
{
if(top==-1)
cout<<"\nEmpty Stack!!!\n";
else
{
for(int i=0;i<=top;i++)
cout<<arr[i]<<" ";
cout<<"<--TOP\n";
}
}
int main()
{
int p;
stack s1;
s1.initialize();
//Pushing Values
cout<<"\nPUSHING 3,5,7 onto stack\n";
s1.push(3);
s1.push(5);
s1.push(7);
cout<<"\nStack is : ";
s1.show();
//Show Top Value
cout<<"\nTop of Stack is :"<<s1.stack_top()<<endl;
//Popping Values
cout<<"\nPOPPING\n";
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
p=s1.pop();
if(p!=NULL)
cout<<"\nPopped out value is : "<<p<<endl;
//Show Top of Stack
p=s1.stack_top();
if(p!=NULL)
cout<<"Top of Stack is : "<<p<<endl;
return 0;
}
Output:
PUSHING 3,5,7 onto stack
Stack is : 3 5 7 <--TOP
Top of Stack is :7
POPPING
Popped out value is : 7
Popped out value is : 5
Popped out value is : 3
Stack is Empty!
No comments:
Post a Comment