I have a large integer , 234321, stored in a text file, input.txt. I want to read it, digit by digit, then insert into a linked list. This doesn't work, I know it has something to do with my while loop in main, how can I fix this?
we can fix it, but the approach is probably wrong.
read the number one character at a time and insert those into the list.
that is, instead of having int num1 that you read from the file, read char c1 instead and insert that as a digit (you probably want c1-'0' not c1, that is you want 0 not '0' the character right?)
not sure, it depends on your file structure, but I would think
do //assuming you know you are now at a point to expect a number...
cin >> c1;
... things
while(isdigit(c1));
consider using new instead of malloc. Malloc is C and its a little more clunky to use it. It does have an advantage because of realloc, but c++ vectors and containers generally have that covered so you don't have to fool with it.
Node logic seems good, but a few notes:
- Consistency: if start of list is called Head, call it that throughout (rename 'top')
- Parallelism: destroy the things you create (in this case missing frees)
- C++: compare pointers to nullptr instead of NULL. Prefer new and delete to malloc/free. (Nitpick) A lot of your methods are named in the Java style of lower case letter first -- you either want snake_case_methods or CamelCaseMethods.
I'd do a single read to seed the linked list with the first number, and then do the loop for the remaining ones, to avoid having a bunch of numbers and then a null item.