Advertisement
eduardovp97

ex5.c

Oct 24th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "listas.h"
  4.  
  5. void sumar(TLista *x, TLista *y, TLista *c);
  6.  
  7. int main(){
  8.    
  9.     TLista a,b,c;
  10.  
  11.     init_list(&a);
  12.     init_list(&b);
  13.     init_list(&c);
  14.     //1234
  15.     insert_list_final(&a,1);
  16.     insert_list_final(&a,2);
  17.     insert_list_final(&a,3);
  18.     insert_list_final(&a,4);
  19.     print_list(&a);
  20.     //219
  21.     insert_list_final(&b,2);
  22.     insert_list_final(&b,1);
  23.     insert_list_final(&b,9);
  24.     print_list(&b);
  25.     sumar(&a,&b,&c);
  26.     print_list(&c);
  27.     return 0;
  28. }
  29.  
  30. void sumar(TLista *x, TLista *y, TLista *c){
  31.     int carry = 0;
  32.     TLista a,b;
  33.  
  34.     init_list(&a);
  35.     init_list(&b);
  36.    
  37.     invertir(x,&a);
  38.     invertir(y,&b);
  39.     printf("Hola\n");
  40.     TNode *n1 = malloc(sizeof(TNode));
  41.     TNode *n2 = malloc(sizeof(TNode));
  42.     n1 = a.first; // a->first == (*a.first)
  43.     n2 = b.first;
  44.     while(n1 != NULL && n2 != NULL){
  45.         insert_list_first(c,(n1->info + n2->info + carry)%10);
  46.         carry = (n1->info + n2->info + carry) / 10;
  47.         n1 = n1->next;
  48.         n2 = n2->next;
  49.     }
  50.     while(n1 != NULL){
  51.         insert_list_first(c,(n1->info + carry) % 10);
  52.         carry = (n1->info + carry) / 10;
  53.         n1 = n1->next;
  54.     }
  55.     while(n2 != NULL){
  56.         insert_list_first(c,(n2->info + carry) % 10);
  57.         carry = (n2->info + carry) / 10;
  58.         n2 = n2->next;
  59.     }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement