Issues with linked list addition

I'm writing a program that is supposed to add two linked lists together and display their sum in vigesimal digits. The problem is that when I have a carry it will not add to the next node, and also when I try to add in lowercase values as opposed to uppercase, my answer is different, when the uppercases and lowercases of the same letter should get the same answer. Not sure what exactly what I'm doing wrong, can someone point me in the right direction? I'm assuming my issue lies in my Add() function, but i can't figure it out.
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <iostream>
#include <string>
#undef NULL
using namespace std;

typedef char element;
const int NULL=0;
const int BASE=20;
const element SENTINEL='#';

class listnode{
        public:
                element data;
                listnode*next;
        };

class LList{
        private:
                listnode*head;
                listnode*tail;
        public:
                LList();
                ~LList();
                void Clean();
                void ReadBackward();
                void Print();
                void InsertHead(element thing);
                void InsertTail(element thing);
                void Duplicate(LList&Source);
                void Add();
                void Multiply();
                void DisplayMenu();
                void GetMenuChoice();
                void Steal(LList&Victim);
                void GetCommand();
                void Reverse();
                int CharToInt(char userval);
                char IntToChar(int userval);
        };

int main(){

        LList A; //L1
        A.GetCommand();
}

        
void LList::Print(){
        //PRE: The N.O. LList is valid
        //POST: The N.O. LList is unchanged, and its elements have been 
        //displayed
 Reverse();

        int counter;

        listnode*temp;
        temp=head;
        while(temp!=NULL){
                cout<< temp->data;
                temp=temp->next;
                }

        cout << endl <<endl;

        Reverse();

        }

void LList::ReadBackward(){
        //PRE: The N.O. LList is valid
        //POST: The N.O. LList will be valid using data provded by the user
        //and will be stored in a forward order

        element userval;
        listnode*temp;
        Clean();

        cout<<endl;
        cout <<"Enter a vigesimal number, followed by " <<SENTINEL <<": ";
        cin>> userval;

        while(userval!=SENTINEL){
                temp=new listnode;
                temp->data=userval;
                temp->next=head;

                if(head==NULL)
                        tail=temp;
                else
                        ;

                head=temp;
                cin>>userval;
                }

        cout <<endl <<"Entering completed" <<endl <<endl;

        cout << "Current vigesimal number is:  ";
}

int LList::CharToInt(char userval){
        //Converts individual vigesimal numbers into integers to perform calc.

        char ch;
        int num;
        ch=userval;

        if((ch>=48)||(ch<=57))
                num=ch-'0';
        else if((ch>=65)||(ch<=74))
                num=ch-'A'+10;
        else if ((ch>=97)||(ch<= 106))
                num=ch-'a'+10;  //converts lower to upper, then to int

        return num;
        }

char LList::IntToChar(int userval){
        //Converts integer number to vigesimal number to display to the user

        int num;
        char ch;
        num=userval;

        if((num>=0)||(num<=9))
                ch=num+'0';
        else if ((num>9)||(num<=19))    //num is A-J
                ch=num+'A'-10;

        return ch;
        }

void LList::Reverse(){
        //PRE: NO LList is valid
        //POST: NO LList is unchanged, except its elements are now in reverse 
        //order

        LList Helper;           //calls constructor, makes head NULL
        listnode*temp;
        temp=head;

        while(temp!=NULL){
                Helper.InsertHead(temp->data);
                temp=temp->next;
                }
        Steal(Helper);
 }

void LList::Clean(){
        //PRE: The N.O. LList is valid
        //POST: The N.O. LList is valid and empty, and all of the memory
        //for its listnodes has been returned to the system memry pool

        listnode*temp;
        while(head!=NULL){
                temp=head;
                head=head->next;
                delete temp;
                }
        }

LList::LList(){
        //PRE: None
        //POST: The N.O. LList is valid and empty

        head=NULL;
        }

LList::~LList(){
        //PRE: The N.O. LList is valid
        //POST: The N.O. LList is valid and empty, and all memory for its 
        //listnodes has been returned to the heap

        Clean();
        }

void LList::InsertHead(element thing){
        //PRE: The N.O. LList is valid
        //POST: The N.O. LList is unchanged, except it now contains a new 
        //listnode at its head end containing thing

        listnode*temp;
        temp=new listnode;
        temp->data=thing;
        temp->next=head;
        if(head==NULL)
                tail=temp;
        else
                ;
        head=temp;
        }

void LList::InsertTail(element thing){
        //PRE: The N.O. LList is valid
//POST: The N.O. LList is unchanged, except it now contains a new 
        //listnode at its tail end containing thing

        listnode*temp;
        temp=new listnode;
        temp->data=thing;
        temp->next=NULL;
        if(head==NULL)
                head=temp;
        else
                tail->next=temp;
        tail=temp;
        }

void LList::Steal(LList & Victim){
        Clean();
        head=Victim.head;
        tail=Victim.tail;
        Victim.head=NULL;
        }

void LList::Add(){
        //This function adds two linked lists together, storing their sum in a
        //third linked list

        LList Result;   //sum of both llists
 LList B;        //2nd llist

        B.ReadBackward();       //Gets elements to add from user
        B.Print();

        cout<< "Adding a new vigesimal number to the current vig.  number"
        <<endl <<endl;

        int sum;
        int sum1;
        int sum2;
        int carry;

        sum=0;
        sum1=0;
        sum2=0;
        carry=0;

        listnode*temp1;
        listnode*temp2;

        temp1=head;
        temp2=B.head;

        while((temp1!=NULL)&&(temp2!=NULL)){
                sum1=CharToInt(temp1->data);
                sum2=CharToInt(temp2->data);

                temp1=temp1->next;
                temp2=temp2->next;

                sum=(sum1+sum2+carry)%BASE;
                carry=(sum1+sum2+carry)/BASE;

                Result.InsertTail(IntToChar(sum));
                }

        while((temp1==NULL)&&(temp2!=NULL)){
                sum2=CharToInt(temp2->data);

                temp2=temp2->next;

                sum=(sum2+carry)%BASE;
                carry=(sum2+carry)/BASE;

                Result.InsertTail(IntToChar(sum2));
                }
while((temp2==NULL)&&(temp1!=NULL)){
                sum1=CharToInt(temp1->data);

                temp1=temp1->next;

                sum=(sum1+carry)%BASE;
                carry=(sum1+carry)/BASE;

                Result.InsertTail(IntToChar(sum1));
                }

        Steal(Result);
        Print();
        GetCommand();
        }

                     


I deleted some irrelevant functions so the code would fit.
Last edited on
Lines 108,110,112: You're using an OR condition when you need an AND condition. e.g. Any value >= 48 will cause the first if to be true and will cause the second condition to be ignored.

Line 115: What happens is the character entered is not a vigesimal digit? You're going to return an uninitialized (garbage) value.

Lines 125,127: Same issue with OR condition. You need an AND condition here also.

Line 130: Same issue returning uninitialized value if userval is out of range.
Thanks that helped a lot! Here is my updated add function, now it works sometimes but if there is a carry value it will flip it the wrong way, but if there is not a carry its fine. For example if I add 34A+1 i get 34B, but if I add I+J I get H1 instead of 1H. Not sure where the error lies.

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
 void LList::Add(){
        //This function adds two linked lists together, storing their sum in a
        //third linked list

        cout <<endl
        << "Adding a new vigesimal number to the current vig. number."
        <<endl;

        LList Result;   //sum of both llists
        LList B;        //2nd llist

        B.ReadBackward();       //Gets elements to add from the user
        B.Print();              //Print those elements to the user

        int sum;        //sum of both numbers
        int sum1;       //sum of first number
        int sum2;       //sum of second number
        int carry;

        sum=0;
        sum1=0;
        sum2=0;
        carry=0;

        listnode*temp1;
        listnode*temp2;

        temp1=head;
        temp2=B.head;

        while((temp1!=NULL)||(temp2!=NULL)){
        //while at least one of the lists is not empty

                if(temp1!=NULL){        //Native object not empty
                        sum1=CharToInt(temp1->data); //gets integer value
                        temp1=temp1->next;          //pointer pointing to next
                                                   //listnode in list
                        }

                else // no more values to add
                        sum1=0;

                if(temp2!=NULL){        //List B is not empty
                        sum2=CharToInt(temp2->data); //gets integer value
                        temp2=temp2->next; //pointer pointing to next listnode
                        }

                else    //no more values to add
                        sum2=0;

                sum=sum1+sum2+carry;//sum is sum of NO + B + carry value        
                carry=0;//but carry is currently 0

                while(sum>19){
                        sum=sum-BASE;
                        carry++;
                        Result.InsertTail(IntToChar(carry));
                        }

                Result.InsertTail(IntToChar(sum));
                sum=0;
                }

        cout<< "Adding completed." <<endl <<endl;
        cout << "Current vigesimal number is: ";

        Steal(Result);
        Print();
        GetCommand();
        }
   
Last edited on
Topic archived. No new replies allowed.