This code i have implemented is working fine and no error and when i am taking input from user it gives me an error segmentation fault, i don't know why This is my First code and working fine
#include<iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
};
void display(Node *n){
if(n!=NULL){
Node *tmp;
tmp=n;
while(tmp!=NULL){
cout<<"Value : "<<tmp->data<<endl;
tmp=tmp->next;
}
}
}
Node *append(Node *n,int d){
Node *head;
Node *tmp;
if(n==NULL){
Node *node = new Node();
head = node;
head->data=d;
head->next=NULL;
tmp=head;
return tmp;
}
if(tmp!=NULL){
Node *node = new Node();
node->data=d;
tmp->next=node;
node->next=NULL;
tmp = tmp->next;
}
return head;
}
int main(){
Node *head=NULL;
Node *h=append(head,10);
append(h,20);
append(h,30);
append(h,40);
append(h,50);
display(h);
}
This is my second code when taking input from user it gives me error segmentation fault, i don't know what wrong have i done
#include using namespace std;
class Node{
public:
int data;
Node *next;
};
void display(Node *n){
if(n!=NULL){
Node *tmp;
tmp=n;
while(tmp!=NULL){
cout<<"Value : "<<tmp->data<<endl;
tmp=tmp->next;
}
}
}
Node *append(Node *n,int d){
Node *head;
Node *tmp;
if(n==NULL){
Node *node = new Node();
head = node;
head->data=d;
head->next=NULL;
tmp=head;
return head;
}
if(tmp!=NULL){
Node *node = new Node();
node->data=d;
tmp->next=node;
node->next=NULL;
tmp = tmp->next;
}
return head;
}
int main(){
Node *head=NULL;
cout<<"Enter the value :"<<endl;
int val;
cin>>val;
Node *h=append(head,val);
int choice=0;
cout<<"Do you want to continue? (0 , 1) Yes = 1 and No = 0"<<endl;
cin>>choice;
while(choice!=0){
cout<<"Enter a value :"<<endl;
cin>>val;
append(h,val);
cout<<"Do you want to continue? (0 , 1) "<<endl;
cin>>choice;
}
display(h);
}