Friday 24 November 2017

C++ Program to use Linked List and subtract two large Numbers


Code:

#include   iostream
#include   conio.h
using namespace std;
int c = 0,c1 = 0;
struct node1
{
    node1 *link;
    int data1;
}*head = NULL, *m = NULL, *np1 = NULL, *n = NULL;
struct node
{
    node *next;
    int data;
}*start = NULL, *p = NULL, *np = NULL, *q = NULL;
void store(int x)
{
    np1 = new node1;
    np1->data1 = x;
    np1->link = NULL;
    if (c == 0)
    {
        head = np1;
        m = head;
        m->link = NULL;
        c++;
    }
    else
    {
        m = head;    
        while (m->link != NULL)
        {
            m = m->link;
        }
        m->link = np1;
        np1->link = NULL;          
    }
}
void keep(int x)
{
    np = new node;
    np->data = x;
    np->next = NULL;
    if (c1 == 0)
    {
        start = np;
        p = start;
        p->next = NULL;
        c1++;
    }
    else
    {
        p = start;
        while (p->next != NULL)
        {
            p = p->next;
        }
        p->next = np;
        np->next = NULL;            
    }   
}
void sub()
{
    int x;
    p = start;
    m = head;
    while (p != NULL)
    {
        if (p->data >= m->data1)
        {
            p->data = p->data - m->data1;
            p = p->next;
            m = m->link;
            continue;
        }
        else if (p->data < m->data1)
        {
            q = p;
            n = m;
            x = 0;
            do
            {
                if (q->data <= n->data1 && x == 0)
                {
                    q->data = q->data + 10;
                    q = q->next;
                    n = n->link;
                    x++;
                }
                else if (q->data <= n->data1 && x != 0)
                {
                    q->data = q->data + 9;
                    q = q->next;
                    n = n->link;
                    x++;
                }
                if (q->data > n->data1)
                {
                    q->data = q->data - 1;
                } 
            }   
            while (q->data  < n->data1);
        }
    }
}
void traverse()
{
    node *q = start;
    int c = 0, i = 0;
    while (q != NULL)
    {
        q = q->next;
        c++;
    }
    q = start;
    while (i != c)
    {
        x[c - i - 1] = q->data;
        i++;
        q = q->next;
    }
    cout<<"Result of subtraction for two numbers:\t";
    for (i = 0; i < c; i++)
    {
        cout<
    }
}
void swap(int *a,int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
int main()
{
    int n, x, mod, mod1;
    int n1 = 0, n2 = 0;
    cout<<"Enter the two numbers"<
    cin>>n;
    cin>>x;
    if (x > n)
    {
        swap(&x, &n);
    }
    while (n > 0)
    {
        mod = n % 10;
        n = n / 10;
        keep(mod);
        n1++;
    }
    while (x > 0)
    {
        mod1 = x % 10;
        x = x / 10;
        store(mod1);
        n2++;
    }
    n1 = n1 - n2;
    while (n1 > 0)
    {
        store(0);
        n1--;
    }
    sub();
    traverse();
    getch();
}


Output:

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



More C++ Programs:


















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...