lundi 25 avril 2022

C++ Constructor behaving strangely in my Linkedlist program

Today I was taught Linked list in class and I wanted to implement it on my own.

Here's the part of the code that I wrote. Note that traverseLL traverses the Linked list and insertAtEnd inserts a new node at the end of the linked list.

I believe I can implement Linked list logic / methods / functions on my own. But my question is, inside insertAtEnd function when I create a newNode with the parameters - my data to be inserted, and nullptr (because inserting at the end), It inserts garbage values (or memory addresses maybe) in my node, ignoring the data passed to the constructor.

using namespace std;
#define NL '\n'

class Node {
    public:
        int data;
        Node* next;

    Node (int data, Node* nextPtr=nullptr) {
        data = data;
        next = nextPtr;
    }
};


void insertAtEnd(Node* &head, int data) {

    Node* newNode = new Node(data, nullptr);  // <---- Issue in this line
    // When I do as above, my linkedlist nodes always store garbage values and not the data being passed.

    // However, when I un-comment the below line, I get the correct output.
    // newNode->data = data;

    if (head == nullptr)
        head = newNode;
    
    else {
        Node* temp = head;

        while (temp->next != nullptr)
            temp = temp->next;

        temp->next = newNode;
    }
}


void traverseLL(Node* head) {
    if (head == nullptr)
        return;

    while (head->next) {
        cout << head->data << " -> ";
        head = head->next;
    }
    cout << head->data << NL;
}




int main() {

    Node* head = nullptr;

    insertAtEnd(head, 10);
    insertAtEnd(head, 20);
    insertAtEnd(head, 30);
    
    traverseLL(head);

    return 0;
}

For example, the output for the above code when keeping newNode->data = data line commented, is :

16259544 -> 16258392 -> 16258392

But when I un-comment that line, my output becomes, which is intended:

10 -> 20 -> 30

Why is this happening? Even though I've defined my constructor, why is it not working?

Aucun commentaire:

Enregistrer un commentaire