Insertion:- To add a new node in a given Doubly Linked List is known as Insertion. It is of four types-
इंसर्शन:- डबली लिंक्ड लिस्ट में किसी नए नोड को जोड़ना, इंसर्शन कहलाता है। यह चार प्रकार से किया जा सकता है-
a) Insert first node (पहला नोड जोड़ना)
//Insert First Node in
DLL
void insertfirstdll(){
int item;
clrscr();
n=malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow
Condition\n");
getch();
return;
}
printf("Enter an
item to insert\n");
scanf("%d",&item);
n->info=item;
n->back=NULL;
if(first==NULL){
n->next=NULL;
first=last=n;
}
else{
n->next=first;
first->back=n;
first=n;
}
printf("First node
nserted\n");
getch();
return;
}
b) Insert last node (अंतिम नोड जोड़ना)
//Insert Last Node in
DLL
void insertlastdll(){
int item;
clrscr();
n=malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow
Condition\n");
getch();
return;
}
printf("Enter item
to insert\n");
scanf("%d",&item);
n->info=item;
n->next=NULL;
if(last==NULL){
n->back=NULL;
first=last=n;
}
else{
n->back=last;
last->next=n;
last=n;
}
printf("last Node
inserted\n");
getch();
return;
}
c) Insert a node after given location (किसी दी गयी स्थिति के पश्चात नोड को जोड़ना)
//Insert a Node after
given Location in DLL
void
insertafterlocdll(NODE *loc){
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow
Condition\n");
getch();
return;
}
if(first==NULL){
printf("DLL is not
Exist\n");
getch();
return;
}
if(loc==last){
printf("location is
last Node\nChoose Option insert last node in dll\n");
getch();
return;
}
printf("Enter an
item to insert\n");
scanf("%d",&item);
n->info=item;
n->back=loc;
n->next=loc->next;
loc->next->back=n;
loc->next=n;
printf("node
inserted after given location\n");
getch();
return;
}
d) Insert a node before given location (किसी दी गयी स्थिति के पुर्व नोड को जोड़ना)
//Insert a Node before
given Location in DLL
void
insertbeforelocdll(NODE *loc){
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow
Condition\n");
getch();
return;
}
if(first==NULL){
printf("DLL is not
Exist\n");
getch();
return;
}
if(loc==first){
printf("location
sis first Node\nChoose Option insert first node in dll\n");
getch();
return;
}
printf("Enter an
item to insert\n");
scanf("%d",&item);
n->info=item;
n->back=loc->back;
n->next=loc;
loc->back->next=n;
loc->back=n;
}
printf("node
inserted before given location\n");
getch();
return;
}
No comments:
Post a Comment