Linked List : Inserting Operation in c language in hindi , c कंप्यूटर भाषा में इन्सर्ट ऑपरेशन हिंदी में

c कंप्यूटर भाषा में इन्सर्ट ऑपरेशन हिंदी में , Linked List : Inserting Operation in c language in hindi :-
linked लिस्ट का सबसे बड़ा फायदा है की इसमें node को कही पर भी add किया जा सकता है | linked लसी मे किसी भी नए node को add करने के लिए केवल दो pointer को हो रिसेट करना होता है | linked लिस्ट मे node को तीन जगह पर add कर सकते है :-
1.at the beginning
2.at end of list
3.at the middle of listAlgorithm
किसी भी linked लिस्ट मे node को किसी भी position पर add करने के लिए नीचे लिखी algorithm फॉलो होती है :-
अगर list खाली है और new node, head node से पहले add करना है तब
new node , head node के तरह insert होता है |
या
अगर new node, last node से बाद add करना है तब
new node last node की तरह add होगा |

या
new node list की body मे add होगा |

किसी node को first position पर add करने के लिए निन्म algorithm फॉलो होती है :-
1.new node के लिए space को check करना |
2.new node के item फील्ड मे data को assign करना |
3.new node के address फ़ील्ड को लिस्ट के पहले node के address से set करना |
4.head pointer को new node को point करवाना |

किसी node को दो nodes ‘a’ और ‘b’ के बीच add करना |
1.new node के लिए space को check करना |
2.new node के item फील्ड मे data को assign करना |
3.new node के address फ़ील्ड को लिस्ट के node ‘b’ के address से set करना |
4.node ‘a’ के address को new node के address से set करना |

source code :
node को पहले position मे insert कराने का प्रोग्राम को हम create list के उदाहरन मे हम देख चुके इसलिए इस article मे हम at the end और at the middle को पढ़गे |

at the end
Explanation
इस उदाहरण मे ,
किसी linked लिस्ट के node को declare किया गया है जिसमे salary data item है और address एक pointer variable है जिसमे की next node का address store होता है |
इसके बाद तीन function को declare किया है :-
void create_list(int n) : इस function से linked लिस्ट को create किया जाता है |जिसमे number of node को pass किया है |
void display_list(): इस function से linked list को display किया जाता है |
void addnode_end(): इस function का use , new node को लिस्ट के end मे add करने के लिए किया गया है |

main() मे ,
इस function मे ,यूजर से number of node को input लेते है जिसे variable ‘num’ मे assign करते और void create_list() मे pass किया जाता है |
बाद मे void display_list() को call किया जाता है|
इसके बाद मे , void addnode_end() को call किया जाता जिससे एक न्य node list की end मे add हो जाता है |उसके बाद void display_list() को फिर से call किया जाता है |

void create_list(int n) मे ,

सबसे पहले ,एक memory block को allocate करते है जिसका address pointer variable ‘h’ मे store किया है |

अगर pointer variable ‘h’ मे null value होगी , तब no more space का message आएगा |

अगर null नहीं है तो ‘h’ के salary item मे यूजर द्वारा input value को assign होता है और ‘h’ का address field मे null value store हो जाती है |और structure type variable ‘t’ लिस्ट का पहला node ‘h’ बन जाता है|

उसके बाद मे linked list के बाकि के node को बनायेगे और उसे linked list मे add करेगे |

इस operation के loop चलेगा | dynamic memory allocate होता है |इसके address को addnode मे store करा देते है |उसके बाद addnode के item field मे यूजर input को assign करेगे |और इसके address field मे null value assign हो जाती है |

इसके बाद ‘t’ variable के address field को newnode के address से लिंक किया जाता|

Display_list()

इस function मे linked list के सभी element को display करेगे |इसमें

1.सबसे पहले structure type node का एक pointer ‘t’ को करता है |
2.उसके बाद h की value check होती |अगर h की value null है तब empty linked लिस्ट message आएगा |
3. उसके बाद declare variable ‘t’ मे ‘h’ को assign करा देते है |
4.अगर null value नहीं होती तब while loop मे t != NULL की condition के साथ चलेगा |
5.उसके बाद ‘t’ के salary item मे store value को print हो जायेगे |
6.फिर ‘t’ का address , अगले वाले node का address contain करता है |फिर से loop चलता है |
7.ये loop तब तक चलता है जब तक ‘t’ का address null नहीं हो जाता है |

void addnode_end():
इस function मे सबसे पहले addnode pointer variable मे एक नए node के address return होता है |उसके बाद यूजर द्वारा input data को addnode के item field मे assign करा देते है और addnode के address field को null से initial कर देते है |
उसके बाद while loop चलाया जाता है जिसे की लिस्ट के last node के position तक जाया जा सका |
इस loop मे ,initialization t=h; है जो की temporary variable को लिस्ट की head से initial कर देता है और
condition मे t!=null pass होगा इसका मतलब है की जबब तक t की value null नहीं होती tab तक while loop चलता रहेगा |
और t की address field हर बार new node के address से increment होगी |

#include
#include
#include
/* Structure of a node */
struct employee {
int salary; // salary
struct employee *address; // Address
}*h;
void create_list(int n);
void addnode_end(int);
void display_list();
int main()
{
int num, sal;
printf(“Enter nodes number “);
scanf(“%d”, &num);
create_list(num);
printf(“\n display List data \n”);
display_list();
printf(“\nEnter salary that you want to add\n “);
scanf(“%d”, &sal);
addnode_end(sal);
printf(“\n data in the list \n”);
display_list();
getch();
}
void create_list(int n)
{
struct employee *addnode, *t;
int s, i;
h = (struct employee *)malloc(sizeof(struct employee));
if(h == NULL)
{
printf(“no more space “);
}
else
{
printf(“Enter the salary : “);
scanf(“%d”, &s);
h->salary = s; // assign the user inpur salary into salary field of ‘h’
h->address = NULL; // assign null to address field of ‘h’
t = h;
for(i=2; i<=n; i++) { addnode = (struct employee *)malloc(sizeof(struct employee)); if(addnode == NULL) { printf(” No more space “); break; } else { printf(“Enter the salary : “); scanf(“%d”, &s); addnode->salary = s; // assign the user input salary into salary field of new node
addnode->address = NULL; // assign null to address field of new node
t->address = addnode; // node linking mean make a link between t or addnode
t = t->address;
}
}
}
}
void addnode_end(int sala)
{
struct employee *addnode, *t;
addnode = (struct employee*)malloc(sizeof(struct employee));
if(addnode == NULL)
{
printf(” no more space “);
}
else
{
addnode->salary = sala;
addnode->address = NULL;
t = h;
/* Go to last node of linked list */
while(t->address != NULL)
t = t->address;
t->address = addnode; // node linking mean make a link between last node of list or addnode
printf(“last node added”);
}
}

void display_list()
{
struct employee *t;
if(head == NULL)
{
printf(“List is already empty “);
}
else
{
t = head;
while(t != NULL)
{
printf(“salary = %d\n”, t->salary);
t = t->address;
}
}
}
आउटपुट होगा :
Enter nodes number 3
Enter the salary :2000
Enter the salary :3000
Enter the salary :3000
display List data
salary =2000
salary =3000
salary =3000
Enter salary that you want to add 5000
last node added
data in the list
salary =2000
salary =3000
salary =3000
salary =5000

At the end
Explanation
इस उदाहरण मे ,
किसी linked लिस्ट के node को declare किया गया है जिसमे salary data item है और address एक pointer variable है जिसमे की next node का address store होता है |
इसके बाद तीन function को declare किया है :-
void create_list(int n) : इस function से linked लिस्ट को create किया जाता है |जिसमे number of node को pass किया है |
void display_list(): इस function से linked list को display किया जाता है |
void addnode_end(): इस function का use , new node को लिस्ट के end मे add करने के लिए किया गया है |

main() मे ,
इस function मे ,यूजर से number of node को input लेते है जिसे variable ‘num’ मे assign करते और void create_list() मे pass किया जाता है |
बाद मे void display_list() को call किया जाता है|
इसके बाद मे , void addnode_end() को call किया जाता जिससे एक न्य node list की end मे add हो जाता है |उसके बाद void display_list() को फिर से call किया जाता है |

void create_list(int n) मे ,

सबसे पहले ,एक memory block को allocate करते है जिसका address pointer variable ‘h’ मे store किया है |

अगर pointer variable ‘h’ मे null value होगी , तब no more space का message आएगा |

अगर null नहीं है तो ‘h’ के salary item मे यूजर द्वारा input value को assign होता है और ‘h’ का address field मे null value store हो जाती है |और structure type variable ‘t’ लिस्ट का पहला node ‘h’ बन जाता है|

उसके बाद मे linked list के बाकि के node को बनायेगे और उसे linked list मे add करेगे |

इस operation के loop चलेगा | dynamic memory allocate होता है |इसके address को addnode मे store करा देते है |उसके बाद addnode के item field मे यूजर input को assign करेगे |और इसके address field मे null value assign हो जाती है |

इसके बाद ‘t’ variable के address field को newnode के address से लिंक किया जाता|

Display_list()

इस function मे linked list के सभी element को display करेगे |इसमें

1.सबसे पहले structure type node का एक pointer ‘t’ को करता है |
2.उसके बाद h की value check होती |अगर h की value null है तब empty linked लिस्ट message आएगा |
3. उसके बाद declare variable ‘t’ मे ‘h’ को assign करा देते है |
4.अगर null value नहीं होती तब while loop मे t != NULL की condition के साथ चलेगा |
5.उसके बाद ‘t’ के salary item मे store value को print हो जायेगे |
6.फिर ‘t’ का address , अगले वाले node का address contain करता है |फिर से loop चलता है |
7.ये loop तब तक चलता है जब तक ‘t’ का address null नहीं हो जाता है |

void addnode_middle():
इस function मे सबसे पहले addnode pointer variable मे एक नए node के address return होता है |उसके बाद यूजर द्वारा input data को addnode के item field मे assign करा देते है और addnode के address field को null से initial कर देते है |
उसके बाद for loop चलाया जाता है जिसे की लिस्ट के उस position तक जाया जाता जहा पर node को add करना है|
इस loop मे ,initialization t=h; है जो की temporary variable को लिस्ट की head से initial कर देता है और
condition मे t!=pos-1 होगा इसका मतलब है की जब तक t की value position-1 नहीं होती तब तक for loop चलता रहेगा |
और t की address field हर बार new node के address से increment होगी |
जब t की value position-1 हो जाती तब addnode->address = t->address; से addnode के address field को t के address से लिंक करेगे |
source code :
#include
#include
#include
/* Structure of a node */
struct employee {
int salary; // salary
struct employee *address; // Address
}*h;
void create_list(int n);
void addnode_middle(int , int );
void display_list();
int main()
{
int num, s, pos;
printf(“Enter node number : “);
scanf(“%d”, &num);
create_list(num);
printf(“\n linked list data display \n”);
display_list();
printf(“\n Enter salary : “);
scanf(“%d”, &s);
printf(“Enter position the you want to add a node ” );
scanf(“%d”, &pos);
addnode_middle(s, pos);
printf(“\n data in the list after inserting \n”);
display_list();
getch();
}

void create_list(int n)
{
struct employee *addnode, *t;
int sal, i;
h = (struct employee *)malloc(sizeof(struct employee));
if(h == NULL)
{
printf(“no more space “);
}
else
{
printf(“Enter salary : “);
scanf(“%d”, &sal);
h->salary = sal;
h->address = NULL;
t = h;
for(i=2; i<=n; i++) { addnode = (struct employee *)malloc(sizeof(struct employee)); if(addnode == NULL) { printf(“No more space “); break; } else { printf(“Enter salary : “); scanf(“%d”, &sal); addnode->salary = salary;
addnode->address = NULL;
t->address = addnode;
t = t->address;
}
}
}
}
void addnode_middle(int salary, int position)
{
int i;
struct employee *addnode, *t;
addnode = (struct employee*)malloc(sizeof(struct employee));
if(addnode == NULL)
{
printf(” No more space “);
}
else
{
addnode->salary = salary; // Link salary part
addnode->address = NULL;
t = h;
for(i=2; i<=position-1; i++) /* go to position where we want add node **/ { t = t->address;
if(t == NULL)
break;
}
if(t != NULL)
{
addnode->address = t->address; /* Linking means make link between addnode or t */
t->address = addnode; /* linking means make link between t-1 or addnode */
printf(” Node inserted \n”);
}
else
{
printf(“Node can not add at position \n”);
}
}
}
void display_list()
{
struct employee *t;
if(h == NULL)
{
printf(“List is already empty “);
}
else
{
t = h;
while(t != NULL)
{
printf(“salary = %d\n”, t->salary);
t = t->address;
}
}
}
आउटपुट होगा
Enter nodes number 4
Enter the salary :2000
Enter the salary :3000
Enter the salary :3000
Enter the salary :5000
linked list data display
salary =2000
salary =3000
salary =3000
salary =5000
Enter the salary :7000
Enter position the you want to add a node 3
Node inserted
data in the list after inserting
salary =2000
salary =3000
salary =7000
salary =3000
salary =5000