error in c program for binary addition using doubly linkedlist

This is a discussion on error in c program for binary addition using doubly linkedlist within the C Programming forums, part of the General Programming Boards category; when i keep a watch, even though initially head1 is NULL it doesn't execute following lines if(head1==NULL) { head1=move; } ...

  1. #1
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69

    Wink error in c program for binary addition using doubly linkedlist

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

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by gaurav9991 View Post
    when i keep a watch, even though initially head1 is NULL it doesn't execute following lines

    Code:
    if(head1==NULL)
    		{
    			head1=move;
    		}
    Sure it does! Why would you think that it does not?

    By the way, void main is wrong. It must be int main.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User gaurav9991's Avatar
    Join Date
    Oct 2010
    Location
    Pune, Maharashtra, India
    Posts
    69

    it doesn't work on turbo c++ editor

    I'm sorry sir, but it doesn't !
    I'm using Turbo c++ editor version 3.0 , is it a problem due to editor ?
    i have faced the same problem with two different machines but the same editor

    thanks for reply

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    That wasn't meant to be a rhetorical question. I was wondering why you thought it wasn't running that code, because I could imagine the scenario where in a release build the compiler effectively does this:
    Code:
    		if(head1==NULL)
    		{
    			goto label: //head1=move;
    		}
    
    		else
    		{
    			move->next=head1;
    			head1->prev=move;
    		label:
    			head1=move;
    		}
    So it would appear to jump over the if block when stepped through.

    However that was a long shot. The most likely scenario is that you're using a horribly broken and outdated compiler such as Turbo C++.
    Visual Studio 2008 Express and Code Blocks are two good free alternatives, where the above code would work as expected.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I've never had much luck with debuggers. When the compiler optimizes code placement for loops, the breakpoints get odd behavior. I think the above code works perfectly... it's just the stepping debugger is confused.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  2. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  3. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21