Addition in a linked list?

I am creating a linked list and I am confused about how to operator overload the addition of a linked list. this is my .cpp file and the portion im confused about is how to make the addition operator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  #include "LargeInt.h"
#include <string>

using namespace std;
#include "LargeInt.h"
#include <string>

using namespace std;

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ostream &operator<< (ostream &out, const LargeInt &n){
	//forces the display to be in human readable form, not operational order
	out << n.ToString();
	return out;
}

//-----------------------------------------------------------------------------
LargeInt::LargeInt(const string n)
: head(nullptr)
, end(nullptr)
, negative(false)

{
	this->negative = n[0] == '-';
	unsigned int index = this->negative ? 1 : 0;
	while (index < n.size())
	{
		char c = n[index];
		this->Append(atoi(&c));
		++index;
	}
}


//-----------------------------------------------------------------------------
LargeInt::~LargeInt()
{
	ListNode *nodePTR;
	ListNode *nextNode;

	nodePTR = head;

	while (nodePTR != NULL)
	{
		nextNode = nodePTR->next;
		delete nodePTR;
		nodePTR = nextNode;
	}
}

//-----------------------------------------------------------------------------
void LargeInt::Append(int n)
{
	//Allocate new node
	ListNode *newNode = new ListNode(n);

	//if there are 0 newnodes, make newNode the first node.
	if (this->head == nullptr)
	{
		this->head = newNode;
		this->end = newNode;
	}
	else // insert newnode at the end
	{
		newNode->previous = this-> end;
		this->end->next = newNode;
		this->end = newNode;

	}

}

void LargeInt::addDigits()
{ 
	LargeInt current;
	int carry = 0;
	int offset = 0;
	int temp = 0;

	while (temp->next != nullptr)
		temp = temp->next;



}

//-----------------------------------------------------------------------------
LargeInt LargeInt::operator+()
{
	return 0;
}



bool LargeInt::Negative() const
{
	return this->negative;
}

//-----------------------------------------------------------------------------
string LargeInt::ToString() const
{
	//string message = "";
	string message = this->negative ? "-" : "";

	ListNode *current = this->head;

	while (current != nullptr)
	{
		message += to_string(current->number);
		current = current->next;
	}
	
	return message;
}

Last edited on
If this is a singly-linked list, I might be able to help. For future reference, include the class definition (in this case LargeInt), so it is easy to see. If it is any other kind (doubly-linked, circular...etc.), it may not work or be the most efficient, so disregard the following...

You will have to move through the list you already have, by testing if the next pointer is NULL. When it is you create a new node with the value of the first node in the LargeInt &addition you passed in. Create a pointer (ex. LargeInt* ptr) to track the values of addition->next. While that pointer (ex. ptr->next) is not equal to NULL, continue creating new nodes with ptr->data as the data in your nodes.

Link lists can be tricky, and seg faults are easily made. Drawing pictures is incredibly helpful with them. I am probably not right about how you would do this exactly, as I am not sure how your nodes are set up, but the concept is there. You may need additional pointers to track, but you'll have to figure that out yourself.

One more key thing: when you create a node, make sure its next pointer value = NULL, so that when you break out of your loop, the last pointer will have NULL set.
Topic archived. No new replies allowed.