Why my program is not giving any output ??
#include
using namespace std;
class node
{
public:
int data;
node *next;
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 buildlist(node *head)
{
int data;
cin>>data;
while (data!=-1)
{
insertattail(head,data);
cin>>data;
}
}
node *kfromlast(node *head,int k)
{
node *fast=head;
node *slow=head;
for (int i=0;i<k;i++)
{
fast=fast->next;
}
while (fast!=NULL)
{
fast=fast->next;
slow=slow->next;
}
return slow;
}
int main()
{
node *head=NULL;
buildlist(head);
int k;
cin>>k;
head=kfromlast(head,k);
cout<data;
return 0;
}