Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

subtracting number using link list

Posted on 2003-03-15
9
Medium Priority
?
422 Views
Last Modified: 2013-12-14
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);
}
0
Comment
Question by:bigred1283
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 1

Expert Comment

by:oferh
ID: 8146416
looks like homework to me.
And we cant do your homework for you.
0
 
LVL 20

Expert Comment

by:jmcg
ID: 8146506
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8146574
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bigred1283
ID: 8147090
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
 
LVL 5

Accepted Solution

by:
Kocil earned 300 total points
ID: 8147746
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;
    integer* answer;

    /* SUBSTACT ALGORITHM HERE */
    ....

    return answer;
}


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

Please refer to my diagram and REWRITE your algorithm.

0
 
LVL 5

Expert Comment

by:Kocil
ID: 8147781
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 !

0
 
LVL 5

Expert Comment

by:Kocil
ID: 8147825
Hey another cases
123-33  -> decreasing nodes
123-123 -> empty nodes or one node with Zero

This is interesting homework :)

0
 

Author Comment

by:bigred1283
ID: 8148197
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 8148548
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question