WRONG OUTPUT of the code

#include <bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node *next;
//constructor
node(int d){
d=data;
next=NULL;
}
};
//Insertion at head
void Insertathead(node *&head,int data){
node *n=new node(data);
n->next=head;
head=n;
}
//Print the linked list
void print(node *head){
node temp=head;
while(temp !=NULL){
cout<data<<"->";
temp=temp->next;
}
}
//Length of the linked list
int length(node
head){
node *temp=head;
int len=0;
while(temp !=NULL){
len++;
}
return len;
}
//Insertion at the tail
void Insertattail(node *&head,int data){
node n=new node(data);
if(head==NULL){
head=new node(data);
return;
}
node
temp=head;
while(temp->next !=NULL)
temp=temp->next;
temp->next=n;
}

//Insertion in the middle
void Insertioninthemiddle(node & head,int data,int p){
if(head==NULL || p==0)
Insertathead(head,data);
else if(p>length(head))
Insertattail(head,data);
else{
int jump=1;
node
temp=head;
while(jump<=p-1){
temp=temp->next;
jump++;
}
node*n=new node(data);
n->next=temp->next;
temp->next=n;
}

}
int main() {
node *head=NULL;
Insertathead(head,2);
Insertathead(head,3);
Insertathead(head,4);
Insertathead(head,5);
print(head);
}

@Detoxo_16
Problem in your constructor.
Change d=data to data =d;

still there’s problem in insert in the middle function

@Detoxo_16
I believe your indexing is off by 1. In some places in your code, you are following 1 based indexing and in some instances , your code follows 0 based indexing. Dry run it for any input and follow your indexing. If your problem is still not solved , share your updated code so I can help you out.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.