Im attempting my first linked list and it is near completion but i have a few error i cant understand. i have a attach, insert, remove_current functions, which add and remove elements from the list, i believe i got those working fine, but my copy constructor loops infinently when i try to clone the list. heres what i got
Code:void Sequence::insert(const Sequence::value_type &entry) { if(head == NULL){ //Insertion for first item head = new Node(entry, head); tail = head; cur = head; ++many_nodes; return; } else if(cur == head || cur == NULL){ head = new Node(entry, head); cur = head; ++many_nodes; return; } else{ pre->next = new Node(entry, cur); cur = pre->next; ++many_nodes; } }Code:void Sequence::attach(const value_type &entry) { if( head == NULL){ head = new Node(entry, head); cur = head; tail = head; ++many_nodes; return; } else if (cur == tail) { cur->next = new Node(entry, cur->next); pre = cur; cur = cur->next; tail = cur; ++many_nodes; return; } else if (cur == NULL){ tail->next = new Node(entry, tail->next); tail = tail->next; cur = tail; ++many_nodes; return; } else{ cur->next = new Node(entry, cur->next); pre = cur; cur = cur->next; } ++many_nodes; }Code:void Sequence::remove_current() throw(std::out_of_range) { Node * tmp = NULL; if(cur == NULL) return; if(cur == head && tail == head){ tmp = head; head = head->next; cur = head; tail = head; --many_nodes; return; } if(cur == head){ tmp = head; head = head->next; cur = head; delete tmp; --many_nodes; return; } else if (cur == tail){ tmp = cur; cur = cur->next; tail = pre; delete tmp; --many_nodes; return; } else{ tmp = cur; cur = cur->next; pre->next = cur; delete tmp; } --many_nodes; }
Copy Constructor:
Thanks alot!!!Code:Sequence::Sequence(const Sequence& from) : head(NULL), tail(NULL), cur(NULL), pre(NULL), many_nodes(0) { do_copy(from); } void Sequence::do_copy(const Sequence& from) { start(); for(Node * p = from.head; p != NULL; p = p->next){ attach(p->data); } }



LinkBack URL
About LinkBacks


