You are using a version without Ads of this website. Please, consider donating:

### My linked list won't take big numbers.

My program is almost done all that is left is entering big numbers, the program can add and subtract small numbers but not big numbers because it puts them all in one node, I need the program to store each number in a node.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859`` ``````#include #include using namespace std; class Node { public: int value; Node * next; Node(); Node(int); }; Node::Node():value(0),next(NULL){} Node::Node(int v):value(v),next(NULL){} void Print(Node* h) { while(h) { cout<<" -> "< value; h = h -> next; } cout<> x; Node * h1 = new Node(x); Print(h1); cout<> y; Node * h2 = new Node(y); Print(h2); cout<>operation; if( operation == '+' ) { result = h1 ->value + h2 ->value; cout<<"Result="<value - h2 ->value; cout<<"Result="<
Last edited on
You are just storing each value in one node.

You have to split the number into single digits and store it in different nodes.
Last edited on
Ok that is what I was saying, how do I do that?
I am giving one example for this, It is written by using `std::list`

 ``123456789101112131415161718192021`` ``````#include #include using namespace std; int main() { list val; int num=45712; int num1=num; while(num!=0) { int digit=num%10; val.push_front(digit); num=num/10; } cout<::iterator i=val.begin();i!=val.end();i++) cout<<(*i)<<"->"; cout<
Alright, I stored every number in a node but the output is always 0.
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394`` ``````#include #include using namespace std; class Node { public: int value; Node * next; Node(); Node(int); }; Node::Node():value(0),next(NULL){} Node::Node(int v):value(v),next(NULL){} void Print(Node* h) { while(h) { cout< value; h = h -> next; } cout<next) t=t->next; t->next=tmp; } int main() { string x, y; char operation; int result = 0, v; //over here a initialized the result to 0 and I always //get the results 0, if I remove it I get an error saying result is not initialized Node* h1 = NULL; Node* h2 = NULL; cout<<"Enter first number: "; cin>> x; for(int i=0; i> y; for(int i=0; i>operation; if( operation == '+' ) { while(!h1 && !h2) { result = h1 ->value + h2 ->value; } cout<<"Result="<value - h2 ->value; } cout<<"Result="<
Logic for addition and subtraction is wrong.

`while(!h1 && !h2)`

Even in first loop it fails the condition and breaks the loop.
Finally default result value '0' is displayed.
ok, how do I fix that?
For addition and subtraction logic you have to use double linked list because we have to traverse list from backward to add or subtract.

 ``` 245 + 589 ---- 834```

You can't get this value by directly adding each digit from start.
Last edited on
So I got the code to work properly except the subtraction doesn't work in some exceptional situations.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143`` ``````#include #include using namespace std; class Node { public: int value; Node * next; Node(); Node(int); }; Node::Node():value(0),next(NULL){} Node::Node(int v):value(v),next(NULL){} void Print(Node * h) { if(!h) return; Print(h->next); cout<value; } void AddHead(Node* & head, int n) { Node * tmp = new Node(n); tmp->next = head; head=tmp; } void Addition( Node* h1,Node* h2) { int carry = 0, sum; Node* result = NULL; Node* tmp = NULL; Node* prev = NULL; while(h1 !=NULL || h2 != NULL) { sum = carry +((h1? h1->value: 0) +(h2? h2->value: 0)); carry = (sum >=10)? 1:0; sum = sum % 10; tmp = new Node(sum); if(result== NULL){ result = tmp;} else{ prev->next = tmp;} prev = tmp; if(h1) h1=h1->next; if(h2) h2=h2->next; } if(carry > 0) tmp->next = new Node(carry); Print(result); } void Subtraction( Node* h1,Node* h2) { int carry = 0, sum; Node* result = NULL; Node* tmp = NULL; Node* prev = NULL; while(h1 !=NULL || h2 != NULL) { sum = carry + ((h1? h1->value: 0) - (h2? h2->value: 0)); carry = (sum >=10)? -1:0; sum = sum % 10; tmp = new Node(sum); if(result== NULL){ result = tmp;} else{ prev->next = tmp;} prev = tmp; if(h1) h1=h1->next; if(h2) h2=h2->next; } if(carry > 0) tmp->next = new Node(carry); Print(result); } int Length(Node* head) { int i=0; while(head) { i++; head=head->next; } return i; } int main() { string num1,num2; char operation, ans; int v; do { Node* h1 = NULL; Node* h2 = NULL; cout<<"Enter first number: "; cin >> num1; for(unsigned int i=0; i> num2; for(unsigned int i=0; i>operation; if(operation == '+') { cout<<"Result: "; Addition(h1,h2); cout<> ans; } while((ans == 'Y') || (ans == 'y')); return 0; }``````

I believe I see a problem with lines 65 and 66 in your Subtraction function:
 ``12`` ``````carry = (sum >=10)? -1:0; sum = sum % 10;``````

That is closer to what's appropriate for addition. Indeed, it differs from the code in Addition only by the - sign in the first line.

Try this code in place of lines 65, 66:
 ``12`` ``````carry = (sum < 0)? -1:0;// we borrow if the sum is negative if(carry < 0) sum += 10;// add amount borrowed ``````

EDIT: Just noticed line 76 `if(carry > 0)`. Since carry = 0 or -1 in Subtraction, carry will never be > 0.
Last edited on
So I fixed the code now if the user tries to subtract a big number form a small number it switches the numbers but I was wondering if you know how to make the code ignore entered numbers so if the user enters 00030 the program will ignore the zeros on the left.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140`` ``````#include #include using namespace std; class Node { public: int value; Node * next; Node(); Node(int); }; Node::Node():value(0),next(NULL){} Node::Node(int v):value(v),next(NULL){} void Print(Node * h) { if(!h) return; Print(h->next); cout<value; } void AddHead(Node* & head, int n) { Node * tmp = new Node(n); tmp->next = head; head=tmp; } void Addition( Node* h1,Node* h2) { int carry = 0, sum; Node* result = NULL; Node* tmp = NULL; Node* prev = NULL; while(h1 !=NULL || h2 != NULL) { sum = carry +((h1? h1->value: 0) +(h2? h2->value: 0)); carry = (sum >=10)? 1:0; sum = sum % 10; tmp = new Node(sum); if(result== NULL){ result = tmp;} else{ prev->next = tmp;} prev = tmp; if(h1) h1=h1->next; if(h2) h2=h2->next; } if(carry > 0) tmp->next = new Node(carry); Print(result); } void Subtraction( Node* h1,Node* h2) { int carry = 0, sum; Node* result = NULL; Node* tmp = NULL; Node* prev = NULL; while(h1 !=NULL || h2 != NULL) { sum = carry + ((h1? h1->value: 0) - (h2? h2->value: 0)); carry = (sum < 0)? -1:0; if(carry < 0) sum += 10; /*sum = sum % 10;*/ tmp = new Node(sum); if(result== NULL){ result = tmp;} else{ prev->next = tmp;} prev = tmp; if(h1) h1=h1->next; if(h2) h2=h2->next; } //if(carry < 0) /*tmp->next = new Node(carry);*/ Print(result); } int Length(Node* head) { int i=0; while(head) { i++; head=head->next; } return i; } int main() { string num1,num2; char operation, ans; int v; do { Node* h1 = NULL; Node* h2 = NULL; cout<<"Enter first number: "; cin >> num1; for(unsigned int i=0; i> num2; for(unsigned int i=0; i>operation; if(operation == '+') { cout<<"Result: "; Addition(h1,h2); cout< h1) {cout<<"-"; Subtraction(h2, h1);}//this is where i switch the numbers else if(h1 > h2) {Subtraction(h1,h2);} cout<> ans; } while((ans == 'Y') || (ans == 'y')); return 0; }``````
You are reading number as string.
So trim the left side 0's from the string by looping through each character of string and using `string.erase(iterator)` to remove it from string.
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: