-
Linked List Help
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:
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);
}
}
Thanks alot!!!
-
Can you show us the context in which you're using this.
If you're doing something like
foo = foo;
Then I can see some deep problems in the code, like forever trying to append to itself.
-
copy constr
I call the copy constructor in the program by declaring
Code:
Sequence s2 = s; /*s is a linked with several nodes that i want to
*/copy into s2
-
You don't show start() or the Node constructor so I can't say what you do in there. However, in general, to copy the data from one list into another in the same sequence I'd first make sure that the old list isn't empty. If it is then no copying needed. If it isn't, since you keep track of the tail node, you don't need to run the list. If tail node is NULL then new list is empty so assign an initialized new node to head node and tail node. If tail node is not NULL, then assign an initialized node to tail->next and then assign tail->next to tail. What you do with cur and prev when you append each new node to tail is up to you. I'd repeat the process advancing from head node of old list to tail node of old list.