#include <iostream>
using namespace std;
//Define the stack class, set default stack size to 2
//use a template to define type at later point
template<class T,int size=2>
class stack{
private: T data[size];
int stack_ptr;
public:
stack(void);
void push(T x);
T pop();
};
//constructor function to initialize stack and data
template<class T,int size>
stack<T,size>::stack(void)
{
int i;
for(i=0;i<size;i++) data[i]=0;
stack_ptr=0;
}
//Push data onto stack
template<class T,int size>
void stack<T,size>::push(T x)
{
if(stack_ptr>=size)
{
cout<<"cannot push data: stack full"<<endl;
return;
}
data[stack_ptr++]=x;
cout<<"Pushed\t" << x << "\tonto the stack"<<endl;
return;
}
//Pop data from stack
template<class T,int size>
T stack<T,size>::pop()
{
if(stack_ptr<=0)
{
cout<<"cannot pop data: stack empty"<<endl;
return data[0];
}
cout<<"popped\t"<< data[--stack_ptr]<< "\tfrom stack"<<endl;
return data[stack_ptr];
}
int main()
{
//create stack of integers
stack<int,10> s;
s.push(45);
s.pop();
//try to pop an empty stack
s.pop();
s.push(56);
s.push(29);
s.push(31);
s.pop();
s.pop();
//create stack of doubles
stack<double,2> d;
d.push(4.5);
d.push(5.9);
//trying to pushing on a full stack
d.push(7.2);
d.pop();
d.pop();
//trying to pop an empty stack
d.pop();
//declaring stack of characters and using default size
stack<char> c;
char w;
c.push('n');
c.push('l');
//trying to push on full stack
c.push('w');
c.pop();
//pick up the stack value
w=c.pop();
cout<<"Picked the character ***"<< w << "*** which was popped from top"<<endl;
//trying to pop an empty stack
c.pop();
return 0;
}
using namespace std;
//Define the stack class, set default stack size to 2
//use a template to define type at later point
template<class T,int size=2>
class stack{
private: T data[size];
int stack_ptr;
public:
stack(void);
void push(T x);
T pop();
};
//constructor function to initialize stack and data
template<class T,int size>
stack<T,size>::stack(void)
{
int i;
for(i=0;i<size;i++) data[i]=0;
stack_ptr=0;
}
//Push data onto stack
template<class T,int size>
void stack<T,size>::push(T x)
{
if(stack_ptr>=size)
{
cout<<"cannot push data: stack full"<<endl;
return;
}
data[stack_ptr++]=x;
cout<<"Pushed\t" << x << "\tonto the stack"<<endl;
return;
}
//Pop data from stack
template<class T,int size>
T stack<T,size>::pop()
{
if(stack_ptr<=0)
{
cout<<"cannot pop data: stack empty"<<endl;
return data[0];
}
cout<<"popped\t"<< data[--stack_ptr]<< "\tfrom stack"<<endl;
return data[stack_ptr];
}
int main()
{
//create stack of integers
stack<int,10> s;
s.push(45);
s.pop();
//try to pop an empty stack
s.pop();
s.push(56);
s.push(29);
s.push(31);
s.pop();
s.pop();
//create stack of doubles
stack<double,2> d;
d.push(4.5);
d.push(5.9);
//trying to pushing on a full stack
d.push(7.2);
d.pop();
d.pop();
//trying to pop an empty stack
d.pop();
//declaring stack of characters and using default size
stack<char> c;
char w;
c.push('n');
c.push('l');
//trying to push on full stack
c.push('w');
c.pop();
//pick up the stack value
w=c.pop();
cout<<"Picked the character ***"<< w << "*** which was popped from top"<<endl;
//trying to pop an empty stack
c.pop();
return 0;
}
No comments:
Post a Comment