Quick Question in Linked List

Could someone see why my check bigger doesn't work?
In a sense that if I say 2000 - 4000 it works,
but if the first number is bigger (4000 - 2000) it doesn't work.
my project presentation is in 6 hours, I hope you guys do this one
for me....I have worked hard on this one!!!!

Appreciated.


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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <string>
using namespace std;
class Node
{
public:
        int val;
        Node * next;
        Node(){val=0;next=NULL;}
        Node(int v){val=v;next=NULL;}
};
void AddHead(Node*& h, int v)
{
        Node *a = new Node(v);
        a->next = h;
        h=a;
}
void Print(Node *h)
{
        while(h)
        {
                cout<<h->val;
                h=h->next;
        }
}
int Length(Node *h)
{
        int i=0;
        while(h)
        {
                i++;
                h=h->next;
        }
        return i;
}
void DeleteHead(Node*&h)
{
        if(!h) return;
        Node *t = h;
        h=h->next;
        delete t;
}
  void Plus(Node* a, Node* b, Node* c)
{
        Node*head=NULL;
        int ans=0;
        while(a && b)
        {
                ans = a->val+b->val;
                if(!a->next)
               {
                  AddHead(head, ans);
                a=a->next;
                b=b->next;
                  break;
               }
               if(ans>=10)
                {
                        ans=ans-10;
                      if(a->next) a->next->val++;
                }
                AddHead(head, ans);
                a=a->next;
                b=b->next;
        }
        while(a)
        {
                AddHead(head, a->val);
                a=a->next;
        }
        while(b)
        {
                AddHead(head, b->val);
                b=b->next;
        }
        Print(head);
 delete a,b,c;
}
void Minus(Node* a, Node* b, Node* c)
{
   
Node*head=NULL;
        int ans=0;
        while(a && b)
        {
  if(a->val-b->val<0)
    {
       a->val=a->val+10;
       a->next->val--;
       ans = a->val-b->val;
    }
   else
    {
    ans = a->val-b->val;
    }
    AddHead(head, ans);
    a=a->next;
    b=b->next;
  }
        while(a)
        {
                AddHead(head, a->val);
                a=a->next;
        }
        while(b)
        {
                AddHead(head, b->val);
                b=b->next;
        }
        Print(head);
		delete a,b,c;
}
bool CheckBigger(Node*a, Node* b)
{
    if (a->val > b->val)
    return true;
    else if( a->val < b->val)
    return false;
    a=a->next;
	b=b->next;

}

int main()
{
        Node* answer = NULL;
        string num1="", num2="";
        char ansdo, ansop;
        do{
        cout<<"Enter the first number:";
        cin>>num1;
         const char *in = num1.c_str();
        int i=0;
        Node * head1 = NULL;
        while(in[i]!='\0')
        {
                int value = in[i]-'0';
                AddHead(head1, value);
                i++;
        }
         cout<<"Enter the second number:";
        cin>>num2;
         const char *in2 = num2.c_str();
        int j=0;
        Node * head2 = NULL;
        while(in2[j]!='\0')
        {
                int value = in2[j]-'0';
                AddHead(head2, value);
                j++;
        }
 
         cout<<"Enter + to add or - to subtract: ";
        cin>>ansop;
       
        if ( ansop == '+')
        {
        Plus(head1,head2,answer);
        Print(answer);
cout<<endl;
        }
    else if (ansop == '-')
        {
            if (Length(head1)>Length(head2))
                {
                Minus(head1,head2,answer);
                Print(answer);
                }
            else if (Length(head1)<Length(head2))
                {
                  Minus(head2,head1, answer);
                  Print(answer);
               }
           else
             if (CheckBigger(head1, head2))
             {
                Minus(head1,head2,answer);
               Print(answer);
             }
          else
             {
              Minus(head2,head1, answer);
                  Print(answer);
             }
          
           cout<<endl;
       }
        else cout<<"Wrong Input!"<<endl;
       
        cout<<"Would you like to repeat the calculation? <Y to repeat>:";
        cin>>ansdo;
} while(ansdo == 'y' || ansdo == 'Y');
         return 0;
 
}
Last edited on
in your Minus() function

this is the correct logic for your while(a && b) loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (a && b)
	{
		if (a->val - b->val<0)
		{
			ans = b->val - a->val;
		}
		else
		{
			ans = a->val - b->val;
		}
		AddHead(head, ans);
		a = a->next;
		b = b->next;
	}
Hi khalaf,
I just tried your code and added it to my code yet it doesn't work,
maybe I implemented your code in the wrong line.
Could you implement it yourself and then show the full code to me,

Thanks!
It's working for me. look at your minus function, there is that while (a && b) loop. Then check mine.

Don't add my code to yours, change yours to look like mine.

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
void Minus(Node* a, Node* b, Node* c)
{

	Node*head = NULL;
	int ans = 0;
	while (a && b)
	{
		if (a->val - b->val<0)
		{
			ans = b->val - a->val;
		}
		else
		{
			ans = a->val - b->val;
		}
		AddHead(head, ans);
		a = a->next;
		b = b->next;
	}
	while (a)
	{
		AddHead(head, a->val);
		a = a->next;
	}
	while (b)
	{
		AddHead(head, b->val);
		b = b->next;
	}
	Print(head);
	delete a, b, c;
}


Enter the first number:4
Enter the second number:2
Enter + to add or - to subtract: -
2
Would you like to repeat the calculation? <Y to repeat>:y
Enter the first number:2
Enter the second number:4
Enter + to add or - to subtract: -
2
Would you like to repeat the calculation? <Y to repeat>:



Good luck
Last edited on
Main() seems to think that Plus() and Minus() modify their 3rd argument but they don't. In fact they delete it. This is why it's a good idea to put a comment at the top of your functions to describe exactly what they are supposed to do. I suggest you change Plus() to:
1
2
// Add the numbers in a and b, returning the result
Node * Plus(Node* a, Node* b)

and then in main() you can do this:
1
2
answer = Plus(head1, head2);
Print(answer);


Plus() is a mess:
Line 52: what if ans >= 10? You'll be putting a digit value of 10-19 in the answer.
Line 68: Same problem.
Line 60: You're changing one of the numbers that was passed in! That's a very bad idea. Imagine how hard it would be to write code if x = a+b changed a or b.

Basically you have to deal with the carry on each digit.

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
// Add the numbers in a and b, returning the result
Node * Plus(Node* a, Node* b)
{
    Node*head=NULL;
    int ans=0;
    int carry = 0;
    while(a && b)
        {
            ans = a->val+b->val+carry;
            if(ans>=10) {
                ans=ans-10;
                carry = 1;
            } else {
                carry = 0;
            }
            AddHead(head, ans);
            a=a->next;
            b=b->next;
        }

    // There may be more digits in one of the numbers. Point 'a'
    // to the one with (possibly) more digits
    if (a == NULL) a = b;
    while(a)
        {
            ans = a->val + carry;
            if(ans>=10) {
                ans=ans-10;
                carry = 1;
            } else {
                carry = 0;
            }
            AddHead(head, a->val);
            a=a->next;
        }
    if (carry) {
        AddHead(head, carry);
    }
    return head;
}


Minus() has the same problems.

Something important to note: Plus() and Minus() take their arguments with the least significant digit at the head of the list, but they create their result with the least significant digit at the tail of the list! This means that Print() is real easy, but in any sort of Production system this wouldn't be acceptable.

CheckBigger() only compares one digit. What if that digit is the same but the next one is different? Remember, the numbers you're comparing are stored with the least significant digit first. Also, since main() is really just interested in which digit is larger, you can combine Length() and CheckBigger() into one function, but it requires recursion. Are you familiar with that? Here is pseudo-code for what I have in mind:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// Compare the numbers a and b. Return a negative number, 0, or a positive number
// depending on whether a is less than, equal to, or greater than b.
int compare(Node*a, Node* b)
{
    // The idea here is to compare the more significant digits first. If they
    // are all equal then use the current digit in an attempt to break the tie
    if (a and b are null) {
        return 0. you reachec the end of two numbers with the same number of digits
    else if (b == NULL) {
        return 1. A has more digits than b
    else if (a == NULL) {
        return -1. B has more digits.
    }

    // if you get here then a and b are not null. Compare the more significant digits
    int result = compare(a->next, b->next);
    if (result == 0) {  // More significant digits are all the same.
        result = a->val - b->val;    // break the tie
    }
    return result;
}


If you do this then the code for subtracting in main() becomes:
1
2
3
4
5
6
7
                if compare(head1, head2) < 0) {
                    answer = Minus(head2, head1);
                } else {
                    answer = Minus(head1, head2);
                }
                Print(answer);

I Hope this helps.
kkhalaf wrote:
Don't add my code to yours, change yours to look like mine.
delete a, b, c;

That doesn't do what you think it does. It is equivalent to:
1
2
3
    delete a;
    b;
    c;
where the last two statements are essentially no-ops.
Topic archived. No new replies allowed.