This is the first piece of code I've written on my own since I started learning a couple of months ago. The code works and it takes an int as input and creates a linked list with each node representing a digit. I've added a function that sums two linked listed ints and returns a third list with the result. The rest of the code is also distributed in functions.
Before I move on, it would be greatly appreciated to know if I'm doing something wrong, if my code is properly structured or if I'm following a good/bad style.
#include <iostream>
using std::cout;
using std::cin;
struct intNode {
int num;
intNode *next;
};
int deallocateList(intNode *&s);
int intConversionHelper(int n);
int lenList(intNode *&s);
int sumListContent(intNode *&s);
intNode *sumLists(intNode *&s1, intNode *&s2);
int printNode(intNode *&s);
int intLen(int n);
intNode *intToList(int n);
void append(intNode *&s, int n);
int main() {
int num1;
int num2;
cout << "Give me number 1: ";
cin >> num1;
cout << "Give me number 2: ";
cin >> num2;
intNode *list1 = intToList(num1);
intNode *list2 = intToList(num2);
cout << "\nPrinting ints as linked lists: \n";
printNode(list1);
printNode(list2);
cout << "\nSum of the two lists (as a list too!)\n";
intNode *sumList = sumLists(list1, list2);
printNode(sumList);
deallocateList(list1);
deallocateList(list2);
deallocateList(sumList);
}
int printNode(intNode *&s) {
intNode *tmp = s;
while (tmp != NULL) {
cout << tmp->num;
tmp = tmp->next;
}
cout << "\n";
}
int intLen(int n) {
int len = 1;
while (n > 9) {
len++;
n /= 10;
}
return len;
}
intNode *intToList(int n) {
intNode *intList = NULL;
int num;
int dec = 10;
num = n % 10;
append(intList, num);
int count = 1;
while (count < intLen(n)) {
num = n / dec % 10;
dec = dec * 10;
append(intList, num);
count++;
}
return intList;
}
void append(intNode *&s, int n) {
if (s == NULL) {
s = new intNode;
s->num = n;
s->next = NULL;
}
else {
intNode *nNode = new intNode;
nNode->num = n;
nNode->next = s;
s = nNode;
}
}
intNode *sumLists(intNode *&s1, intNode *&s2) {
int sum1 = sumListContent(s1);
int sum2 = sumListContent(s2);
return intToList(sum1 + sum2);
}
int sumListContent(intNode *&s) {
intNode *tmp = s;
int sum = 0;
int len = lenList(s);
while (tmp != NULL) {
sum += intConversionHelper(len) * tmp->num;
len--;
tmp = tmp->next;
}
return sum;
}
int lenList(intNode *&s) {
intNode *tmp = s;
int len = 0;
while (tmp != NULL) {
len++;
tmp = tmp->next;
}
return len;
}
int intConversionHelper(int n) {
float value = n;
for (int i = 1; i < n; i++) {
value = value * 10;
}
value = value / n;
return value;
}
int deallocateList(intNode *&s) {
intNode *tmp = s;
while (s != NULL) {
tmp = tmp->next;
delete[] s;
s = tmp;
}
}
delete[]indeleteList(should bedelete, since the node gets allocated withnew, notnew[]). However, you, msaltz, should edit the purpose of your code into your post. \$\endgroup\$