when i keep a watch, even though initially head1 is NULL it doesn't execute following lines
if(head1==NULL)
{
head1=move;
}
the code is here, please help me
Code:
#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();
}