Your storeNum and printList look great. A few small things to clear up first:
Here's what happens when I compile your program:
Code:
$ gcc -Wall -g -std=c99 list.c -o list
list.c: In function ‘storeNum’:
list.c:15: warning: assignment from incompatible pointer type
list.c:16: warning: assignment from incompatible pointer type
list.c: In function ‘main’:
list.c:69: warning: implicit declaration of function ‘isdigit’
list.c:71: warning: implicit declaration of function ‘strlen’
list.c:71: warning: incompatible implicit declaration of built-in function ‘strlen’
list.c:72: warning: passing argument 1 of ‘storeNum’ from incompatible pointer type
list.c:10: note: expected ‘struct list *’ but argument is of type ‘struct linked_list *’
list.c:72: warning: assignment from incompatible pointer type
list.c:74: warning: incompatible implicit declaration of built-in function ‘strlen’
list.c:75: warning: passing argument 1 of ‘storeNum’ from incompatible pointer type
list.c:10: note: expected ‘struct list *’ but argument is of type ‘struct linked_list *’
list.c:75: warning: assignment from incompatible pointer type
list.c:83: warning: implicit declaration of function ‘strncmp’
If you don't a list of warnings like this, then turn up the warnings on your compiler.
- Lines 10, 15, 16, 72, 75: All these are because you used 'struct list' on line 10 instead of 'struct linked_list'
- Line 69: You need to #include <ctype.h>
- Line 71, 74, 83: You need to #include <string.h>
I fixed that and ran your program with the given input. Here's what I got:
Code:
$ ./list
Enter a command: 67+89
First number:
{ 9 8 -5 7 6 }
Second number:
{ }
Your add doesn't work because you incorrectly build first_num and second_num. The problems are happening in main, not in add. You can tell because the lists are printed before add is called, so that's not where the problem is. Think about your parsing. Right now, you fill first_num with everything from the user input, from index 0 to length-1. You need to stop adding things to first_num when you get a '+' or '-'. Then, store the + or - for later, and read all the subsequent digits into second num. Again, there is no need for your 'current' variable. Something like:
Code:
i = 0
length = strlen(input) // this just saves us from calling strlen a bunch of times
while i < length and input[i] is a digit
add it to first_num
i++
if i < length and input[i] is a '+' or a '-'
op = input[i]
i++
while i < length and input[i] is a digit
add input[i] to second_num
i++
if op is a '+'
result = add(first_num, second_num)
else if op is a '-'
result = subtract(first_num, second_num)
print result
result shouldn't be global. You can have the list built inside add, using storeNum (which is why I suggested you rewrite it), and simply return the pointer to the head of the list. This will also avoid any strange pointer issues. Lastly, you are making your computations overly complex. There's no need for sum_nc or your if (sum>9) bit. Try:
Code:
struct linked_list *add(struct linked_list *first_num, struct linked_list *second_num)
{
struct linked_list result = NULL; // local list, initialized to NULL
...
sum = temp1->data + temp2->data + carry;
result = storeNum(result, sum % 10);
carry = sum / 10;
...
return result;
}