Solved

# subtracting number using link list

Posted on 2003-03-15

I need to write a function that will subtract large numbers. But I want to use link list that are dynamically allocated. The function will read in two pointers that point to the head of each link list, but the numbers are read in back words so if you put in 123 it would be in the link list as 321 the answer also has to be put in back words because if you saved it back word when I print out the list it will the right way. The pointer is defined as (typedef struct integerNode *integer;) and the structure is (struct node{

int data;

struct node *next;

};)

hear is what I have

integer SubtractIntegers(integer n1, integer n2){

struct node *walker1,*walker2,*current;

int temp,R=0,ans,t=0;

integer answer;

walker1=n1;

walker2=n2;

answer=(struct node *) malloc(sizeof(struct node));

current=(struct node *) malloc(sizeof(struct node));

current->next=answer;

current->next->next=NULL;

if(walker1->data<walker2->data){

walker1->next->data-1;

walker2->data+10;

}

temp=(walker1->data)-(walker2->data);

ans=temp;

current->next->data=ans;

answer=current;

walker1 = walker1->next;

walker2 = walker2->next;

while (walker2!=NULL){

if(walker1==NULL)

ans=walker2->data;

else{

if(walker1->data<walker2->data){

walker1->next->data-1;

walker2->data+10;

}

temp=(walker1->data)-(walker2->data)+R;

ans=temp;

}

current=(struct node *) malloc(sizeof(struct node));

current->next=answer;

current->next->data=ans;

answer=current;

walker1 = walker1->next;

walker2 = walker2->next;

}

current->data=R;

answer=current;

return(answer);

}