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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<string>
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<<" -> "<<h -> value;
            h = h -> next;
      }
      cout<<endl;
}

int main()
{
      char operation;
      int result, x, y;


      cout<<"Enter first number: ";
	  cin>> x;
      Node * h1 = new Node(x);
      
	  Print(h1);
      cout<<endl;

      cout<<"Enter second number: ";
	  cin>> y;
      Node * h2 = new Node(y);
	  Print(h2);
      cout<<endl;

      cout<<"What operation you want, + or -";
      cin>>operation;
      if( operation == '+' )
      {
            result = h1 ->value + h2 ->value;
            cout<<"Result="<<result<<endl;
      }

      else if( operation == '-' )
      {
            result = h1 ->value - h2 ->value;
            cout<<"Result="<<result<<endl;
      }
}
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>
#include<list>
using namespace std;

int main()
{
	list<int> val;
	int num=45712;
	int num1=num;
	while(num!=0)
	{
		int digit=num%10;
		val.push_front(digit);
		num=num/10;
	}
	cout<<num1<<endl;
	for(list<int>::iterator i=val.begin();i!=val.end();i++)
		cout<<(*i)<<"->";
	cout<<endl;
  return 0;
}
Alright, I stored every number in a node but the output is always 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream>
#include<string>
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<<h -> value;
            h = h -> next;
      }
      cout<<endl;
}

void AddTail(Node*& head, int n)
{
	Node* tmp = new Node(n);
	if(!head)
	{
		head=tmp;
		return;
	}
	Node * t=head;
	while(t->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<x.length(); i++)
        {
			v= x[i] - '0';
                AddTail(h1,v);
        }

	  Print(h1);
      cout<<endl;

      cout<<"Enter second number: ";
	  cin>> y;
      

	  for(int i=0; i<y.length(); i++)
        {
                v= y[i] - '0';
                AddTail(h2,v);
        }

	  Print(h2);
      cout<<endl;

      cout<<"What operation you want, + or -";
      cin>>operation;
      if( operation == '+' )
      {
		  while(!h1 && !h2)
		  {
            result = h1 ->value + h2 ->value;
		  }
            cout<<"Result="<<result<<endl;
      }

      else if( operation == '-' )
      {
		  while(!h1 && !h2)
		  {
            result = h1 ->value - h2 ->value;
		  }
            cout<<"Result="<<result<<endl;
      }
}
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.

i.e. simple addition logic.
 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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <string>
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<<h->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<num1.length(); i++)
        {
			v= num1[i] - '0';
                AddHead(h1,v);
        }
	Print(h1);
	cout<<endl;
	cout<<"Enter second number: ";
	cin>> num2;
	for(unsigned int i=0; i<num2.length(); i++)
        {
                v= num2[i] - '0';
                AddHead(h2,v);
        }
	Print(h2);
	cout<<endl;

	cout<<"Do you want to add or substract the numbers enter + or -: ";
	cin>>operation;
	if(operation == '+')
	{
	cout<<"Result: ";
	 Addition(h1,h2);
	 cout<<endl;
	}
	else if(operation == '-')
	{
	cout<<"Result: ";
	Subtraction(h1,h2);
	cout<<endl;
	}
	cout<<"Again :";
	cin>> ans;
	}
	while((ans == 'Y') || (ans == 'y'));
	return 0;
}
closed account (D80DSL3A)
I'm glad to see that you're getting this to work. I kind of had my doubts. Sorry about the bad start. I'd like to be helpful.

I believe I see a problem with lines 65 and 66 in your Subtraction function:
1
2
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:
1
2
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <iostream>
#include <string>
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<<h->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<num1.length(); i++)
        {
			v= num1[i] - '0';
                AddHead(h1,v);
        }
	Print(h1);
	cout<<endl;
	cout<<"Enter second number: ";
	cin>> num2;
	for(unsigned int i=0; i<num2.length(); i++)
        {
                v= num2[i] - '0';
                AddHead(h2,v);
        }
	Print(h2);
	cout<<endl;

	cout<<"Do you want to add or substract the numbers enter + or -: ";
	cin>>operation;
	if(operation == '+')
	{
	cout<<"Result: ";
	 Addition(h1,h2);
	 cout<<endl;
	}
	else if(operation == '-')
	{
	cout<<"Result: ";
	if(h2 > h1)
	{cout<<"-"; Subtraction(h2, h1);}//this is where i switch the numbers
	else if(h1 > h2)
	{Subtraction(h1,h2);}
	cout<<endl;
	}
	cout<<"Again :";
	cin>> 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.