Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an arithmetic program that stores integers in linked list nodes. Each nod

ID: 3852675 • Letter: C

Question

Create an arithmetic program that stores integers in linked list nodes. Each node will store a three digit integer, Subsequent nodes will hold parts of numbers that are greater than four digits long.

For example: 2,101,453,788 would be represented by four nodes, the first being left padded with zeros, resulting in

002

101

453

788

Your program will need to overload the arithmetic operators + and –

Each number input to the program shall have its own linked list and the result of the arithmetic shall be stored in a results linked list. The final result of the arithmetic operation shall be output to the console, with each part of the number listed with its node location.

Example:

If we wanted to add 10,000 and 845 the output result would be:

The answer is

Node 1 = 010

Node 2 = 845

Giving 10,845

Language: C++

002

101

453

788

Explanation / Answer

#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, *ptr = NULL;
struct node
{
node *next;
int data;
}*start = NULL, *p = NULL, *np = 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 add()
{
int i = 0;
node1 *t = head;
node *v = start;
while (t != NULL)
{
if (v == NULL)
{
t->data1 = t->data1 + i;
i = t->data1 / 10;
t->data1 = t->data1 % 10;
if (t->link == NULL && i == 1)
{
ptr = new node1;
ptr->data1 = i;
ptr->link = NULL;
t->link = ptr;
t = t->link;
}
t = t->link;
continue;
}   
else
{
t->data1 = t->data1 + v->data + i;
i = t->data1 / 10;
t->data1 = t->data1 % 10;
if (t->link == NULL && i == 1)
{
ptr = new node1;
ptr->data1 = i;
ptr->link = NULL;
t->link = ptr;
t = t->link;
}
t = t->link;
v = v->next;
}
}   
}   
void traverse()
{
node1 *q = head;
int c = 0,i = 0;
while (q != NULL)
{
q = q->link;
c++;
}
q = head;
while (i != c)
{
x[c - i - 1] = q->data1;
i++;
q = q->link;
}
cout<<"Result of addition for two numbers:";
for (i = 0; i < c; i++)
{
cout<<x[i]<<" ";
}
}
void swap(int *a,int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int n, x, mod, mod1;
cout<<"Enter the two numbers"<<endl;
cin>>n;
cin>>x;
if (x > n)
{
swap(&x,&n);
}
while (n > 0)
{
mod = n % 10;
n = n / 10;
store(mod);
}
while (x > 0)
{
mod1 = x % 10;
x = x / 10;
keep(mod1);
}
add();
traverse();
getch();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote