#include<iostream>
using namespace std;
///////////////////////////////////// this is our stack class of type template ///////////////////////////////////////////////////
template <class type>
class Stack{
int top;
int max;
type * array;
public:
Stack(int n = 10);
bool push (type data);
bool pop(type & data);
bool isFull();
bool isEmpty();
~Stack();
};
template <class type>
Stack<type>::Stack(int n){
top = -1;
max = n;
array = new type[max];
}
template <class type>
bool Stack<type>::push (type data){
if(!isFull()){
array[++top] = data;
return true;
}
else
return false;
}
template <class type>
bool Stack<type>::pop(type & data){
if(!isEmpty()){
data = array[top--];
return true;
}
return false;
}
template <class type>
bool Stack<type>::isFull(){
if(top == max-1)
return true;
else
return false;
}
template <class type>
bool Stack<type>::isEmpty(){
if(top==-1)
return true;
else
return false;
}
template <class type>
Stack<type>:: ~Stack(){
delete [] array;
}
////////////////////////////////////////////stack functions ends here ////////////////////////////////////////////////////
int main()
{
Stack<int> st; // this is how u instantiate a template stack in this exmple this become an integer stack
//Stack<char> st; //char stack
//Stack<bool> st; // boolian stack
//Stack<float> st; // float stack
st.push(10);
st.push(15);
st.push(16);
int temp=0;
st.pop(temp);
cout<<"Popped item : "<<temp<<endl;
system("PAUSE");
return 0;
}
No comments:
Post a Comment