# 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;

walker1=n1;
walker2=n2;
current=(struct node *) malloc(sizeof(struct node));
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;
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->data=ans;

walker1 = walker1->next;
walker2 = walker2->next;
}

current->data=R;
}
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
looks like homework to me.
And we cant do your homework for you.
0
OwnerCommented:
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?
0
Data Warehouse Architect / DBACommented:
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
0
Author Commented:
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.
0
Commented:
Conceptualy from Kdo:
> 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.

Thats why the list is backward :)
123 = [3]->[2]->[1]
4567 = [7]->[6]->[5]->[4]

I wonder about negative number ??

Then about your data structure, typedefing a structure as pointer is confusing. It should be
typedef struct node {
int data;
struct  node *next;
} integer;

Or use Borland's convention
typedef struct node {
int data;
struct  node *next;
} TInteger, *PInteger;

Consequently your function should look like
integer* SubstactIntegers(integer* n1, integer* n2)
{
struct node *walker1,*walker2,*current;

/* SUBSTACT ALGORITHM HERE */
....

}

And now about the substact algorithm, you are completely wrong on three things.
1. Your loop should be based on walker1 and walker2, add each digit until the end of walker1 and walker2. You must take care if the length is not the same.
2. You should allocate a new node for the result for each digit.
3. Don't forget the elementary math as noted by Kdo
> Now subtract, record the result, and bring along any "carry" as you work your way up the list

0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Ouchh ...
I wrote it as SUBSTRACT,
but my wordprocessor changed it to SUBSTACT.

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 !

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

This is interesting homework :)

0
Author Commented:
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.
0
Data Warehouse Architect / DBACommented:
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
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.