using
System;
public
class
GFG{
class
node
{
public
int
item;
public
node nxt;
public
node(
int
item, node t)
{
this
.item = item;
this
.nxt = t;
}
};
static
node insertAtFirst(node listHead, node x)
{
x.nxt = listHead;
listHead =
null
;
listHead = x;
return
listHead;
}
static
node insertAtEnd(node listHead, node x)
{
if
(listHead.nxt ==
null
)
{
listHead.nxt = x;
return
listHead;
}
listHead.nxt = insertAtEnd(listHead.nxt, x);
return
listHead;
}
static
node insertInBetween(node temp, node x,
int
pos)
{
if
(pos == 2)
{
if
(temp !=
null
)
{
x.nxt = temp.nxt;
temp.nxt = x;
}
return
temp;
}
if
(temp ==
null
)
{
return
temp;
}
temp.nxt = insertInBetween(temp.nxt, x, --pos);
return
temp;
}
static
void
print(node head)
{
if
(head ==
null
)
{
return
;
}
Console.Write(head.item +
" "
);
print(head.nxt);
}
public
static
void
Main(String[] args)
{
node head =
new
node(1,
null
);
for
(
int
i = 2; i < 16; i++)
insertAtEnd(head,
new
node(i,
null
));
head = insertInBetween(head,
new
node(100,
null
), 4);
head = insertInBetween(head,
new
node(101,
null
), 200);
head = insertAtFirst(head,
new
node(100,
null
));
head = insertAtEnd(head,
new
node(100,
null
));
print(head);
}
}