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