BigInt

This is a discussion on BigInt within the C Programming forums, part of the General Programming Boards category; I am doing the classic bigint problem adding and subtracting long integers via a linked list. I am having problems ...

BigInt

I am doing the classic bigint problem adding and subtracting long integers via a linked list. I am having problems getting each digit individually into a linked list. Because the list goes from L>R and we add/subtract from R>L, I have to reverse them, which works fine. But now my program crashes and I can't figure out why nor can I figure out how to get each digit individually into a separate node of the list. I know that I need a loop somewhere inside my function (right?) but I just can't figure out the syntax at all. Can someone please point me in the right direction?

If you're using an int to store one decimal digit only, there'll be a lot of RAM waste. Does your teacher really want you to store every digit separately?! Does he want a number that keeps adapting its size everytime it gets bigger or smaller?!

No!! A data type like BigNum must remain in one size after it's created! Another issue is: How will you represent negative numbers?

But now my program crashes and I can't figure out why nor can I figure out how to get each digit individually into a separate node of the list. I know that I need a loop somewhere inside my function (right?) but I just can't figure out the syntax at all.

To help figure out where the crash occurs, you can set breakpoints and step through with your debugger. The crash may well be due to something you did much earlier on, but at least it can give you a starting point to work with.

As for the syntax: you seemed to have managed the for loops quite well, so I am not sure why you consider this a problem.

Originally Posted by lostandconfused

can we keep it to simple terms please?

If I read him correctly, Sebastiani's point is basically that you should be storing more than just a (base ten) digit in an int. Of course, if your instructor's requirement is that you are supposed to store exactly one digit in each node of the linked list, then so be it. You could save some space by having an unsigned char member instead, but you do not need to worry too much about that.

Originally Posted by Sipher

I believe linked lists are a mistake because an data type is presumed to be a continues block of memory.

Linked lists might not be a terribly good choice here, but your reasoning sounds rather flawed. What exactly are you trying to say?

Originally Posted by Sipher

A data type like BigNum must remain in one size after it's created!

Eh, that is not true, at least not in the way that you seem to be intending.

So what is that rule about? Forget it, I am fairly certain that it does not apply. The nodes of a linked list can be accessed in a continuous fashion. Sure, they might not be stored contiguously, but that does not disqualify a linked list from being used to store the digits of an arbitrary precision integer, and thus implement a bigint data type.