Singly Linked List (SLL) Insertion operation and their types, Insert functions/algorithms

Insertion:- To add a new node in a given Singly Linked List is known as Insertion. It is of four types-
इंसर्शन:- लिंक्ड लिस्ट में किसी नए नोड को जोड़ना, इंसर्शन कहलाता है। यह चार प्रकार से किया जा सकता है-    

a) Insert first node (पहला नोड जोड़ना)

//Insert First Node in SLL
void insertfirstsll(){
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow Condition\n");
getch();
return;
}
printf("Enter item \n");
scanf("%d",&item);
n->info=item;
if(start==NULL){
n->link=NULL;}
else{
n->link=start;}
start=n;
printf("First Node inserted\n");
getch();
return;
}

b) Insert last node (अंतिम नोड जोड़ना)

//Insert Last Node in SLL
void insertlastsll(){
NODE *temp=start;
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow Condition\n");
getch();
return;
}
printf("Enter item \n");
scanf("%d",&item);
n->info=item;
n->link=NULL;
if(temp==NULL){
start=n;
}
else{
while(temp->link!=NULL){
temp=temp->link;}
temp->link=n;}
printf("Last Node inserted\n");
getch();
return;
}

c) Insert a node after given location (किसी दी गयी स्थिति के पश्चात नोड को जोड़ना)

//Insert a Node after given Location in SLL
void insertafterlocsll(NODE *loc){
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow Condition\n");
getch();
return;
}
printf("Enter item \n");
scanf("%d",&item);
n->info=item;
if(start==NULL){
printf("SLL is not Exist\nChoose Option Insert First Node\n");
getch();
return;
}
else{
n->link=loc->link;
loc->link=n;
}
printf("Node inserted after given location\n");
getch();
return;
}

d) Insert a node before given location  (किसी दी गयी स्थिति के पुर्व नोड को जोड़ना)

//Insert a Node before given Location in SLL
void insertbeforelocsll(NODE *loc){
NODE *temp=start;
int item;
clrscr();
n= malloc(sizeof(NODE));
if(n==NULL){
printf("Overflow Condition\n");
getch();
return;
}
if(loc==start){
printf("LOC is First Node\nChoose Option Insert First Node\n");
getch();
return;
}
printf("Enter item \n");
scanf("%d",&item);
n->info=item;
n->link=loc;
while(temp->link!=loc){
temp=temp->link;}
temp->link=n;
printf("Node inserted before given Location\n");
getch();
return;
}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...