Ques:linked list -k append
code
#include<bits/stdc++.h>
using namespace std;
class node{
public:
int data;
node* next;
node(int d){
data=d;
next=NULL;
}
};
void print(node* head)
{
nodetemp=head;
while(temp!=NULL)
{
cout<data<<" ";
temp=temp->next;
}
}
void insert(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 append(node *&head,int k)
{
int ct;
node *h=head;
node *h2=head;
while(h!=NULL)
{
ct++;
h=h->next;
}
h=head;
node p=head;
node m;
int jana=ct-k-1;
for(int i=1;i<=jana;i++)
{
p=p->next;
}
m=p->next;
p->next=NULL;
head=m;
while(true)
{
if(m->next==NULL)
{
m->next=h2;
break;
}
m=m->next;
}
}
int main()
{
node *head=NULL;
int n;
cin>>n;
while(nā)
{
int d;
cin>>d;
insert(head,d);
}
int k;
cin>>k;
append(head,k);
print(head);
}