A doubly linked list is a linked
data structure that consists of a set of sequentially linked records called
nodes. Each node contains two fields, called links, that are references to the
previous and to the next node in the sequence of nodes. The beginning and
ending nodes' previous and next links. It can be conceptualized as two singly
linked lists formed from the same data items, but in opposite sequential orders.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct DLL
{
int
n;
struct
DLL *next;
struct
DLL *prev;
};
struct
DLL *head,*last;
void
add()
{
struct
DLL *temp,*pr;
clrscr();
temp=(struct DLL *)malloc(sizeof(struct
DLL));
printf("\n\tEnter a Value : ");
scanf("%d",&temp->n);
if(head==NULL)
{
head=temp;
temp->next=temp->prev=NULL;
}
else
{
for(pr=head;pr->next!=NULL;pr=pr->next);
pr->next=temp;
temp->next=NULL;
temp->prev=pr;
last=temp;
}
}
void
dispF()
{
struct
DLL *pr;
printf("\n\tInput
Order : ");
for(pr=head;pr!=NULL;pr=pr->next)
{
printf("%d ",pr->n);
}
getch();
}
void
dispB()
{
struct
DLL *pr;
printf("\n\tBackward
Order : ");
for(pr=last;pr!=NULL;pr=pr->prev)
{
printf("%d ",pr->n);
}
getch();
}
void del ()
{
int
num;
struct
DLL *pr,*cur;
printf("\tEnter
data to be deleted:");
scanf("%d",&num);
for(pr=head;pr!=NULL;pr=pr->next)
{
if(num==pr->n)
break;
else
cur=pr;
}
if(pr==head)
{
head=head->next;
head->prev=NULL;
}
else
if(pr==NULL)
printf("\nNumber
Not found");
else
{
cur->next=pr->next;
pr->next->prev=cur->prev;
last=cur;
}
free(pr);
}
void
insm()
{
struct DLL *temp,*cur,*pr;
int i=0,pos;
printf("\n\tEnter Position :
");
scanf("%d",&pos);
temp=(struct
DLL *)malloc(sizeof(struct DLL));
printf("\n\tEnter no to Insert : ");
scanf("%d",&temp->n);
for(cur=head;cur!=NULL;cur=cur->next)
{
i++;
if(pos==i)
{
break;
}
else
{
pr=cur;
}
}
if(cur!=NULL)
{
cur->prev->next=temp;
temp->next=cur;
temp->prev=cur->prev;
cur->prev=temp;
}
else
{
printf("\n\tPosition
out of range : ");
}
}
void
insl()
{
struct
DLL *temp,*cur,*pr;
temp=(struct
DLL *)malloc(sizeof(struct DLL));
printf("\n\tEnter no to Insert : ");
scanf("%d",&temp->n);
last=temp;
for(cur=head;cur->next!=NULL;cur=cur->next);
cur->next=temp;
temp->next=NULL;
temp->prev=cur;
last=temp;
}
void
insf()
{
struct
DLL *temp,*cur,*pr;
temp=(struct
DLL *)malloc(sizeof(struct DLL));
printf("\n\tEnter no to Insert : ");
scanf("%d",&temp->n);
temp->prev=NULL;
temp->next=head;
head->prev=temp;
head=temp;
}
void
main()
{
int
c=0;
head=last=NULL;
clrscr();
while(c!=8)
{
printf("\n\t1.
Add Node ");
printf("\n\t2.
Display Foward");
printf("\n\t3.
Display Backward");
printf("\n\t4.
Delete");
printf("\n\t5. Insert
First");
printf("\n\t6.
Insert Middle");
printf("\n\t7. Insert
Last");
printf("\n\t8. Exit");
printf("\n\tEnter
a choice : ");
scanf("%d",&c);
switch(c)
{
case
1: add();
break;
case
2: dispF();break;
case
3: dispB();break;
case
4: del ();break;
case
5: insf();break;
case
6: insm();break;
case
7: insl();break;
case
8:printf("\n\tQuit.......");
getch();
exit(0);
default:printf("\n\t
* * Invalid Choice * *");
}
}
getch();
}
No comments:
Post a Comment