Overloading the plus operator for linked lists(binary numbers)

Pages: 12
I have tried to understand the concept of linked lists and I have read the assigned chapter 2 times. My teacher is a little laid back when it comes to teaching! This is only a portion of my program. This function is supposed to add 2 binary numbers 11101+1101 and store the result in the temp list. The answer I get is 10000.I don't think that it is adding the carry. If there is anything wrong with the way I have asked this question, please let me know!


binary operator+( binary num1, binary num2)
{
binary temp;
int carry, sum;
nodetype *current1, *current2, *newnode;
reverselist(num1.head_ptr);
reverselist(num2.head_ptr);
current1=num1.head_ptr;
current2=num2.head_ptr;
temp.head_ptr = NULL;
carry = 0;
while(current1!=NULL && current2!=NULL)
{
newnode=new nodetype;
newnode->link = temp.head_ptr;
temp.head_ptr=newnode ;
sum = current1->info + current2->info + carry;
temp.head_ptr->info = sum% 2;
carry = carry/2;
current1=current1->link;
current2=current2->link;
}

while(current1!=NULL)
{
newnode=new nodetype;
newnode->link = temp.head_ptr;
temp.head_ptr=newnode ;

sum = current1->info + carry;
temp.head_ptr->info = sum%2;
carry = carry/2;
current1=current1->link;


}
while (current2!=NULL)
{
newnode=new nodetype;
newnode->link = temp.head_ptr;
temp.head_ptr=newnode ;

sum = current2->info + carry;
temp.head_ptr->info = sum%2;
carry = carry/ 2;

current2=current2->link;

}

if (carry)
{
newnode=new nodetype;
newnode->link = temp.head_ptr;
temp.head_ptr=newnode ;
newnode->info=carry;
}

return temp;

}
carry = carry/2;
Should it not be carry = sum/2; ?
That is what I had at first and my teacher changed it! I will try this.


Yea it didn't work. Should I post all of my code? I don't think that it is possible that any other code has errors.



One thing that I notice is that whenever I change the sum or the carry. The out put sum is 10000 no matter what! Something is ignoring those 2 variables.
Last edited on

carry = sum - (sum%2); ?
That doesn't work either. Is there possibly something masking the carry? LIke whatever I do, I get this in the outfile.

1 1 1 0 1
1 1 0 1
1 0 0 0 0
Try 0 0 0 0 0 with 0 0 0 0 0
I'm sorry, come again?

Never mind I understand ha. I will try this!

I get 0 0 0 0 0 .
Last edited on
I don't see what's wrong in your code.
You should try to debug it step by step.

( btw, the correct formula is carry = sum/2; )
Would posting the whole program help? When you say debug, you mean trace through everything right?


Is my reverselist ok?



void reverselist(nodetype*& mylist)
{
nodetype *templist, *tempnode;

templist = NULL;
while(mylist !=NULL)
{
tempnode = mylist;


mylist = mylist->link;
tempnode->link = templist;
templist = tempnode;

}
mylist = templist;

}
Last edited on
Yep, I mean trace through everything.
If it's not too big, posting the whole program would indeed help.

Your reverselist() seems correct.
Hehe...I'll let you determine the size ;)

#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
#include "binary.h"

ostream & operator<<(ostream & outfile, const binary & outclass)
{
nodetype *current;
current=outclass.head_ptr;
while(current!=NULL)
{
outfile<<current->info<<" ";
current=current->link;
}

return outfile;
}
istream & operator>>(istream & infile, binary & inclass)
{

nodetype *newnode,*last;
char ch;
int intch;
list_clear(inclass.head_ptr);
inclass.head_ptr=NULL;
last=NULL;
infile>>ws;
infile.get(ch);


while( isdigit(ch)and infile)
{


intch = ch -'0';


newnode= new nodetype;
newnode->info=intch;
newnode->link=NULL;
if (inclass.head_ptr==NULL)
{
inclass.head_ptr=newnode;

}
else
{
last->link=newnode;

}
last=newnode;
infile.get(ch);

}


return infile;
}
binary operator+( binary num1, binary num2)
{
binary temp;
int carry, sum;
nodetype *current1, *current2, *newnode;
reverselist(num1.head_ptr);
reverselist(num2.head_ptr);
current1=num1.head_ptr;
current2=num2.head_ptr;
temp.head_ptr = NULL;
carry = 0;
while(current1!=NULL && current2!=NULL)
{
newnode=new nodetype;
newnode->link = temp.head_ptr;
temp.head_ptr=newnode ;
sum = current1->info + current2->info + carry;
temp.head_ptr->info = sum% 2;
carry = sum/2;
current1=current1->link;
current2=current2->link;
}

while(current1!=NULL)
{
newnode=new nodetype;
newnode->link = temp.head_ptr;
temp.head_ptr=newnode ;

sum = current1->info + carry;
temp.head_ptr->info = sum%2;
carry = sum/2;
current1=current1->link;


}
while (current2!=NULL)
{
newnode=new nodetype;
newnode->link = temp.head_ptr;
temp.head_ptr=newnode ;

sum = current2->info + carry;
temp.head_ptr->info = sum%2;
carry = sum/2;

current2=current2->link;

}

if (carry)
{
newnode=new nodetype;
newnode->link = temp.head_ptr;
temp.head_ptr=newnode ;
newnode->info=carry;
}

return temp;

}
binary::binary()
{
head_ptr=NULL;
count=0;

}
binary::binary(const binary & inclass)
{
nodetype *tail_ptr;
list_copy(inclass.head_ptr,head_ptr,tail_ptr);

}
binary::~binary()
{
list_clear(head_ptr);

}
const binary & binary::operator =(const binary & otherlist)
{
nodetype *tail_ptr;
if(this != &otherlist)
list_clear(head_ptr);
list_copy(otherlist.head_ptr,head_ptr,tail_ptr);
return otherlist;




}
int binary::getcount() const
{

}
void list_clear(nodetype*& head_ptr)
{
nodetype *removeptr;
while(head_ptr!=NULL)
{
removeptr=head_ptr;
head_ptr=head_ptr->link;
delete removeptr;
}
}
void list_copy(const nodetype*source_ptr, nodetype*& head_ptr, nodetype*& tail_ptr)
{
nodetype *temp;
head_ptr=NULL;
tail_ptr=NULL;
if(source_ptr==NULL)
return;

head_ptr=new nodetype;
head_ptr->link=NULL;
head_ptr->info=source_ptr->info;
tail_ptr=head_ptr;

source_ptr=source_ptr->link;
while(source_ptr !=NULL)
{
temp= new nodetype;
temp->link=NULL;
temp->info=source_ptr->info;
tail_ptr->link=temp;
tail_ptr=tail_ptr->link;
source_ptr=source_ptr->link;
}
}
void reverselist(nodetype*& mylist)
{
nodetype *templist, *tempnode;

templist = NULL;
while(mylist !=NULL)
{
tempnode = mylist;


mylist = mylist->link;
tempnode->link = templist;
templist = tempnode;

}
mylist = templist;

}



Here is the main program code for the most part.

infile>>binobj1;
while(infile)
{
infile>>binobj2;
outfile<<binobj1<<endl;
outfile<<binobj2<<endl;
outfile<<binobj1+binobj2<<endl;



infile>>binobj1;
}








I tried your code and it works correctly.
The problem comes from elsewhere.
Could you explain?


My infile is

11101 1101


with leading white spaces

and my outfile comes to be

1 1 1 0 1
1 1 0 1
1 0 0 0 0





my main is


using namespace std;
#include "binary.cpp"
#include <iostream>
#include <fstream>
int main()
{

ifstream infile;
ofstream outfile;
binary binobj1;
binary binobj2;
infile.open("program2in.txt");
if(!infile)
{
cout << "Failure to open program2in.txt." << endl;
system ("pause");
return 1;
}
outfile.open("program2out.txt");
if(!infile)
{
cout << "Failure to open program2out.txt." << endl;
system ("pause");
return 1;
}

infile>>binobj1;
while(infile)
{
infile>>binobj2;
outfile<<binobj1<<endl;
outfile<<binobj2<<endl;
outfile<<binobj1+binobj2<<endl;



infile>>binobj1;
}


infile.close();outfile.close();
system ("pause");
return 0;
}









Last edited on
I tried the code you posted and I found no problem with it.
The problem necessarily comes from the code you've not posted.

Here's the code I used:
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
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;

struct nodetype
{
    nodetype* link;
    int info;
};

class binary
{
public:
    nodetype* head_ptr;
    int count;

    binary();
    binary(const binary & inclass);
    ~binary();
    const binary& operator =(const binary & otherlist);
    int getcount() const;

};

void list_clear(nodetype*& head_ptr);
void list_copy(const nodetype*source_ptr, nodetype*& head_ptr, nodetype*& tail_ptr);
void reverselist(nodetype*& mylist);

ostream & operator<<(ostream & outfile, const binary & outclass)
{
    nodetype *current;
    current=outclass.head_ptr;
    while(current!=NULL)
    {
        outfile<<current->info;
        current=current->link;
    }

    return outfile;
}

istream & operator>>(istream & infile, binary & inclass)
{

    nodetype *newnode,*last;
    char ch;
    int intch;
    list_clear(inclass.head_ptr);
    inclass.head_ptr=NULL;
    last=NULL;
    infile>>ws;
    infile.get(ch);


    while( isdigit(ch)and infile)
    {


        intch = ch -'0';


        newnode= new nodetype;
        newnode->info=intch;
        newnode->link=NULL;
        if (inclass.head_ptr==NULL)
        {
            inclass.head_ptr=newnode;

        }
        else
        {
            last->link=newnode;

        }
        last=newnode;
        infile.get(ch);

    }


    return infile;
}

binary operator+( binary num1, binary num2)
{
    binary temp;
    int carry, sum;
    nodetype *current1, *current2, *newnode;
    reverselist(num1.head_ptr);
    reverselist(num2.head_ptr);
    cout << num1 << endl << num2 << endl;
    current1=num1.head_ptr;
    current2=num2.head_ptr;
    temp.head_ptr = NULL;
    carry = 0;
    while(current1!=NULL && current2!=NULL)
    {
        newnode=new nodetype;
        newnode->link = temp.head_ptr;
        temp.head_ptr=newnode ;
        sum = current1->info + current2->info + carry;
        temp.head_ptr->info = sum% 2;
        carry = sum/2;
        current1=current1->link;
        current2=current2->link;
    }

    while(current1!=NULL)
    {
        newnode=new nodetype;
        newnode->link = temp.head_ptr;
        temp.head_ptr=newnode ;

        sum = current1->info + carry;
        temp.head_ptr->info = sum%2;
        carry = sum/2;
        current1=current1->link;


    }
    while (current2!=NULL)
    {
        newnode=new nodetype;
        newnode->link = temp.head_ptr;
        temp.head_ptr=newnode ;

        sum = current2->info + carry;
        temp.head_ptr->info = sum%2;
        carry = sum/2;

        current2=current2->link;

    }

    if (carry)
    {
        newnode=new nodetype;
        newnode->link = temp.head_ptr;
        temp.head_ptr=newnode ;
        newnode->info=carry;
    }

    return temp;

}

binary::binary()
{
    head_ptr=NULL;
    count=0;

}

binary::binary(const binary & inclass)
{
    nodetype *tail_ptr;
    list_copy(inclass.head_ptr,head_ptr,tail_ptr);

}

binary::~binary()
{
    list_clear(head_ptr);

}

const binary & binary::operator =(const binary & otherlist)
{
    nodetype *tail_ptr;
    if(this != &otherlist)
        list_clear(head_ptr);
    list_copy(otherlist.head_ptr,head_ptr,tail_ptr);
    return otherlist;
}

int binary::getcount() const
{
 return count;
}

void list_clear(nodetype*& head_ptr)
{
    nodetype *removeptr;
    while(head_ptr!=NULL)
    {
        removeptr=head_ptr;
        head_ptr=head_ptr->link;
        delete removeptr;
    }
}

void list_copy(const nodetype*source_ptr, nodetype*& head_ptr, nodetype*& tail_ptr)
{
    nodetype *temp;
    head_ptr=NULL;
    tail_ptr=NULL;
    if(source_ptr==NULL)
        return;

    head_ptr=new nodetype;
    head_ptr->link=NULL;
    head_ptr->info=source_ptr->info;
    tail_ptr=head_ptr;

    source_ptr=source_ptr->link;
    while(source_ptr !=NULL)
    {
        temp= new nodetype;
        temp->link=NULL;
        temp->info=source_ptr->info;
        tail_ptr->link=temp;
        tail_ptr=tail_ptr->link;
        source_ptr=source_ptr->link;
    }
}

void reverselist(nodetype*& mylist)
{
    nodetype *templist, *tempnode;

    templist = NULL;
    while(mylist !=NULL)
    {
        tempnode = mylist;


        mylist = mylist->link;
        tempnode->link = templist;
        templist = tempnode;

    }
    mylist = templist;

}


int main(void)
{
    binary a,b,c;

    cout << "Enter Bit stream 1:\n";
    cin >> a;
    cout << '\n';
    cout << "Enter Bit stream 2:\n";
    cin >> b;
    cout << '\n';

    cout << "You've entered " << a << " and " << b << endl;

    c = a + b;

    cout << "their sum is : " << c << endl;

    return 0;
}
my main is


using namespace std;
#include "binary.cpp"
#include <iostream>
#include <fstream>
int main()
{

ifstream infile;
ofstream outfile;
binary binobj1;
binary binobj2;
infile.open("program2in.txt");
if(!infile)
{
cout << "Failure to open program2in.txt." << endl;
system ("pause");
return 1;
}
outfile.open("program2out.txt");
if(!infile)
{
cout << "Failure to open program2out.txt." << endl;
system ("pause");
return 1;
}

infile>>binobj1;
while(infile)
{
infile>>binobj2;
outfile<<binobj1<<endl;
outfile<<binobj2<<endl;
outfile<<binobj1+binobj2<<endl;



infile>>binobj1;
}


infile.close();outfile.close();
system ("pause");
return 0;
}
Maybe I am not clear.
I am adding

11101
+ 1101

to get 01010 with a carry of 1 to display 101010.

I slightly modified the way you write files to make it more readable:
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
int main()
{
    ifstream infile;
    ofstream outfile;
    binary binobj1;
    binary binobj2;
    infile.open("program2in.txt");

    if(!infile)
    {
        cout << "Failure to open program2in.txt." << endl;
        system ("pause");
        return 1;
    }

    outfile.open("program2out.txt");
    if(!infile)
    {
        cout << "Failure to open program2out.txt." << endl;
        system ("pause");
        return 1;
    }

    while( (infile>>binobj1) && (infile>>binobj2) )
    {
        outfile << binobj1 << " + " << binobj2 << " = " << binobj1 + binobj2 << endl;
    }

infile.close();outfile.close();
system ("pause");
return 0;
}


program2in.txt:
10101010
01010101

11001100
11001100

1
10

01
10

1111
1


program2out.txt:
10101010 + 01010101 = 11111111
11001100 + 11001100 = 110011000
1 + 10 = 11
01 + 10 = 11
1111 + 1 = 10000
For some reason, my outfile is blank.
You can detect outfile problems by replacing the line where I write in it with:
1
2
3
4
5
6
if( !(outfile << binobj1 << " + " << binobj2 << " = " << binobj1 + binobj2 << endl) )
{
    cout << "Problem writing program2out.txt." << endl;
    system ("pause");
    return 1;
}


If it's not a problem writing outfile, then I don't know.
Last edited on
Yes now the outfile displays + and = so it is not a problem with the outfile then?

I do not get a message saying that it is a problem with the out file so I guess it's not the outfile.


Here did I give you my .h file?


#include <iostream>
#include <cstdlib>
using namespace std;
struct nodetype
{
int info;
nodetype *link;
};
class binary
{
friend ostream & operator<<(ostream & outfile, const binary & outclass);
friend istream & operator>>(istream & infile, binary & inclass);
friend binary operator+( binary num1, binary num2);

public:
binary();
binary(const binary & inclass);
~binary();
const binary & operator =(const binary & otherlist);
int getcount() const;
private:
nodetype *head_ptr;
int count;
};
void list_clear(nodetype*& head_ptr);
void list_copy(const nodetype*source_ptr, nodetype*& head_ptr, nodetype*& tail_ptr);
void reverselist(nodetype*& head_ptr);













Last edited on
Pages: 12