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

Linked List based integer addition I am new to the topic of linked lists and I d

ID: 3630020 • Letter: L

Question

Linked List based integer addition
I am new to the topic of linked lists and I don't know how to define this function to add two linked list based integers and storing the result in a third
can manage the addition algorithm, just need help with the return type en parameters of my function.
//Header file: linkedList.h

#ifndef H_LinkedListType
#define H_LinkedListType

#include <iostream>
#include <cassert>

using namespace std;

//Definitions of the node
template<class Type>
struct nodeType
{
Type info;
nodeType<Type> *link;
};

template<class Type>
class linkedListType
{

friend ostream& operator<<<>(ostream&, const linkedListType<Type> &);
//Overload the stream insertion operator.

//Adding two integers
friend linkedListType<int> AddLists<>(const linkedListType<int> &,
const linkedListType<int> &);

public:
const linkedListType<Type>& operator=
(const linkedListType<Type>& list);
void initializeList();
bool isEmptyList();
int length();
void destroyList();
Type front();
Type back();
bool search(const Type& searchItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();


protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;

private:
void copyList(const linkedListType<Type>& otherList);


};

//Function that I have problem with:
template<class Type>
linkedListType<int> AddLists(const linkedListType<int> &L1,
const linkedListType<int> &L2)
{
linkedListType<Type> L3;
nodeType<Type> *current_L1;
nodeType<Type> *current_L2;
nodeType<Type> *current_L3;

current_L1 = L1.first;
current_L2 = L2.first;
current_L3 = L3.first;

Type carry = 0;

while (current_L1 != NULL || current_L2 != NULL)
{
current_L3->info = current_L1->info + current_L2->info + carry;
if (current_L3->info > 9)
{
carry = current_L3->info/10;
current_L3->info = current_L3->info%10;
}
current_L1 = current_L1->link;
current_L2 = current_L2->link;
current_L3 = current_L3->link;
}

}

//Main to test function with:
#include <iostream>
#include "LinkedList.h"

using namespace std;

int main()
{
linkedListType<int> L1;
linkedListType<int> L2;
linkedListType<int> L3;

//Build up L1 and L2 starting with least signigicant digit
L1.insertLast(5);
L1.insertLast(3);
L1.insertLast(1);

L2.insertLast(2);
L2.insertLast(7);
L2.insertLast(1);

cout<<L1<<endl<<L2<<endl;

AddLists(L1,L2);
cout<<L3<<endl;



return 0;
}

Explanation / Answer

#include
#include

using namespace std;

//Definitions of the node
template
struct nodeType
{
Type info;
nodeType *link;
};

template
class linkedListType
{

friend ostream& operator<<<>(ostream&, const linkedListType &);
//Overload the stream insertion operator.

public:
const linkedListType<Type>& operator=
(const linkedListType<Type>& list);
void initializeList();
bool isEmptyList();
int length();
void destroyList();
Type front();
Type back();
bool search(const Type& searchItem);
void insertFirst(const Type& newItem);
void insertLast(const Type& newItem);
void deleteNode(const Type& deleteItem);
linkedListType();
linkedListType(const linkedListType<Type>& otherList);
~linkedListType();

protected:
int count;
nodeType<Type> *first;
nodeType<Type> *last;

private:
void copyList(const linkedListType<Type>& otherList);


};

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