CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Template list

Threaded View

  1. #1
    Join Date
    May 2013
    Posts
    5

    Template list

    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; 
    }
    Attached Images Attached Images  

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured