I am getting output as -> 1 1 2 3 4 5, for input-> 1 2 3 4 5 -1 , this code, I don't know why

#include<bits/stdc++.h>
using namespace std;

class node{
public:
int data;
node* next;

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

};

//calculating length of linked list
int length(node* head)
{
int len=0;
while(head!=NULL)
{
head=head->next;
len++;
}
return len;
}

void insertattail(node* &head,int d)
{
if(head==NULL)
head=new node(d);

node* tail=head;
while(tail->next!=NULL)
    tail=tail->next;
tail->next=new node(d);
return;

}

//this is called passing by value of head pointer
void print(node* head)
{
node* temp=head;
while(temp!=NULL)
{
cout<data<<" ";
temp=temp->next;
}
}

void buildlist(node* &head)
{
int d;
cin>>d;

while(d!=-1)
{
    insertattail(head,d);
    cin>>d;
}

}
int main()
{
node* head=NULL;
buildlist(head);

print(head);


return 0;

}

Please save your code on ide.codingblocks.com and then share its link.

Check now. Just a return has been added in line 33.

Thanks pratyush, it works