Doubt in the code

#include
using namespace std;
class node{
public:
int data;
node* next;

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

};
void insertAtTail(node*&head,int data){

if(head==NULL){
    head = new node(data);
    return;
}
node*tail = head;
while(tail->next!=NULL){
    tail = tail->next;
}
tail->next = new node(data);
return;

}
void kappend(node &head,int k,int n){
node
append=head;
node *tail=head;
int p=1;
while(p<=(n-k-1)){
append=append->next;
p++;
}
while(tail->next!=NULL){
tail=tail->next;
}

tail->next=head;
head=append->next;
append->next=NULL;

}
void buildlist(node*&head,int n){
int data,count=0;
cin>>data;
while(count<n){
insertAtTail(head,data);
cin>>data;
count++;
}
}
void print(nodehead){
//node
temp = head;

while(head!=NULL){
    cout<<head->data<<"->";
    head = head->next;
}
cout<<endl;

}
ostream& operator << (ostream& os,nodehead){
print(head);
return os;
}
int main() {
node
head = NULL;

int n,k;
cin>>n;
buildlist(head,n);
cin>>k;
kappend(head,k,n);
cout<<head;
//cout<<"Hello World!";

}

Hello @priyeshant1104,

  1. The output format is wrong.
  2. Logic is not working correctly.
  3. Not handling the case when k>=N

Example:
7
1 2 2 1 8 5 6
8
Expected Output:
6 1 2 2 1 8 5
Your Output:
1 2 2 1 8 5 6

Observation:
The required output is the same as that of k=1.
Similarly,
for k=7 the output is the same as that of k=0.
for k=9 the output is the same as that of k=2.

Solution:
k = k mod n;
this would limit the value of k within the range [0,n-1]

Refer to the following code:

Hope, this would help.
Give a like if you are satisfied.

Thanks a lot it worked!