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 Delete at Any Position
Kaushik S
Dec 08
2015
Code
4
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;
struct
node* prev;
}node;
struct
node* head=NULL;
void
insert(
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
{
head->prev=temp;
temp->next=head;
head=temp;
}
}
void
print()
{
struct
node* temp=head;
while
(temp!=NULL)
{
printf(
"%d"
,temp->data);
temp=temp->next;
}
}
void
DeleteAnyValue(
int
x)
{
struct
node* temp=head;
if
(head==NULL)
{
printf(
"No records to delete"
);
}
else
if
(temp->data==x)
{
head=temp->next;
free(temp);
}
else
{
struct
node* temp2=NULL;
temp2=temp->next;
while
(temp->next->data!=x)
{
temp=temp->next;
temp2=temp->next;
}
temp->next=temp2->next;
temp2->prev=temp;
free(temp2);
}
printf(
" After Deletion "
);
print();
}
void
main()
{
insert(5);
insert(2);
insert(3);
insert(7);
print();
DeleteAnyValue(2);
getch();
}
C
Datastructures