Please specify class T when you declare the object, for example for 'int',
stack < type_name > stack_object_name;
template < class T >
class stack
{
struct node
{
T data;
struct node *next;
}*top;
public:
stack()
{
top=NULL;
}
void push(const T & value)
{
struct node *ptr;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
getch();
}
T pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
getch();
return;
}
temp=top;
top=top->next;
T t=temp->data;
delete temp;
return t;
}
void show()
{
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
getch();
return;
}
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<data<< " ->";
ptr1=ptr1->next;
}
}
};
No comments:
Post a Comment