#include "stdafx.h"
#include<iostream>
using namespace std;
template<class stype>
class Stack
{
private:
struct node
{
stype data;
node * next;
}*q;
public:
Stack()
{
q=NULL;
}
void push(stype d)
{
struct node * temp;
temp=q;
temp=new node;
temp->data=d;
if(q==NULL)
{
temp->next=NULL;
q=temp;
}
else
{
temp->next=q;
q=temp;
}
}
void pop()
{
struct node *temp;
temp=q;
if(q==NULL)
{
cout<<"Stack Empty"<<endl;
}
else
{
cout<<"Popped element :\t";
cout<<temp->data<<endl;
q=q->next ;
free(temp);
}
}
~Stack()
{
delete q;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Stack<int> S;
int cho,i;
while(1)
{
cout<<"enter choice"<<endl;
cout<<"1--- push"<<endl;
cout<<"2-----pop"<<endl;
cout<<"3----exit"<<endl;
cin>>i;
switch(i)
{
case 1:
cout<<"enter digit"<<endl;
cin>>cho;
S.push(cho);
break;
case 2:
S.pop();
break;
case 3:
exit(1);
}
}
return 0;
}
hi all,
I have made this template class for implementing stack (type int) .
now i want my class to take both char and int as data .
as for now its taking int only as i have instantiated the template as Stack<int> IntObject
;
and for taking char i have to make it Stack<char>CharObject
;
but i want my class to take both char and int in same object.
i dont want the code i just need help .
TIA