DS - Module - II - DLL
Implementation
of Doubly Linked List
#include<stdio.h>
#include<stdlib.h>
void create();
void display();
void insert_at_begin();
void insert_at_end();
void insert_at_pos();
void delete_at_begin();
void delete_at_end();
void delete_at_pos();
void reverse();
struct node
{
int data;
struct node *prev,*next;
}*new,*head,*temp,*d;
int i,value,p;
char ch;
void main()
{
int n;
printf("\n 1.create\n 2.display\n 3.insert_at_begin\n
4.insert_at_end\n 5.insert_at_pos\n 6.delete_at_begin\n 7.delete_at_end\n
8.delete_at_pos\n 9.reverse");
while(1)
{
printf("enter your choice:");
scanf("%d",&n);
switch(n)
{
case 1:create();
break;
case 2:display();
break;
case 3:insert_at_begin();
break;
case 4:insert_at_end();
break;
case 5:insert_at_pos();
break;
case 6:delete_at_begin();
break;
case 7:delete_at_end();
break;
case 8:delete_at_pos();
break;
case 9:reverse();
break;
default:exit(1);
}
}
}
void create()
{
do
{
new=(struct node *)malloc(sizeof(struct node));
printf("enter a value");
scanf("%d",&value);
new->prev=NULL;
new->data=value;
new->next=NULL;
if(head==NULL)
{
head=new;
temp=new;
}
else
{
new->prev=temp;
temp->next=new;
temp=temp->next;
}
printf("\n do u want add one more node(y/n)");
scanf("%c",&ch);
}while(ch=='y');
}
void display()
{
temp=head;
while(temp!=NULL)
{
printf("%d<->",temp->data);
temp=temp->next;
}
}
void insert_at_begin()
{
new=(struct node *)malloc(sizeof(struct node));
printf("enter a value");
scanf("%d",&value);
new->data=value;
new->next=head;
head->prev=new;
head=new;
}
void insert_at_end()
{
new=(struct node *)malloc(sizeof(struct node));
printf("enter a value");
scanf("%d",&value);
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
new->prev=temp;
temp->next=new;
new->data=value;new->next=NULL;
}
void insert_at_pos()
{
new=(struct node *)malloc(sizeof(struct node));
printf("enter a value");
scanf("%d",&value);
printf("enter a position");
scanf("%d",&p);
temp=head;
for(i=1;i<p-1;i++)
{
temp=temp->next;
}
new->next=temp->next;
temp->next->prev=new;
temp->next=new;
new->prev=temp;
new->data=value;
}
void delete_at_begin()
{
temp=head;
printf("\n
deleted node is%d->",temp->data);
head=head->next;
temp->next=NULL;
head->prev=NULL;
free(temp);
}
void delete_at_end()
{
temp=head;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
d=temp->next;
temp->next=NULL;
d->prev=NULL;
printf("\n
deleted node is %d",d->data);
free(d);
}
void delete_at_pos()
{
temp=head;
printf("enter
a position");
scanf("%d",&p);
for(i=1;i<p-1;i++)
{
temp=temp->next;
}
d=temp->next;
temp->next=d->next;
d->next->prev=temp;
printf("\n
deleted node is %d",temp->data);
d->prev=NULL;
d->next=NULL;
free(d);
}
void reverse()
{
temp=head;
while(temp->next!=NULL)
{temp=temp->next;
}
while(temp->prev!=NULL)
{
printf("%d<->",temp->data);
temp=temp->prev;
}
printf("%d<->",temp->data);
}
Output:-
1.create
2.display
3.insert_at_begin
4.insert_at_end
5.insert_at_pos
6.delete_at_begin
7.delete_at_end
8.delete_at_pos
9.reverseenter your choice:1
enter a value13
do u want add one more node(y/n)enter your
choice:1
enter a value26
do u want add one more node(y/n)enter your
choice:1
enter a value39
do u want add one more node(y/n)enter your
choice:2
13<->26<->39<->enter
your choice:3
enter a value52
enter your choice:2
52<->13<->26<->39<->enter
your choice:4
enter a value65
enter your choice:2
52<->13<->26<->39<->65<->enter
your choice:5
enter a value78
enter a position4
enter your choice:2
52<->13<->26<->78<->39<->65<->enter
your choice:6
deleted node is52->enter your choice:2
13<->26<->78<->39<->65<->enter
your choice:7
deleted node is 65enter your choice:2
13<->26<->78<->39<->enter
your choice:8
enter a position3
deleted node is 26enter your choice:9
39<->26<->13<->enter
your choice:10
Comments
Post a Comment