I am new to the templates..
I was trying to implement templates in my x programs....
and i am stuck ...
help me...
problem is with the "search" function
when i was not using templates
search function was
Stack::Nodeptr Stack::search(char item)
and worked perfectly fine..
when using templates...
i tried to do..
template <class Dtype>
Stack<Dtype>::Nodeptr Stack<Dtype>::search(Dtype item)
but it doesn't bug...
i have also tried other combination but :(
Program is like this
template <class Dtype>
class Stack
{
protected:
struct Node
{
Dtype info;
Node *link;
};
typedef Node *Nodeptr;
Nodeptr front;
public:
Stack();
void push(Dtype);
Dtype pop();
Dtype peep();
Nodeptr search(Dtype);
void display();
void menu();
};
template <class Dtype>
Stack<Dtype>::Stack()
{
front=NULL;
}
template <class Dtype>
Dtype Stack<Dtype>::peep()
{
if(front==NULL) return '\0';
return front->info;
}
template <class Dtype>
void Stack<Dtype>::push(Dtype data)
{
Nodeptr temp=new Node;
temp->info=data;
temp->link=front;
front=temp;
}
template <class Dtype>
Dtype Stack<Dtype>::pop()
{
if(front==NULL) return '\0';
Nodeptr temp=front;
front=front->link;
int r=temp->info;
delete temp;
return r;
}
/* this guy has the problem
template <class Dtype>
Stack<Dtype>::Nodeptr Stack<Dtype>::search(Dtype item)
{
Nodeptr traverse=front;
while(traverse!=NULL)
{
if(traverse->info==item) return traverse;
traverse=traverse->link;
}
return NULL;
}
*/
template <class Dtype>
void Stack<Dtype>::display()
{
Nodeptr traverse=front;
while(traverse!=NULL)
{
cout<<traverse->info<<(char)26;
traverse=traverse->link;
}
cout<<(char)237;
}
template <class Dtype>
void Stack<Dtype>::menu()
{
Dtype data;
for(;;)
{
cls;
cout<<"Stack\n"
<<"\n1. Push"
<<"\n2. Pop"
<<"\n3. Peep"
<<"\n4. Search //problem lies here"
<<"\n5. Display"
<<"\n\nEnter your choice: ";
switch(getch())
{
case '1':
case 13:
cls;
cout<<"Enter any no: "; cin>>data;
push(data);
break;
case '2':
cls;
cout<<pop()<<" The Element is deleted";
getch();
break;
case '3':
cls;
cout<<peep();
getch();
break;
/*since the part is not working i commented it
case '4':
cls;
cout<<"Enter any no: "; cin>>data;
Nodeptr temp;
temp=search(data);
if(temp!=NULL) cout<<temp->info<<" is found !!";
else cout<<data<<" is not found !!";
getch();
break;
*/
case '5':
cls;
display();
getch();
break;
case 27:
return;
};
}
}