Write your question here.
Hello, I'm doing a homework aasignment on templates, and i have to build a list.
The problem starts when i am trying to add elements to the list. For instance if i chose to add 5 different elements (1,2,3,4,5) the output will be (5,5,5,5,5).
I have no idea how to fix it. Thank you for all the help!
Code:void add_back(T t){
Node* tmp = new Node;
tmp -> m_data = &t;
if(m_head == NULL)
{
m_head = tmp;
m_tail = m_head;
m_size += 1;
}
else
{
m_tail -> m_next = tmp;
tmp -> m_prev = m_tail;
m_tail = tmp;
m_size += 1;
}
}
Code:void add_front(T t){
Node* tmp = new Node;
tmp -> m_data = &t;
if(m_head == NULL)
{
m_head = tmp;
m_size += 1;
}
else
{
m_head -> m_prev = tmp;
tmp -> m_next = m_head;
m_head = tmp;
m_size += 1;
}
}
Code:int main()
{
List<int> list;
if(!list.isEmpty())
cout<<"Error"<<endl;
list.add_back(5);
list.add_back(21);
list.add_back(34);
list.add_front(70);
cout<<list.size()<<endl; //size is 1
if(list.isEmpty())
cout<<"Error"<<endl;
cout<<list.pop_back()<<endl;
if(!list.isEmpty())
cout<<"Error"<<endl;
}