-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_9-10_integer_linked_list.cpp
More file actions
182 lines (135 loc) · 3.34 KB
/
4_9-10_integer_linked_list.cpp
File metadata and controls
182 lines (135 loc) · 3.34 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
struct listNode {
int num;
listNode * next;
};
typedef listNode * intLinkedList;
void printInteger(intLinkedList integer);
void addDigit(intLinkedList& integer, char charDigit);
intLinkedList sumIntLists(intLinkedList integer1, intLinkedList integer2);
void removeLinkedList(intLinkedList &head);
int main()
{
char charDigit = 0;
intLinkedList integer1 = nullptr;
cout << "Type in a number:\n";
charDigit = cin.get();
while(charDigit != 10)
{
addDigit(integer1, charDigit);
charDigit = cin.get();
}
cout << "\nThe number is:\n";
printInteger(integer1);
intLinkedList integer2 = nullptr;
charDigit = 0;
cout << "\nType in another number:\n";
charDigit = cin.get();
while(charDigit != 10)
{
addDigit(integer2, charDigit);
charDigit = cin.get();
}
cout << "\nThe number is:\n";
printInteger(integer2);
intLinkedList sumInteger = sumIntLists(integer1, integer2);
cout << "\nThe sum of the numbers is:\n";
printInteger(sumInteger);
removeLinkedList(integer1);
removeLinkedList(integer2);
removeLinkedList(sumInteger);
cin.get();
return 0;
}
void addDigit(intLinkedList& integer, char charDigit)
{
if (charDigit < '0' || charDigit > '9')
{
cout << "Wrong character" << endl;
return;
}
int digit = charDigit - '0';
listNode * newNode = new listNode;
newNode->num = digit;
newNode->next = integer;
integer = newNode;
}
void printInteger(intLinkedList integer)
{
listNode * loopPtr = integer;
if (loopPtr != nullptr)
{
printInteger(loopPtr->next);
cout << loopPtr->num;
}
}
// Exercise 4.10
intLinkedList sumIntLists(intLinkedList integer1, intLinkedList integer2)
{
intLinkedList head;
int digit1 = 0;
int digit2 = 0;
int sumDigit = 0;
int extraVal = 0;
listNode * loopPtr1 = integer1;
listNode * loopPtr2 = integer2;
listNode * loopPtrSum = new listNode;
head = loopPtrSum;
while (loopPtr1 != nullptr || loopPtr2 != nullptr)
{
if (loopPtr1 != nullptr)
{
digit1 = loopPtr1->num;
loopPtr1 = loopPtr1->next;
}
if (loopPtr2 != nullptr)
{
digit2 = loopPtr2->num;
loopPtr2 = loopPtr2->next;
}
sumDigit = digit1 + digit2 + extraVal;
if (sumDigit >= 10)
{
extraVal = 1;
sumDigit = sumDigit - 10;
}
else
{
extraVal = 0;
}
loopPtrSum->num = sumDigit;
digit1 = digit2 = 0;
// Check if next digit exists
if (loopPtr1 != nullptr || loopPtr2 != nullptr || extraVal)
{
loopPtrSum->next = new listNode;
}
else
{
loopPtrSum->next = nullptr;
}
loopPtrSum = loopPtrSum->next;
}
if (extraVal)
{
loopPtrSum->num = 1;
loopPtrSum->next = nullptr;
}
return head;
}
void removeLinkedList(intLinkedList &head)
{
listNode * loopPtr = head;
listNode * deleteNode;
while (loopPtr != nullptr)
{
deleteNode = loopPtr;
loopPtr = loopPtr->next;
delete deleteNode;
}
head = nullptr;
return;
}