-3

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.

greybeard
  • 2,249
  • 8
  • 30
  • 66
  • 2
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – 463035818_is_not_an_ai Aug 29 '23 at 18:08
  • 3
    You've asked a question before, but haven't given feedback to the answers you got. – trincot Aug 29 '23 at 18:11
  • 3
    Unrelated: `#include` [loads the gun](https://stackoverflow.com/q/31816095/4581301). `using namespace std;` [takes the safety off](https://stackoverflow.com/q/1452721/4581301). It is now really easy to shoot yourself in the foot, so **don't do this**. Tell whoever you learned it from. If you got it off a website, consider taking what they teach with a grain of salt because they could be passing on even worse information. – user4581301 Aug 29 '23 at 18:11
  • 2
    I have answered this to their previous question, and here we are. Doesn't seem to register somehow... – trincot Aug 29 '23 at 18:13
  • I didn't even need to use a debugger to find the likely issue, just some [rubber duck debugging](https://en.wikipedia.org/wiki/Rubber_duck_debugging). – Some programmer dude Aug 29 '23 at 18:14
  • 2
    @SAMRAT SARKAR First of all there is no doubly linked list in the presented code.:) – Vlad from Moscow Aug 29 '23 at 18:51
  • @SAMRAT SARKAR And where is there the name value declared? – Vlad from Moscow Aug 29 '23 at 18:58
  • Note that the code presented does *not* attempt to swap `node`s: it tries to swap associated data. With `head` & `next` public, *aliases* could be all over the place, with uncontrollable results. – greybeard Aug 30 '23 at 04:38

1 Answers1

0

You have a small error. value should be data, because your node doesn't have value defined.

while(before!=nullptr)
{   
     node* after=before->next;
     int temp=before->data;
     before->data=after->data;
     after->data=temp;
     before=before->next->next;
}

It works for me after making this change.

As others have pointed out, I would highly recommend using a debugger to find these types of errors.

Finally, with doubly linked lists each node would need the ability to go forward and backwards. Yours only has "next", which means it's just a singly linked list.

greybeard
  • 2,249
  • 8
  • 30
  • 66
twalow
  • 405
  • 3
  • 6