Im rn learning linked lists and therefor I wanted to make a programm visualizing the mathematical 3x+1 Problem (random number that if its even gets divided by 2 and if its odd gets multiplied by 3 and +1). The linked list should track the History of the numbers. But im struggling implemanting it correctly. Here is my code:
#include <iostream>
#include <cmath>
using namespace std;
int number;
int counter;
struct Liste_Zahlen
{
int data;
Liste_Zahlen* next;
};
int funktion(){
while(number != 1)
{
if(number % 2)
{
number = number/2;
}
else
{
number = 3 * number + 1;
}
Liste_Zahlen* n;
Liste_Zahlen* t;
n = new Liste_Zahlen;
t = n;
n->data = number;
t->next = n;
t = t->next;
counter = counter + 1;
}
return counter;
}
int main(){
cout << "Geben Sie eine Zahl an? " <<endl;
cin >> number;
Liste_Zahlen* n;
Liste_Zahlen* t;
Liste_Zahlen* h;
n = new Liste_Zahlen;
t = n;
h = n;
n->data = number;
funktion();
cout << "Die Zahl " << number << " ist nach " << counter << " Schritten in den Loop gefallen." <<endl;
cout << "Die mit dem Algorithmus erreichten Zahlen sind: " <<
system("pause");
return 0;
}```