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!
Code:
#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;
}
Code:
#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: