-2

Output is some trash value. When I add the print() funtion inside the scan it is giving the correct answer. That means my linked list is getting destroyed after the scan function.

Tags

#include <bits/stdc++.h>

using namespace std;

class node{
    public:
        int data;
        node *next;
        node(int data){
            this->data = data;
            next  = NULL;
        }
};

void print(node *head){
    node *temp = head;
    while(temp != NULL){
        cout << temp->data << " ";
        temp = temp->next;
    }
}

void scanLl(node *head){
    int data;
    node *temp;
    head = NULL;
    cin >> data;
    while(data != -1){
        node *ptr = new node(data);
        if(head == NULL){
            head = ptr;
            temp = head;
        }
        else{
            temp->next = ptr;
            temp = temp->next;
        }
        cin >> data;
    }
}

int main(){
    node *head;
    scanLl(head);
    print(head);
}

Output 17744 0 for input 1 2 3 4 5

Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303
  • 3
    If you used a debugger, you would be able to tell exactly where things go sideways. – Scott Hunter May 26 '22 at 19:28
  • 3
    `node *head;` creates an uninitialized `node*`. `scanLl(head);` accepts `head` as an argument by value. So things like `head = NULL;` and all the changes to `head` inside the body of `scanLl` are acting on a copy of the variable from `main`. That means that when you return from `scanLl` and go to `print(head);`, you're still using whatever uninitialized thing you had before. – Nathan Pierson May 26 '22 at 19:28
  • 2
    Please [don't include that header file](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Any resource (book, tutorial, even teacher) that teaches that header file should be considered as not very good. – Some programmer dude May 26 '22 at 19:30
  • This would have been a harder thing to hit with a proper C++ list class. – sweenish May 26 '22 at 19:34
  • @NathanPierson Thanks I moved the head inside the function and returned it. It worked. – Revanth Pershad May 26 '22 at 19:38
  • 1
    @Someprogrammerdude `#include using namespace std; class node{ public: int data;`-- Now the programmer has confused the whole thing if they now want to use [std::data](https://en.cppreference.com/w/cpp/iterator/data). – PaulMcKenzie May 26 '22 at 19:38

1 Answers1

1

The function scanLl accepts the pointer to the head node by value

void scanLl(node *head){

and

node *head;
scanLl(head);

It means that the function deals with a copy of the value of the original pointer used as an argument. Changing the copy within the function does not reflect on the value of the original pointer.

At least the function should accept the pointer by reference

void scanLl(node * &head){

Though taking into account that the value of the function argument is not used then it is better to declare the function like

node * scanLl();

and call it like

node *head = scanLl();
Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303