Add Two Numbers Represented by Linked Lists in C++

This C++ program takes the values of two large numbers as input and displays the computed value node by node in the resultant linked list.

Here is the source code of the C++ program to display the addition of two large numbers using linked list. The C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is also shown below.

  1. /*
  2.  * C++ Program to use Linked List and add two large Numbers
  3.  */
  4. #include<iostream>
  5. #include<conio.h>
  6. using namespace std;
  7. int c = 0, c1 = 0;
  8. struct node1
  9. {
  10.     node1 *link;
  11.     int data1;
  12. }*head = NULL, *m = NULL, *np1 = NULL, *ptr = NULL;
  13. struct node
  14. {
  15.     node *next;
  16.     int data;
  17. }*start = NULL, *p = NULL, *np = NULL;
  18. void store(int x)
  19. {
  20.     np1 = new node1;
  21.     np1->data1 = x;
  22.     np1->link = NULL;
  23.     if (c == 0)
  24.     {
  25.         head = np1;
  26.         m = head;
  27.         m->link = NULL;
  28.         c++;
  29.     }
  30.     else
  31.     {
  32.         m = head;    
  33.         while (m->link != NULL)
  34.         {
  35.             m = m->link;
  36.         }
  37.         m->link = np1;
  38.         np1->link = NULL;          
  39.     }
  40. }
  41. void keep(int x)
  42. {
  43.     np = new node;
  44.     np->data = x;
  45.     np->next = NULL;
  46.     if (c1 == 0)
  47.     {
  48.         start = np;
  49.         p = start;
  50.         p->next = NULL;
  51.         c1++;
  52.     }
  53.     else
  54.     {
  55.         p = start;
  56.         while (p->next != NULL)
  57.         {
  58.             p = p->next;
  59.         }
  60.         p->next = np;
  61.         np->next = NULL;            
  62.     }
  63. }
  64. void add()
  65. { 
  66.     int i = 0;
  67.     node1 *t = head;
  68.     node *v = start;
  69.     while (t != NULL)
  70.     {
  71.         if (v == NULL)
  72.         {
  73.             t->data1 = t->data1 + i;
  74.             i = t->data1 / 10;
  75.             t->data1 = t->data1 % 10;
  76.             if (t->link == NULL && i == 1)
  77.             {
  78.                 ptr = new node1;
  79.                 ptr->data1 = i;
  80.                 ptr->link = NULL;
  81.                 t->link = ptr;
  82.                 t = t->link;
  83.             }
  84.             t = t->link;
  85.             continue;
  86.         }   
  87.         else
  88.         {
  89.             t->data1 = t->data1 + v->data + i;
  90.             i = t->data1 / 10;
  91.             t->data1 = t->data1 % 10;
  92.             if (t->link == NULL && i == 1)
  93.             {
  94.                 ptr = new node1;
  95.                 ptr->data1 = i;
  96.                 ptr->link = NULL;
  97.                 t->link = ptr;
  98.                 t = t->link;
  99.             }
  100.             t = t->link;
  101.             v = v->next;
  102.         }
  103.     }           
  104. }       
  105. void traverse()
  106. {
  107.     node1 *q = head;
  108.     int c = 0,i = 0;
  109.     while (q != NULL)
  110.     {
  111.         q = q->link;
  112.         c++;
  113.     }
  114.     q = head;
  115.     while (i != c)
  116.     {
  117.         x[c - i - 1] = q->data1;
  118.         i++;
  119.         q = q->link;
  120.     }
  121.     cout<<"Result of addition for two numbers:";
  122.     for (i = 0; i < c; i++)
  123.     {
  124.         cout<<x[i]<<"\t";
  125.     }
  126. }
  127. void swap(int *a,int *b)
  128. {
  129.     int temp;
  130.     temp = *a;
  131.     *a = *b;
  132.     *b = temp;
  133. }
  134. int main()
  135. {
  136.     int n, x, mod, mod1;
  137.     cout<<"Enter the two numbers"<<endl;
  138.     cin>>n;
  139.     cin>>x;
  140.     if (x > n)
  141.     {
  142.         swap(&x,&n);
  143.     }
  144.     while (n > 0)
  145.     {
  146.         mod = n % 10;
  147.         n = n / 10;
  148.         store(mod);
  149.     }
  150.     while (x > 0)
  151.     {
  152.         mod1 = x % 10;
  153.         x = x / 10;
  154.         keep(mod1);
  155.     }
  156.     add();
  157.     traverse();
  158.     getch();
  159. }

 
Output
 
Enter the two numbers
564
1999
Result of addition for two numbers:2      5       6       3

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.