Thread: Overloading the + operator for linked lists(binary numbers)

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Overloading the + operator for linked lists(binary numbers)

    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 <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);
    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;
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > If there is anything wrong with the way I have asked this question, please let me know!
    Well the fact your code is all on one line is a big reason to read no further.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    1) Don't put using namespace std in header files. use std:: before std types and functions instead.

    2) The fastest way to find out what's wrong is to step through your code with a debugger and make sure it's doing what you want it to do at every step.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    Thanks for your help guys I figured out what was wrong. For some reason I couldn't post the code properly sorry!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list -= operator overloading
    By mrmodest in forum C++ Programming
    Replies: 5
    Last Post: 03-09-2012, 11:55 PM
  2. Operator overloading Polynomials Doubly linked list
    By Fatima Rizwan in forum C++ Programming
    Replies: 2
    Last Post: 02-27-2010, 09:58 AM
  3. Linked and binary lists
    By Shadowwoelf in forum C++ Programming
    Replies: 6
    Last Post: 10-10-2007, 02:16 AM
  4. Linked lists and Binary trees
    By Scotty in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-04-2002, 06:59 PM
  5. overloading binary + operator
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 11:41 AM