linked list - C++ doesn't save the changes by other method -


i'm trying implement linked list class in c++ , got problem. have += operator adds new node.

the linked list class interface:

template <typename type>  class linkedlist { public:     linkedlist<type>* head; //  linked list stracture     type data;     linkedlist<type>* next; //  others ....     size_t length; public:     linkedlist();     ~linkedlist();     void initializehead(linkedlist<type>* headptr);     size_t size() const;     linkedlist& operator+=(const type& add);     void operator-=(const type& remove);     linkedlist<type>& operator[] (const size_t index) const;     bool operator== (const linkedlist<type> &versus) const;     friend ostream& operator<< (ostream& out,linkedlist& obj); }; 

and here have += overload implement:

template <typename type> linkedlist<type>& linkedlist<type>::operator +=(const type& add) {     // head ptr - :)     linkedlist<type>* p = head->next;     // go end     while(p) p = p->next;     // on end - create new..!!!     try {         p = new linkedlist<type>;     } catch (bad_alloc& e) {         cout << "there\'s allocation error....";     } catch (...) {         cout << "an unknown error.." << endl;     }// fill , done     p->data = add;     p->next = null;     // increment length .........     ++head->length;     // done ............     return *p; } 

additionally , have "array" access overload method:

template <typename type> linkedlist<type>& linkedlist<type>::operator [](const size_t index) const {     if(index < 0 || index >= length) // invaild argument         throw  exception();     // continue     linkedlist<type>* p = head;     for(size_t = 0; < index; ++i) p = p->next; // @ want     return *p; } 

all works correctly - checked on dibugger,

the problem - += doesn't save new node in "head->next", reason, after finish += method, head->next equal null.

do know why new allocation don't link head->next?

thanks lot!!

after while(p) p = p->next; p null

and next p = new linkedlist<type>; don't link p head.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -