singly linked list constructor and assignment operator question

Hi everyone -

I was trying to study data structure in c++ by myself, and was following this homework
http://www-cs.engr.ccny.cuny.edu/~wolberg/cs212/hw/hw4.html

I believe I got most of them correct, except there's a strange problem with my copy constructor and assignment operator. I tried my best to debug this problem and I found that the advance() method (move cursor and precursor to next) does not work for strange reason, and it only appears in copy constructor and assignment operator (I cout current() before and after advance(), and it does not work). I might be wrong though.

I would really like to know if I'm on the right track, and what's the problem behind this bug. I appreciate any feedback from you. Thanks!

the file is rather lengthy, so I'm sharing them here.
https://gist.github.com/6772eea3244f64e4847a

Thank you!
Last edited on
http://www.cplusplus.com/forum/general/112111/
seq_ex3.cpp:116:29: error: declaration of ‘void operator delete(void*)’ has a different exception specifier
/usr/include/c++/4.9.0/new:132:6: error: from previous declaration ‘void operator delete(void*) noexcept’

sequence3.cpp: In member function ‘main_savitch_5::sequence::value_type main_savitch_5::sequence::current() const’:
sequence3.cpp:146:1: warning: control reaches end of non-void function 



> I found that the advance() method does not work for strange reason,
> and it only appears in copy constructor and assignment operator
I need a better description than `does not work'
Also, `advance()' is used in other tests and in the `correct()' function.


> the file is rather lengthy
learn to isolate the problem. ¿which test number is the one giving issue? ¿do I need to run the previous tests in order to reproduce the problem?


> I cout current() before and after advance(), and it does not work
¿do you realize the amount of output that your program has?
¿how do you expect us to find that "debug" output? At least use a different stream (like cerr or clog, or a file)

And again, ¿what does `it does not work' mean?
Hi thanks for replying!

I did some additional test last night, and I'm sure the problem was with my constructor and =operator, however, not with the advance() method. for some reason, whenever I copy the linked-list sequence when cursor is at the beginning of the sequence, it copies one extra first element.
for instance,

sequence original is {1,2,3,4,5,6,7}
sequence copy(original)
copy is {1,1,2,3,4,5,6,7}
and the cursor stay with the second 1.

thanks for helping me out, I'll try to go through the logic again to see if i was missing anything.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr)
	{
		//...

		list_head_insert(head_ptr, source_ptr->data()); //first insertion
		tail_ptr = head_ptr;
		//source_ptr keeps pointing to the same location
		
		// Copy the rest of the nodes one at a time, adding at the tail of new list.
		while (source_ptr != NULL)
		{	
			list_insert(tail_ptr, source_ptr->data()); //second insertion
			tail_ptr = tail_ptr->link();
			source_ptr = source_ptr->link();
		}
	}


Using a circular list with header cell would avoid having to code all those especial cases.
Last edited on
i figured it out. I should advance source_ptr before looping. don't know why i didn't catch that at first. thanks for helping out though!
Topic archived. No new replies allowed.