#include <stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
struct node* createnode(int val)
{
struct node
* p
=(struct node
*)malloc(sizeof(struct node
)); p->data=val;
p->next=NULL;
return p;
}
struct node* insertlinked(struct node* head,int pos,int num)
{
struct node* temp=head;
if(pos==0)
{
struct node* temp1=createnode(num);
temp1->next=head;
head=temp1;
return head;
}
while(pos>1 && temp!=NULL)
{
pos--;
temp=temp->next;
}
if(temp==NULL)
{
return NULL;
}
struct node* temp2=createnode(num);
temp2->next=temp->next;
temp->next=temp2;
return head;
}
struct node* dellinked(struct node* head,int pos)
{
struct node* temp=head;
if(pos==0)
{
head=head->next;
return head;
}
while(pos>1 && temp->next!=NULL)
{
pos--;
temp=temp->next;
}
if(temp->next==NULL)
{
return NULL;
}
struct node* temp1=temp->next;
temp->next=temp->next->next;
return head;
}
void printlinked(struct node* head)
{
if(head==NULL)
return;
while(head!=NULL)
{
head=head->next;
}
}
int main()
{
int pos,val,pos1;
struct node* head=createnode(10);
head->next=createnode(20);
head->next->next=createnode(30);
head->next->next->next=createnode(40);
printf("enter the pos and value to be inserted\n"); scanf("%d %d",&pos
,&val
); printf("enter the value to be deleted\n"); struct node* temp=insertlinked(head,pos,val);
printlinked(temp);
struct node* temp1=dellinked(temp,pos1);
printlinked(temp1);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlPHN0ZGxpYi5oPgpzdHJ1Y3Qgbm9kZXsgCglpbnQgZGF0YTsKCXN0cnVjdCBub2RlKiBuZXh0Owp9OwpzdHJ1Y3Qgbm9kZSogY3JlYXRlbm9kZShpbnQgdmFsKQp7CglzdHJ1Y3Qgbm9kZSogcD0oc3RydWN0IG5vZGUqKW1hbGxvYyhzaXplb2Yoc3RydWN0IG5vZGUpKTsKCXAtPmRhdGE9dmFsOwoJcC0+bmV4dD1OVUxMOwoJcmV0dXJuIHA7Cn0KCnN0cnVjdCBub2RlKiBpbnNlcnRsaW5rZWQoc3RydWN0IG5vZGUqI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