Advertisement
Guest User

addition

a guest
Oct 8th, 2013
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. //program to calculate sum of two long integers using linked list
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6.  
  7. typedef struct node *nodeptr;
  8. struct node{
  9. int data;
  10. nodeptr next;
  11. };
  12.  
  13. list_print(nodeptr s)
  14.    {
  15.  
  16.    if(s->next!=NULL)
  17.    {
  18.  
  19.    list_print(s->next);
  20.    printf("%d",s->data);
  21.    }
  22.    else{printf("%d",s->data);}
  23.  
  24.    }
  25. list_print_str(nodeptr s)
  26.    {
  27.  
  28.    while(s->next!=NULL)
  29.    {
  30.      printf("%d",s->data);
  31.      s=s->next;
  32.    }
  33.    printf("%d",s->data);
  34.  
  35.    }
  36. int num_list_add_leng(nodeptr s ,char str[])
  37. {
  38.  
  39.  int len=strlen(str);
  40.  
  41.  int x=0;
  42.  while(x<len)
  43.  {
  44.                 int d=(int)str[x]-(int)'0';
  45.                 nodeptr temp=NULL;
  46.                 temp=malloc(sizeof(nodeptr));
  47.                 temp->next=s->next;
  48.                 s->next=temp;
  49.                 temp->data=d;
  50.                 x++;
  51.  }
  52.  return (len);
  53.  
  54. }
  55. initialize(nodeptr one,nodeptr two,nodeptr ans)
  56. {
  57.     one->next=NULL;
  58.     two->next=NULL;
  59.     ans->next=NULL;
  60.     one->data=0;
  61.     two->data=0;
  62. }
  63. int bfr=0;
  64.  
  65.  
  66.  
  67. add(nodeptr one,nodeptr two,nodeptr ans)
  68. {
  69.  
  70.     int ad1,ad2;
  71.  while(1){
  72.  
  73.     if((one->next!=NULL)&&(two->next!=NULL))
  74.     {
  75.         one=one->next;
  76.         ad1=one->data;
  77.         two=two->next;
  78.         ad2=two->data;
  79.     }
  80.     else{
  81.     if((one->next==NULL)&&(two->next!=NULL))
  82.     {
  83.  
  84.         ad1=0;
  85.         two=two->next;
  86.         ad2=two->data;
  87.  
  88.     }
  89.  
  90.     else{
  91.     {
  92.         ad2=0;
  93.         one=one->next;
  94.         ad1=one->data;
  95.  
  96.     }
  97.     }
  98.     }
  99.  
  100.     nodeptr temp=NULL;
  101.     temp=malloc(sizeof(nodeptr));
  102.     temp->next=ans->next;
  103.     ans->next=temp;
  104.     temp->data=(ad1+ad2+bfr)%10;
  105.     bfr=(ad1+ad2+bfr)>9?1:0;
  106.     if((one->next==NULL)&&(two->next==NULL))
  107.     break;
  108.     }
  109.     if(bfr)
  110.     {
  111.     nodeptr temp=NULL;
  112.     temp=malloc(sizeof(nodeptr));
  113.     temp->next=ans->next;
  114.     ans->next=temp;
  115.     temp->data=1;
  116.  
  117.     }
  118.  
  119.  
  120. }
  121. main()
  122. {
  123.  nodeptr one=NULL;
  124.  nodeptr two=NULL;
  125.  nodeptr ans=NULL;
  126.  one=malloc(sizeof(nodeptr));
  127.  two=malloc(sizeof(nodeptr));
  128.  ans=malloc(sizeof(nodeptr));
  129.  initialize(one,two,ans);
  130.  char a[10000];
  131.  printf("first number ");
  132.  scanf("%s",a);
  133.  int l1=num_list_add_leng(one,a);
  134.  printf("\n second number ");
  135.  scanf("%s",a);
  136.  int l2=num_list_add_leng(two,a);
  137.  list_print(one->next);
  138.  
  139.  printf("\n");
  140.  list_print(two->next);
  141.  printf("\n");
  142.  add(one,two,ans);
  143.  list_print_str(ans->next);
  144.  
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement