0% found this document useful (0 votes)
285 views5 pages

Addition and Multiplication of Huge Numbers Using Doubly Linked List - C ++

This C++ program defines classes for handling large integers (hugeints) using linked lists. The hugeint class contains functions to insert nodes, initialize from a string, and display. Additional functions are defined to add and multiply two hugeints. The main function gets two numbers as strings, initializes hugeint objects, calls the add and multiply functions, and displays the results.

Uploaded by

Akshara P S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
285 views5 pages

Addition and Multiplication of Huge Numbers Using Doubly Linked List - C ++

This C++ program defines classes for handling large integers (hugeints) using linked lists. The hugeint class contains functions to insert nodes, initialize from a string, and display. Additional functions are defined to add and multiply two hugeints. The main function gets two numbers as strings, initializes hugeint objects, calls the add and multiply functions, and displays the results.

Uploaded by

Akshara P S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include<bits/stdc++.

h>
#include<string>
#include<iostream>

using namespace std;

class node
{
public:
int data;
node *prev;
node *next;
node( int data)
{
this->data = data;
this->next = NULL;
this->prev = NULL;
}

};

class hugeint
{
public:
node* front;
node* rear;
int size;
hugeint()
{
front = rear = NULL;
size=0;
}
void insertend(node*);
void insertfront(node*);
void initialise(string);
//void mult(hugeint*, hugeint*);
void display();
};

hugeint add(hugeint a, hugeint b)


{
node* p,*q;
hugeint c;
p=a.rear; q=b.rear;
int carry = 0;

int val,a1,b1;
while(p!=a.front->prev || q!=b.front->prev)
{

if(p==a.front->prev)
{ a1 = 0;
}
else
{
a1 = p->data;
}
if(q == b.front->prev)
{
b1=0;
}
else
{
b1 = q->data;
}

val = a1+b1+carry;
node* n = new node(val%10);
c.insertfront(n);
if(val/10)
{
carry = val/10;
}
else
{
carry = 0;
}
if(p!=NULL) p=p->prev;
if(q!=NULL) q=q->prev;
}
if(carry)
{
node * n = new node(carry);
c.insertfront(n);
}

return c;
}

hugeint mult(hugeint a, hugeint b)


{
int shift=0;
node *p,*q,*r;
hugeint d;
p=a.rear; q=b.rear;
int val,carry=0;
int size = a.size+b.size;
for(int i=0;i<size;i++)
{
node * n = new node(0);
d.insertend(n);

while(q!=b.front->prev)
{
p = a.rear;
r = d.rear;
for(int i = 0; i<shift ; i++)
{
r=r->prev;

}
while(p!=a.front->prev)
{
val = (q->data * p->data)+carry;

val = r->data+val;

r->data = val%10;
carry = (val/10);

r = r->prev;
p = p->prev;

}
while(carry)
{
val = r->data+carry;
r->data=val%10;
carry = val/10;
r = r->prev;
}
q=q->prev;
shift++;

}
/*r = d.front;
while(r->data==0)
{
node * temp = r;
r=r->next;
delete(temp);
}
d.front =r; */
return d;

void hugeint::insertend(node* n)
{
if(front == NULL)
{
front = rear = n;
}
else
{
n->prev = rear;
rear->next = n;
rear = n;
}
size++;
}

void hugeint::insertfront(node*n)
{
if(front == NULL)
{ front=rear=n;
}
else
{
n->next = front;
front->prev=n;
front = n;
}
size++;
}

void hugeint::initialise(string s)
{
int l = s.length();
for(int i =0;i<s.length();i++)
{
node *n = new node(s[i]-'0');
insertend(n);

void hugeint::display()
{
if(front==NULL)
{
cout<<"\nEmpty";
return;
}
else
{
node* temp = front;
while(temp!=rear)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<temp->data<<endl;
}
}

int main()
{
hugeint a,b,c,d;

string m,n;

cout<<"Enter first number:";


getline(cin,m);

cout<<"Enter second number:";


getline(cin,n);
a.initialise(m);
b.initialise(n);
a.display();
b.display();

c=add(a,b);
cout<<"Result = ";
c.display();
d = mult(a,b);
cout<<"Product = ";
d.display();
}

You might also like