0

I'm working with LinkedLists from custom node, I'm still struggling to understand some concepts. For simplicity functions are reduced

    class Node {
    public:
    T data;
    Node* next;
    Node(const T& copy){
    data = copy;}

    class T {
    string name;
    int age;
    T(string name, int age){
    T::name = name;
    T:age = age;}

    class LinkedList{

    private:
           Node* head;
    
    void insertAtFront(string name,int age){

    Node* newNode = new Node(name,age);
    head = newNode;
    
    /*
    T temp(name,age);
    Node newNode(temp);
    head = &newNode */
    
    ;
    }
     
    

I'm coming from java background, I know variables are treated differenly in c++ and java. I know c++ copies by value unless *, & is used. My misconceptions might probably occur because of the differences though, I couldn't solve it.

1-In insertAtFront function, implementations on the internet creates the node pointer dynamically(using new keyword). Can't we do it as the way between /* */ ?

2- I actually dont get the idea of why head is a pointer in the linked list. I've created linked lists in java. As long as the next value of the head is correct (so changing it in the correct way in c++) why should I make head a pointer ?

2
  • 3
    Are you aware of the concept of lifetimes? Node newNode(temp); will be destroyed once the variable goes out of scope, so storing a pointer to it will lead to a dangling pointer Commented Nov 16, 2022 at 11:57
  • 2
    In Java, the "next" value (i.e the link) is a reference to an object, not the object itself (all non-primitive values in Java are references, not objects). In C++ you need to make that indirection explicit, otherwise your nodes would be infinitely large, and you need to manage the lifetimes of objects. There are many Java things you need to unlearn when learning C++.
    – molbdnilo
    Commented Nov 16, 2022 at 12:29

1 Answer 1

0

If you use:

T temp(name,age);
Node newNode(temp);
head = &newNode;

the variable newNode is destroyed when it goes out of scope. Then head points to a destroyed variable. It might continue to hold the same node values for a very short time - probably until the next function call (you can't rely on this). Then that memory space will be reused for some other variable.

head continues pointing to the same memory address, and the data at that address has been overwritten for some other purpose, so you won't be able to read the node data there that you want to read.

Each time you use new it creates a nameless "variable" that doesn't get destroyed and reused until you use delete. Since the variable doesn't have a name, you have to use pointers to refer to it.

2- I actually dont get the idea of why head is a pointer in the linked list. I've created linked lists in java. As long as the next value of the head is correct (so changing it in the correct way in c++) why should I make head a pointer ?

In Java, every Node variable - every object variable - is actually a pointer. In C++, that's not true and you have to ask for pointers when you want them. When you write Node next; in C++ you are saying that the variable next has a real, bona-fide Node inside the variable. If you write class Node {Node next;} then you are saying every node has another node inside it - but that one has a node inside it too - but that one has a node inside it too - on to infinity - which is impossible, of course.

You could choose to use Node head;. That's a valid design choice, but it complicates things. It means the first node in every linked list is inside the list itself, and you can't change which node it is because it's not a pointer. If you wanted delete the first node, then you'd have to do it by copying the second node to the first node and then deleting the second node. And you'd have to figure out how you wanted to store an empty list.

1
  • head becomes an invalid pointer. It is not required that it continues to point to the same location once newNode is destroyed
    – Caleth
    Commented Nov 16, 2022 at 12:56

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.