c p - binary addition using doubly linkedlist

October 7, 2010 at 08:29:42
Specs: Windows XP
I'm trying c code for binary no addition using doubly linkedlist

when i keep a watch, the control never executes the code even the head1 is NULL :-

if(head1==NULL)
{
head1=move;
}

the code is here


#include<stdio.h>
#include<conio.h>
#include<alloc.h>

typedef struct linkedlist
{
int d;
struct linkedlist *next;
struct linkedlist *prev;
}node;

node * createnode(int data)
{
node *newnode;
newnode = (node *)malloc(sizeof(node));
newnode->d=data;
newnode->next=newnode->prev=NULL;
return newnode;
}


void main()
{
int n1,n2,temp,i,sum,carry;
node *head1,*head2,*head3,*move,*move1,*move2,*move3;

clrscr();
head1=head2=head3=NULL;

printf("\n\nEnter no of bits of first no: ");
scanf("%d",&n1);
printf("\n\nEnter Binary no: ");

for(i=0;i<n1;i++)
{
scanf("%d",&temp);

move=createnode(temp);

if(head1==NULL)
{
head1=move;
}

else
{
move->next=head1;
head1->prev=move;
head1=move;
}

}


printf("\n\nEnter no of bits of second no: ");
scanf("%d",&n2);
printf("\n\nEnter Binary no: ");

for(i=0;i<n2;i++)
{
scanf("%d",&temp);

move=createnode(temp);

if(head2==NULL)
{
head2=move;
}

else
{
move->next=head2;
head2->prev=move;
head2=move;
}

}

/* *********** create linkedlist for addition ********** */

move1=head1;
move2=head2;
move3=head3;

while(head1!=NULL || head2 !=NULL)
{
if(head3==NULL)
{
head3=createnode(0);
}
else
{
move=head3;

while(move->next!=NULL)
move=move->next;

move->next=createnode(0);
}

move1=move1->next;
move2=move2->next;

}

/* *********** binary addition ********** */

move1=head1;
move2=head2;
move3=head3;

carry=0;

while(move3!=NULL)
{

sum=(move1->d)+(move2->d)+carry;


if(sum==3)
{
move3->d=1;
carry=1;
}
else if(sum==2)
{
move3->d=0;
carry=1;
}
else if(sum==1)
{
move3->d=1;
carry=0;
}
else
{
move3->d=0;
carry=0;
}

move1=move1->next;
move2=move2->next;
move3=move3->next;

}
/* *********** Display binary addition ********** */

move=head3;
printf("\n\nAddition is :\n\n");
while(move!=NULL)
{
printf(" %d",move->d);
}

getch();
}

please reply me on <removed>@gmail.com

edited by moderator: Removed email address -Razor2.3


See More: c p - binary addition using doubly linkedlist

Report •


#1
October 7, 2010 at 10:29:21
Didn't your professor ever teach you about functions? Why aren't you using them?

You should at the very least have a function to attach a node to the list.

How To Ask Questions The Smart Way


Report •

#2
October 8, 2010 at 09:31:39
OK, you are right, but it's not the answer i am waiting for ...

Report •
Related Solutions


Ask Question