struct node
{
int data;
node *prev;
node *next;
};
void insertAtEnd(int val)
{
node *temp = new node;
temp->data = val;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL)
{
head = temp;
}
else
{
node *p = head;
while (p->next != NULL)
{
p = p->next;
}
p->next = temp;
temp->prev = p;
}
}
void insertAtBegin(int val)
{
node *temp = new node;
temp->data = val;
temp->next = NULL;
temp->prev = NULL;
if (head == NULL)
{
head = temp;
}
else
{
node *p = head;
temp->next = p;
p->prev = temp;
head = temp;
}
}
void insertAtPos(int pos, int val)
{
int i = 0;
int len = getLen();
if (pos < 0 || pos > len - 1)
{
return;
}
node *p = head;
node *q = NULL;
while (i < pos && p->next != NULL)
{
q = p;
p = p->next;
i++;
}
node *temp = new node;
temp->data = val;
temp->next = NULL;
temp->prev = NULL;
q->next = temp;
temp->prev = q;
temp->next = p;
p->prev = temp;
}