Link to home
Start Free TrialLog in
Avatar of bigred1283
bigred1283

asked on

subtracting number using link list

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);
}
Avatar of oferh
oferh

looks like homework to me.
And we cant do your homework for you.
While we can't do your homework for you, we're happy to answer questions on points where you're stuck. It's great that you've shown us that you've made an attempt at doing this problem yourself. What's your question?
Avatar of Kent Olsen
Hi Bigred,

Your instructor has done you a disservice in not teaching you the basics of how this is done.  But you're not alone -- there are a lot of posts to this forum from students with ineffective instructors.

The technique is pretty straight-forward.  I'll describe the process for integers, you can extend it for reals if you need to.

Build your "numbers" as linked lists of their digits (a 10 digit number has 10 nodes, etc).  Addition/subtraction are "right aligned" so align the lowest order digits by traversing the lists to their last node.  Now subtract, record the result, and bring along any "carry" as you work your way up the list.

You get to do the actual coding -- after all, it IS your class.  :)


Kdo
Avatar of bigred1283

ASKER

Yes it is homework but I do not want to write the function for me I just need someone help to fix the code I have.
ASKER CERTIFIED SOLUTION
Avatar of Kocil
Kocil

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ouchh ...
I wrote it as SUBSTRACT,
but my wordprocessor changed it to SUBSTACT.
Please replace it on the fly as you read :)

And to make it clearer, I resume my digram
n1 = 4567 = [7]->[6]->[5]->[4]
n2 = 123 = [3]->[2]->[1]
result = 4567-123 = [7-3]->[6-2]->[5-1]->[4].

Now think the cases of:
123-456 = ?
123-4567 = ?

and how about negative number ?

(-123)- 456 = ?
123-(-456) = ?

Oh damn ... that's complicated !

Hey another cases
123-33  -> decreasing nodes
123-123 -> empty nodes or one node with Zero

This is interesting homework :)

I do not need to worry about negative number because I compare the two numbers read in so n1 will always be larger then n2.
Hi BigRed,

It's apparent that you're struggling with both the math and the C implementation.  You might find it helpful to solve the math, then the C.  Instead of linked lists, use strings (arrays of char) to work out the math.  Once your confident in the math, rework the algorithm to traverse a linked list instead of an arry.  (Hint:  create a large array, perhaps 100 characters, zero the first byte, and read the string starting with the second byte.  The string will be zero terminated at both ends and you can easily march up and down it.)

Your program uses a list for each operand but puts the answer into an 'int'.  I would expect that the reason for storing the numbers as a linked list is to allow vary large input values, which implies very large results.  The 'int' is ok for development, but as Kocil suggests, your result structure needs to be compatible with the input structure.

You have to walk before you can run.  :)


Kdo