0

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);
}
anand patel
  • 33
  • 1
  • 5
  • Does this answer your question? [What is a segmentation fault?](https://stackoverflow.com/questions/2346806/what-is-a-segmentation-fault) – paisanco Sep 27 '20 at 16:04

1 Answers1

0

It seems to me there are a lot of issues with your code. I have modified your code, you can find it below.

  • Study this code, analyse the differences in this code and your code.
  • Brush up your basics with pointers.

I am sure you are going to learn a lot with this. If more help needed, feel free to comment. Happy learning.

#include <iostream>
using namespace std;

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

        Node(int d)         // Constructor
        {
            data = d;
            next = NULL;
        }
};

void display(Node *head){
    Node *tmp = head;
    cout<<"\nLinked List is :\n";
    while(tmp!=NULL)
    {
        cout<<tmp->data<<" -> ";
        tmp=tmp->next;
    }
    cout<<"NULL\n";
}


Node* append(Node *head,int d)
{
    
    // If linked list is empty
    if(head == NULL)
    {
        head = new Node(d);
        return head;
    }

    // Else insert at last
    Node *tmp = head;
    while(tmp->next)
        tmp = tmp->next;
    // Now tmp points to the last element of the linked list, insert after it
    tmp->next = new Node(d);
    return head;
}

int main()
{
    Node *head=NULL;
    
    cout<<"Enter the value :"<<endl;
    int val;
    cin>>val;
    head = append(head,val);
    
    int choice=0;
    

    while(1)
    {
        cout<<"Do you want to continue? (0 , 1) Yes = 1 and No = 0"<<endl;
        cin>>choice;

        if(choice==0) break;
        if(choice!=1)
            cout<< "Please enter a valid choice\n";
        else 
        {
            cout<<"Enter a value :"<<endl;
            cin>>val;
            head = append(head,val);
        }    
    }
  
    display(head);
}
TheCoder
  • 51
  • 4