#linked list

201 messages · Page 1 of 1 (latest)

onyx fulcrum
#

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;
}```
dull jettyBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

kindred jasper
onyx fulcrum
#

thx man

kindred jasper
onyx fulcrum
#

I briefly explained the mathematical part above, but I think its not that important for my problem. I want to create a linked list that hosts and prints in the end every number out for my math algorithm for example if u set number to 8, it should print out 8, 4, 2, 1.

#

or 5 should print out 5, 16, 8, 4, 2, 1

kindred jasper
#

yep i'm aware of this 3x + 1

#

my questions is, what is the outcome you are getting, vs the outcome you want

#

is the maths wrong? or the list not working? giving you runtime error?

onyx fulcrum
#

I didnt test it probably because my first problem is to cout the list in the end

#

math is right

#

list is not working ig

kindred jasper
#

right so you want help with the list

#

right first of all

#

You're using new but not delete -> this is a memory leak (bad)

onyx fulcrum
#

like the code is outside of the list not 100% working either but I should be capable of doing this. But yeah I need help with the list

kindred jasper
#

i think at one point

#

number will never be 1 again

#

that is an infinite loop

onyx fulcrum
#

but it stops when it hits 1 the first time right?

kindred jasper
#

right okay

kindred jasper
onyx fulcrum
#

oh

#

how can I implement it then that it creates a new node every loop in the while loop

#

thats like the main think what I dont understand

kindred jasper
#

I think you should try do a linked list by itself instead of implementing it using this 3x + 1 function thing

#

meaning using a simple node

#

and add to the list using a function, and monitor the amount of nodes etc...

#

even though its not like, "usable", you need to understand how to actually implement it first

#

i think this 3x + 1 is a good idea though for a linked list

onyx fulcrum
#

what do u mean with "by itself"

kindred jasper
#

Meaning do not try implement it using 3x+1

#

struct list
{
  int data;
  list* next_node;
};

^ keep that

onyx fulcrum
#

u mean to try to use linked list first in a more simple way to understand it better?

kindred jasper
#

exactly what i mean yes

#

then, you can use the conjecture thing (the 3x+1 function)

#

will be much easier

onyx fulcrum
#

okay I will try it

kindred jasper
#

practically, instead of giving it a usage, make it work first

#

to get from point A to point B you need to be at point A first :D

onyx fulcrum
#

okay that makes sense

#

haha

kindred jasper
#

yeah of course

#

keep this thread open ifyou need to ask anything else

#

also another suggestion try use english in code, it doesnt really matter but for some people it might be easier to read

#

or actually dont use german and english in the same code

#

use one xD

onyx fulcrum
#

but I have alr the question how is it possible to create a loop that makes a new node every loop

kindred jasper
#

how do you create a new node?

onyx fulcrum
onyx fulcrum
kindred jasper
#

i do not understand what you mean by foot

#

Create 1 node first

#

add a node to a list

#

what i am asking you to do first is create the absolute bare minimum for a linked list

onyx fulcrum
#

okay I'll do that and will come back after that or when I will face new problems by doing it xd

#

but ty alr for the help

kindred jasper
#

ofc np

dull jettyBOT
#

@onyx fulcrum Has your question been resolved? If so, type !solved :)

onyx fulcrum
#

okay im ready now

#
#include <iostream>
using namespace std;
int main() {
    struct Node{
        int data;
        Node* next;
    };
Node* n;
Node* t;
Node* h;
n = new Node;
t=n;
h=n;
n->data = 1;
n = new Node;
n->data = 2;
t->next = n;
t = t->next;
n = new Node;
n->data = 3;
t->next = n;


cout << "List: " << h->data << " " << t->data << " " << n->data <<endl;
    system("pause");
    return 0;
}```
#

The output is 1 2 3

#

how can I put it in a loop?

kindred jasper
#

right, you might want to create a class specificially for the linked list

#

and create a sort of

add_node()

function

onyx fulcrum
#

hm okay lets see if I get this

kindred jasper
#

one sec, let me ask someone more experienced to help you

placid moon
#

yeah if you want to use linked lists, make a linked lists class first

#

That will give you a better idea of how they work as well

kindred jasper
#

yes exactly

#

Node class, and the actual list class

placid moon
#

Make functions to add, remove, etc

kindred jasper
#

Remember, the node is independent from the list

onyx fulcrum
#

wait a class or a function

placid moon
#

A class for the linked list

kindred jasper
#

class

#

with functions in it

onyx fulcrum
#

okay

kindred jasper
#

member functions

placid moon
#

You can use a struct for the node

onyx fulcrum
#

I`ll try

prisma crown
kindred jasper
#

yes and a class for the list since you might want to have some access modifiers

kindred jasper
#

pls use delete

prisma crown
#

so what exactly was the problem?

kindred jasper
#

i think his issue was with how to actually implement it into a loop, but they didn't know how to create a linked list in the first place

placid moon
#

Yeah that's what it sounds like to me too

onyx fulcrum
#

like the original problem was that I wanted to make a linked list that stores every single number beginning with the number a user puts in that goes through the 3x+1 algorithm and cout everything in the end

placid moon
#

Gotta get an actual linked list going first

onyx fulcrum
#

if that was the question xd

kindred jasper
#

right it was

#

but you don't know how to make a linked list, so you should first do that

#

remember also, a list is just how you store the nodes, nodes and lists are different if this makes sense to you :D

placid moon
#

^ Then you can just use the linked list class, which will be easier

#

A linked list is made up of nodes

kindred jasper
#

yep exactly

onyx fulcrum
#

yeah so now I should implement a class with a delete and add function ?

placid moon
#

With whatever functions you need 🙂

#

Some sort of add and remove would be a good start

kindred jasper
kindred jasper
plain scarab
onyx fulcrum
#

how can I detect memory leaks?

placid moon
#

valgrind

kindred jasper
#

you can't neccessarily detect them

#

or more or less there is no need to

kindred jasper
#

but kind of, think of it like this :

whenever a new is called -> a delete must be called

placid moon
#

Well, whenever you're finished with that pointer, before you go out of scope

#

otherwise you have a dangling pointer

kindred jasper
placid moon
#

Or yeah smart pointers work

kindred jasper
#

the new and delete

#

not neccessarily advanced

placid moon
#

Another good thing to learn

#

They're preferred to manual memory allocation nowadays

kindred jasper
#

yeah you can definitely figure t hem out really quickly

placid moon
#

However, I will say

#

If you don't know manual memory allocation, it's good to learn it

plain scarab
placid moon
#

So you understand how it works

plain scarab
#

It deletes itself when it goes out of scope

onyx fulcrum
plain scarab
placid moon
#

You should learn it

kindred jasper
#

you are having trouble visualizing it

onyx fulcrum
kindred jasper
#

^ when you add, it adds a new node, so you'd have 4 here

#

when you remove, it pops a node, so you'd have 2

#

using delete you can make it delete itself when it goes out of scope

placid moon
#

Also gotta track things like the head and the tail

#

So you have to update those when the list is changed

plain scarab
kindred jasper
#

sure

#

it was a more verbose term

#

but yes

placid moon
#

Skipping over manual allocation is not a great idea imo

#

And this is a good time to learn it

plain scarab
kindred jasper
#

yes but you need to build a foundation first

#

you cannot learn lists without learning basic memory concepts

placid moon
onyx fulcrum
#

So I need for every list a head a tail with a nullptr and for example a n that moves between the nodes?

plain scarab
#

The foundation is to always use smart pointers until you literally can't

placid moon
#

I'm assuming they're going to keep coding, so knowing manual memory allocation is good

plain scarab
#

I'm not say it isn't good, just not needed for this

placid moon
kindred jasper
placid moon
#

Yeah that's true

#

I'm just suggesting they use this to learn it

kindred jasper
#

it is a good time to learn it

onyx fulcrum
#

and how do I learn memory allocation?

plain scarab
#

There really isn't much to learn here tho

kindred jasper
#

but its worth it

#

it will take them half an hour

onyx fulcrum
#

does someone has a good yt video on that topic?

kindred jasper
#

Not sure if this is a great one but learncpp is generally good

onyx fulcrum
#

okay ty

plain scarab
#

Learn about it sure, then just never use it and use smart pointers instead

onyx fulcrum
#

I mean I am right now studying in University and need to learn the basic stuff and if they dont allow me to use smart pointers I will need to have some knowledge about the normal ones and the memory allocation

#

but thanks for the advice if I`ll be alowed to programm freely I will come back on this:D

plain scarab
#

If they don't let you use smart pointers then there is something seriously wrong with the staff there

onyx fulcrum
#

I mean the Course is named Basics of higher programm languages

#

maybe thats "too advanced" idk

kindred jasper
#

no its the opposite

#

smart pointers are higher level than manual memory management

#

or more abstracted is the right term i guess

onyx fulcrum
#

true

onyx fulcrum
#

I did it :D@kindred jasper

#
#include <iostream>
#include "Klasse.h"
using namespace std;
List::List(){
head = NULL;
current = NULL;
temp = NULL;
}
void List::addNode(int addData){
    nodePtr n = new node;
    n->next = NULL;
    n->data = addData;
    if(head != NULL)
    {
        current = head;
        while(current->next != NULL)
        {
            current = current->next;
        }
        current->next = n;
    }
    else{
        head = n;
    }
}
void List::deleteNode(int delData)
{
    nodePtr delPtr = NULL;
    temp = head;
    current = head;
    while (current != NULL && current->data != delData)
    {
        temp = current;
        current = current->next;
    }
    if(current == NULL)
    {
        cout << delData << " Die Zahl konnte nicht gefunden werden.\n";
        delete delPtr;
    }
    else{
        delPtr = current;
        current = current->next;
        temp = current;
        if(delPtr == head)
        {
            head = head->next;
            temp = NULL;
        }
        delete delPtr;
        cout << "Die Zahl " << delData << " wurde geloescht.\n";
    }
}
void List::PrintList()
{
    current = head;
    while (current != NULL)
    {
        cout << current->data << ", ";
        current = current->next;
    }
    
}```
#

and thats the header: ```cpp
#pragma once

class List{

private:
typedef struct node{
int data;
node* next;
}* nodePtr;

nodePtr current;
nodePtr temp;
nodePtr head;
int number;

public:
List();
void addNode(int addData);
void deleteNode(int delData);
void PrintList();
};```

#

Tmrw I will implement it into my programm

kindred jasper
#

there ya go @onyx fulcrum great work

#

just so you know, some nice things you can do

#
if(current == NULL)

can become:

if(!current)
#

and if you are doing pointer comparisons, use nullptr instead of null

onyx fulcrum
#

okay

#

thank you

#

for your help

#

and for the advices

dull jettyBOT
#

@onyx fulcrum Has your question been resolved? If so, type !solved :)

onyx fulcrum
#

@kindred jasper I just made a function to check the highest data but when I execute my code it stops at the point the function gets involved. I added everything to the header and the .cpp class Thats the class.cpp: ```cpp
#include <iostream>
#include "Klasse.h"
using namespace std;
List::List(){
head = NULL;
current = NULL;
temp = NULL;
}
void List::addNode(int addData){
nodePtr n = new node;
n->next = NULL;
n->data = addData;
if(head != NULL)
{
current = head;
while(current->next != NULL)
{
current = current->next;
}
current->next = n;
}
else{
head = n;
}
}
void List::deleteNode(int delData)
{
nodePtr delPtr = NULL;
temp = head;
current = head;
while (current != NULL && current->data != delData)
{
temp = current;
current = current->next;
}
if(current == NULL)
{
cout << delData << " The number couldnt get found.\n";
delete delPtr;
}
else{
delPtr = current;
current = current->next;
temp = current;
if(delPtr == head)
{
head = head->next;
temp = NULL;
}
delete delPtr;
cout << "The number " << delData << " got deleted.\n";
}
}
void List::checkGreatest(int maxData)
{
current = head;
while(current->next != NULL)
{
if(maxData > current->data)
{
current = current->next;
}
else if(maxData < current->data)
{
maxData = current->data;
}

}
}
void List::PrintList()
{
current = head;
while (current != NULL)
{
cout << current->data << ", ";
current = current->next;
}

}``` Do u know what could be the problem?

#

Thats the main: ```cpp
#include <iostream>
#include <cmath>
#include "Klasse.h"
using namespace std;
int counter;
int number;
int placeholder = 1;
List lol;
int funktion(){

while(number != 1)
{
if(number % 2 == 0)
{
number = number/2;
}
else
{
number = 3 * number + 1;
}
lol.addNode(number);
counter = counter + 1;
}
return counter;
}

int main(){
cout << "Which number do you wanna check? " <<endl;
cin >> number;

funktion();
cout << "The number " << number << " is after " << counter << " steps proofed not perfect." << endl;
cout << "The highest number reached is: ";
lol.checkGreatest(placeholder);
cout << endl;
cout << "The algorithm reached in the process that many numbers: ";
lol.PrintList();

cout << endl;

system("pause");
return 0;

}```

#

and the header: ```cpp
#pragma once

class List{

private:
typedef struct node{
int data;
node* next;
}* nodePtr;

nodePtr current;
nodePtr temp;
nodePtr head;
int number;

public:
List();
void addNode(int addData);
void deleteNode(int delData);
void checkGreatest(int maxData);
void PrintList();
};```

#

thank u in advance 😄

dull jettyBOT
#

@onyx fulcrum Has your question been resolved? If so, type !solved :)

plain scarab
#

@onyx fulcrum what is the issue?

#

There are a few issues, firstly don't use global variables that aren't constant.
Check greatest doesn't look quite right, you can get in an endless loop here easily. I am guessing this is the issue you are talking about.
Use nullptr not NULL. nullptr is the more C++ way
Don't use using namespace std;

onyx fulcrum
#

Ah okay ty

#

I'll try

dull jettyBOT
#

@onyx fulcrum Has your question been resolved? If so, type !solved :)