I can't see what's wrong with it. Does it have to do with printitem() function? Here's my header. Hope that someone will help me since I'm a bit lost.
#ifndef list_h
#define list_h
template<class t>
class list
{
private:
class node
{
public:
t data;
node*next;
};
node*head;
node*current;
int numitem;
public:
list();
~list();
void addtofront();
void addinmiddle();
bool traverse(t,int&);
void printdata();
int numberofitem();
};
template<class t>
list<t>::list()
{
numitem=0;
head=0;
};
template<class t>
list<t>::~list(){};
template<class t>
void list<t>::addtofront()
{
t item;
node*newnode=new node;
cout<<"enter data: ";
cin>>item;
newnode->data=item;
newnode->next=head;
head=newnode;
numitem++;
};
template<class t>
void list<t>::printdata()
{
current=head;
while(current !=0)
{
cout<<current->data<<" ";
current=current->next;
}
cout<<endl;
};
template<class t>
int list<t>::numberofitem()
{
return numitem;
};
template<class t>
void list<t>::addinmiddle()
{
t item;
t node_number;
cout<<"enter the item location: ";
cin>>node_number;
node*temp1;
temp1=(node*)malloc(sizeof(node));
temp1=head;
for(int i=1;i<node_number;i++)
{
temp1->next;
if(temp1==NULL)
{
cout<<node_number<<" location is not exist"<<endl;
break;
}
}
cout<<"enter new item: ";
cin>>item;
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=item;
temp->next=temp1->next;
temp1->next=temp;
};
template<class t>
bool list<t>::traverse(t target,int &loc)
{
if(numitem==0)
{
cout<<"there is no item in the list. "<<endl;
}
else
{
current=head;
loc=0;
while(current->data !=target&& current->next !=0)
{
current=current->next;
++loc;
}
if(current->data==target)
return true;
else
return false;
}
};
#endif