link to ques: https://hack.codingblocks.com/contests/c/457/465
#include
using namespace std;
class node{
public:
int data;
node * next;
node(int d)
{
data=d;
next=NULL;
}
};
void print (node*head)
{
while(head!=NULL)
{
cout<data<<" “;
head=head->next;
}
cout<<”\n";
}
void insertathead(node *&head,int data)
{
node * n= new node(data);
n->next=head;
head=n;
}
void insertattail(node *&head,int data)
{
if(head==NULL){
head= new node(data);
return;
}
node * temp=head;
while(temp->next!=NULL)
temp=temp->next;
temp->next=new node (data);
return;
}
void insertlist(node *& head,int s)
{
int data,i=0;
cin>>data;
while(i<s)
{
insertattail(head,data);
cin>>data;
i++;
}
}
ostream & operator<<(ostream & os,node *&head)
{
print(head);
return os;
}
void deletion(node *&head,int key)
{
if(head==NULL)
return;
node * temp = head;
node * pre = NULL;
while(temp !=NULL&& temp->data!=key)
{
pre=temp;
temp=temp->next;
}
if(temp==NULL)
return;
else if(temp==head)
head=head->next;
else
pre->next=temp->next;
delete temp;
}
int main()
{
int s,q,x;
cin>>s>>q;
node * head=NULL;
insertlist(head,s);
for (int i=0;i<q;i++)
{
cin>>x;
node * temp=head;
for(int j=0;j<x;j++)
{
temp=temp->next;
}
deletion(head,temp->data);
cout<<head;
}
return 0;
}