TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Doubly Linked List Insert at Any Position
Kaushik S
Dec 08
2015
Code
13.1
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
#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();
}
C
Datastructures