Hi everyone, we were provided all the following code and told to write the addLongInt() function. We are supposed to be adding numbers using a double-linked list. I understand double-linked list, however, I can't seem to understand the provide code at all. All I could add I knew wasn't completely wrong was LongInt sum and return sum. I feel like this would be a simple program without using his code (why do we need this nested class stuff?). My monkey brain is hurting and no one else in my group can figure it out either. Please help with as much explanation as possible. Thank you very much.
you don't need nested objects of this kind here, but you have to use what you have been given, probably. Not everything people do is necessary or awesome.
to add 2 things just add and carry like you did in the first grade..
123456789990
999933344112
-----------------
add 0 and 2, get 2, carry = 0.
add 9 and 1, get 0, carry 1.
add 9,1, and carry get 1, carry 1
...
if its a list you iterate the lists to get the digits.
The first issue is that the provided code doesn't have a destructor - so there are memory leaks! The second issue is that neither copy constructor nor copy assignment are provided. If they are not needed, than at least set them as deleted - otherwise if used there will be problems!
(PS. They are indeed needed. As provided, the code has serious issues as the used copy constructor is being done using a shallow copy rather than a required deep copy. This is a big problem).
The provided code IMO is quite poor!!
There is no need for Ibase which IMO just complicates the code!
In terms of double linked lists and usual names:
struct digit is a node.
d_val is the value
more is a pointer one way to the next digit(node)
less is a pointer the other way to the previous digit
Ibase.most is a pointer to one end of the list
Ibase.least is a pointer to the other end of the list
To iterate the list one way, start at Ibase.most and follow the .less pointers.
To iterate the other way, start at Ibase.least and follow the .more pointers.
There is no provided code to add a node to the list. I'd suggest this as a possible first start. Once you can add a new node at Ibase.most, then you can start coding addLongInt() based upon jonnin's post above.
Thanks for the help. There is still a lot I have to learn. I really appreciate the response. Our group had to submit it yesterday. The following is what we ending up going with. Let me know what you think, I would love some criticism.
if it works and you learned something, its a winner :)
if you want nitpicks..
the lack of comments is bad. what does it do? how is it doing it? give some hints here and there.
magic numbers are usually frowned upon. If you must use them, say why.
eg line 153ish .. why is it 10? what is special about 10? (yes, I know, and yes, you know, but it needs a comment).
it is not really normal for main to do all the work. Its ok in a small program but normally you want to break things into functions and have main be quite simple. You may not know how to do that yet, and that is ok.
at a drive-by glance the blocks in the loop look similar, and there are better ways (eg, functions as I already said) to handle similar code than to copy it and make small changes over and over.
those last 2 ideas ... its very weird to tackle a linked list and not know how to write functions and factor code into reusable blocks.
finally, doing large numbers digit by digit is the slow, brute force way. can you think of any way to do more work per loop iteration? The computer knows how to add up to 64 bit values already, in the cpu hardware...
Thanks for the tips. I took out most comments to make it look cleaner when I submitted it. My comments are always messy gibberish only I can understand. I have noticed other people using functions for everything. That's something I definitely need to work on.