#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node* next,*prev;
}node;
struct node* head;
void insertAtEnd(int x)
{
struct node* temp=NULL;
temp=(node*)malloc(sizeof(struct node));
temp->data=x;
if(head==NULL)
{
temp->next=NULL;
head=temp;
temp->prev=head;
}
else
{
struct node* temp2=head;
while(temp2->next!=NULL)
{
temp2=temp2->next;
}
temp2->next=temp;
temp->prev=temp2->next;
temp->next=NULL;
}
}
void print()
{
struct node* temp=head;
while(temp!=NULL)
{
printf("%d",temp->data);
temp=temp->next;
}
}
void insertAtAnyPosition(int x,int pos)
{
struct node* temp=NULL;
temp=(node*)malloc(sizeof(struct node));
temp->data=x;
if(head==NULL)
{
temp->next=NULL;
head=temp;
temp->prev=head;
}
else
{
struct node* temp2=head;
struct node* temp3=NULL;
int i=0;
for(i=0;i<pos-1;i++)
{
temp2=temp2->next;
temp3=temp2->next;
}
temp->next=temp3;
temp3->prev=temp->next;
temp2->next=temp;
temp->prev=temp2;
}
printf(" After Insertion at any Position ");
print();
}
void main()
{
insertAtEnd(2);
insertAtEnd(3);
insertAtEnd(9);
insertAtEnd(8);
print();
insertAtAnyPosition(23,3);
getch();
}This code snippet is for Doubly Linked List Insert at Any Position.
