I am getting run time error in this code. plz resolve it

#include
using namespace std;
class node{
public:
int data;
nodenext;
node(int d)
{
data=d;
next=NULL;
}
};
void insertattail(node
&head,int data)
{
if(head==NULL)
{
head=new node(data);
return;
}
nodetail=head;
while(tail->next!=NULL)
{
tail=tail->next;
}
tail->next=new node(data);
return;
}
void print(node
head)
{
while(head!=NULL)
{
cout<data<<" ";
head=head->next;
}
cout<<endl;
}
int length(nodehead)
{
int len=0;
while(head!=NULL)
{
head=head->next;
len+=1;
}
return len;
}
void middle(node
&head,int pos)
{
if(head==NULL|| head->next==NULL)
{
return;
}
nodefast=head;
node
prev=head;
nodep=head;
int jump=1;
while(jump<=length(head)-pos)
{ prev=fast;
fast=fast->next;
jump+=1;
}
prev->next=NULL;
node
temp=fast;
int count=0;
while(temp->next!=NULL)
{ count++;
temp=temp->next;
}
temp->next=head;
print(fast);
//print(head);
//print(head);
}
int main() {
int n;
cin>>n;
int arr[n];
node*head=NULL;
for(int i=0;i<n;i++)
{
cin>>arr[i];
insertattail(head,arr[i]);
}
int pos;
cin>>pos;
middle(head,pos);
return 0;
}

@Kunalgoyal please share your code by saving it on ide.codingblocks.com

@Kunalgoyal you need to take care of cases where k > n. You can do so by adding this line before calling the function.
image
Please mark your doubt as resolved.

one test case is still showing run time error.

@Kunalgoyal handle the case of k = 0 as well.

k means position . na

if pos=0 then return head. is it right

@Kunalgoyal yes if k = 0, no need to call the function, print head as it is.

okay. got it thanks.

@Kunalgoyal please mark the doubt as resolved :slight_smile: