Doubly Linklist:- In Singly Linklist we have to move only in forward direction. we don't move in backward direction in linklist but Using Doubly Linklist we can iterate in both forward and backward direction. The Below code will shown you how to create the doubly linklist.
In case of Doubly Linklist we have to use *prev pointer to move in backward direction and *next pointer which help us to move in forward direction.
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
int info;
struct node *prev;
struct node *next;
};
typedef struct node NODE;
NODE *start=NULL,*node,*p;
void create();
void display();
void rev_dis();
void insert_beg();
void insert_mid();
void insert_end();
void main()
{
int a;
clrscr();
while(1)
{
printf("\n\t1:=Create LinkList");
printf("\n\t2:=Reverse Display");
printf("\n\t3:=Display an Element");
printf("\n\t4:=Exit");
printf("\n\t5:=Insert an Element at Middle");
printf("\n\t6:=Insert an Element at Beginning");
printf("\n\t7:=Insert an Element at End");
printf("\n\n\tEnter your choice:=");
scanf("%d",&a);
switch(a)
{
case 1:
create();
break;
case 2:
rev_dis();
break;
case 3:
display();
break;
case 4:
exit(1);
case 5:
insert_mid();
break;
case 6:
insert_beg();
break;
case 7:
insert_end();
break;
default :
printf("\n\n\tyou have entered wrong choice");
}
}
getch();
}
void create()
{
node=(NODE *)malloc(sizeof(NODE));
printf("\n\tEnter the Element:=");
scanf("%d",&node->info);
if(start==NULL)
{
node->prev=NULL;
node->next=NULL;
start=node;
p=node;
}
else
{
node->prev=p;
node->next=NULL;
p->next=node;
p=node;
}
}
void display()
{
node=start;
while(node!=NULL)
{
printf("%d \n",node->info);
node=node->next;
}
}
void rev_dis()
{
NODE *temp;
temp=p;
node=temp;
while(node!=NULL)
{
printf("%d \n",node->info);
node=node->prev;
}
}
void insert_beg()
{
node=(NODE *)malloc(sizeof(NODE));
printf("\n\tEnter the Element:=");
scanf("%d",&node->info);
node->prev=NULL;
node->next=start;
start->prev=node;
start=node;
}
void insert_mid()
{
int flag=0;
int num;
NODE *t;
t=start;
printf("\n\tEnter the location of a No. u want insert after:=");
scanf("%d",&num);
while(t!=NULL)
{
if(num==t->info)
{
flag=1;
node=(NODE *)malloc(sizeof(NODE));
printf("\n\tEnter the Element:=");
scanf("%d",&node->info);
node->next=t->next;
node->next->prev=node;
node->prev=t;
t->next=node;
break;
}
else
{
t=t->next;
}
}
if(flag==0)
printf("\n\tElement Not Inside the Linklist \n");
}
void insert_end()
{
node=(NODE *)malloc(sizeof(NODE));
printf("\n\tEnter the Element:=");
scanf("%d",&node->info);
node->next=NULL;
node->prev=p;
p->next=node;
p=node;
}
0 Comment(s)