Subtract Two Numbers Represented as 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 subtraction 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 subtract 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, *n = NULL;
  13. struct node
  14. {
  15.     node *next;
  16.     int data;
  17. }*start = NULL, *p = NULL, *np = NULL, *q = 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 sub()
  65. {
  66.     int x;
  67.     p = start;
  68.     m = head;
  69.     while (p != NULL)
  70.     {
  71.         if (p->data >= m->data1)
  72.         {
  73.             p->data = p->data - m->data1;
  74.             p = p->next;
  75.             m = m->link;
  76.             continue;
  77.         }
  78.         else if (p->data < m->data1)
  79.         {
  80.             q = p;
  81.             n = m;
  82.             x = 0;
  83.             do
  84.             {
  85.                 if (q->data <= n->data1 && x == 0)
  86.                 {
  87.                     q->data = q->data + 10;
  88.                     q = q->next;
  89.                     n = n->link;
  90.                     x++;
  91.                 }
  92.                 else if (q->data <= n->data1 && x != 0)
  93.                 {
  94.                     q->data = q->data + 9;
  95.                     q = q->next;
  96.                     n = n->link;
  97.                     x++;
  98.                 }
  99.                 if (q->data > n->data1)
  100.                 {
  101.                     q->data = q->data - 1;
  102.                 } 
  103.             }   
  104.             while (q->data  < n->data1);
  105.         }
  106.     }
  107. }
  108. void traverse()
  109. {
  110.     node *q = start;
  111.     int c = 0, i = 0;
  112.     while (q != NULL)
  113.     {
  114.         q = q->next;
  115.         c++;
  116.     }
  117.     q = start;
  118.     while (i != c)
  119.     {
  120.         x[c - i - 1] = q->data;
  121.         i++;
  122.         q = q->next;
  123.     }
  124.     cout<<"Result of subtraction for two numbers:\t";
  125.     for (i = 0; i < c; i++)
  126.     {
  127.         cout<<x[i]<<"\t";
  128.     }
  129. }
  130. void swap(int *a,int *b)
  131. {
  132.     int temp;
  133.     temp = *a;
  134.     *a = *b;
  135.     *b = temp;
  136. }
  137. int main()
  138. {
  139.     int n, x, mod, mod1;
  140.     int n1 = 0, n2 = 0;
  141.     cout<<"Enter the two numbers"<<endl;
  142.     cin>>n;
  143.     cin>>x;
  144.     if (x > n)
  145.     {
  146.         swap(&x, &n);
  147.     }
  148.     while (n > 0)
  149.     {
  150.         mod = n % 10;
  151.         n = n / 10;
  152.         keep(mod);
  153.         n1++;
  154.     }
  155.     while (x > 0)
  156.     {
  157.         mod1 = x % 10;
  158.         x = x / 10;
  159.         store(mod1);
  160.         n2++;
  161.     }
  162.     n1 = n1 - n2;
  163.     while (n1 > 0)
  164.     {
  165.         store(0);
  166.         n1--;
  167.     }
  168.     sub();
  169.     traverse();
  170.     getch();
  171. }

 
Output
 
Enter the two numbers
2567
989
Result of subtraction for two numbers:  1       5       7       8

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

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

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.