I am having problems with a swap pairs function. Here you have to swap two adjacent nodes but it's showing segmentation fault.
I have declared a before node * and after and then I iterated through the loop:
before will move two steps and after will move 1 step ahead the before node.
When before will reach null we will get the required list but it's not showing results.
Your advice would be very helpful;
#include<bits/stdc++.h>
using namespace std;
class node{
    public:
    int data;
    node *next;
    node(int d)
    {
        this->data=d;
        this->next=nullptr;
    }
};
class ll
{
    private:
    node *head=nullptr;
    node *tail=nullptr;
    public:
    void inserthead(int value)
    {
        node *newnode=new node(value);
        if(head==nullptr)
        {
            head=newnode;
            tail=newnode;
        }
        else{
            newnode->next=head;
            head=newnode;
        }
    }
    void inserttail(int value)
    {
        node *newnode=new node(value);
        if(tail==nullptr)
        {
            head=newnode;
            tail=newnode;
        }
        else{
            tail->next=newnode;
            tail=newnode;
        }
    }
    void print()
    {
        node *temp=head;
        while(temp!=nullptr)
        {
            cout<<endl<<temp->data<<endl;
            temp=temp->next;
        }
    }
    void swap_pair()
    {
        node *before=head;
        while(before!=nullptr)
        {   
            node* after=before->next;
            int temp=before->value;
            before->value=after->value;
            after->value=temp;
            before=before->next->next;
        }
    }
};  
int main()
{
    ll l1;
    l1.inserthead(2);
    l1.inserthead(1);
    l1.inserttail(3);
    l1.inserttail(4);
    l1.swap_pair();
    l1.print();
    return 0;
}
I am expecting output 2 1 4 3.
 
     
    