Linked List : Searching in c language in hindi , लिंक लिस्ट में सर्चिंग ऑपरेशन या सर्च क्या होता है हिंदी में

लिंक लिस्ट में सर्चिंग ऑपरेशन या सर्च क्या होता है हिंदी में Linked List : Searching in c language in hindi :-
searching किसी भी data structure मे सबसे common operation होता है |searching operation के लिए  algorithm है :
1.यूजर से item को input करना जिसे search करना है |
2.किसी दो variable को define करना जिसमे से variable 1 मे  found item को रखेगा और दूसरा variable 2 मे , लिस्ट के items को रखता है |
3.अगर variable 2 null नहीं और variable 1 से same नहीं है तो list के address को increment करेगे |और variable 2 मे लिस्ट का next item का address save होगा |
4.step 3 तब तक repeat होगी जब तक variable 2 की value null नहीं हो जाती या variable 1 और variable 2 की value same नहीं हो जाती |
5. अगर variable 2 की value null नहीं है तब variable 1 मिल जाता है और list के current address को return करेगे|
Searching का source code :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct employee{
int salary;                            // item field
struct employee *address; // Address field
} * head;
/* Function declaration */
void create_list(int n);
void display_list();
int search_list(int );
int main()
{
int num, search_item, position;
printf(“Enter nodes number : “);
scanf(“%d”, &num);
create_list(num);
printf(“\n salary in list  \n”);
display_list();
printf(“\n Enter element to be search : “);
scanf(“%d”, &search_item);
// Call function to search first element by k
position = search_list(search_item);
if (position >= 0)
{
printf(“%d found in the list at position %d\n”, search_item, position + 1);
}
else
{
printf(“%d not found in the list.\n”, search_item);
}
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 input 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;
}
}
}
}
int search(int k)
{
int count;
struct employee *searchnode;
count = 0;
searchnode = head;
// Iterate till last element until k is not found
while (searchnode != NULL && searchnode->salary != k)
{
count++;
searchnode = searchnode->address;
}
return (searchnode != NULL) ? count : -1;
}
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;
}
}
}
Explanation
इस code मे , employee एक structure है जो की linked लिस्ट के single node को define करता है |
इसमें तीन function है :-
void create_list(int n): ये function का use , linked लिस्ट को create करने के लिए किया जाता है |
int search(): इस function का use, linked list के node को search करने के लिए किया जाता है |
void display_list(): इस function का use linked लिस्ट के सभी items को display करने के लिए किया जाता है |
main() मे , 
1.main() function मे , तीन variable declare किये गये है |(i) num: ये variable linked list मे nodes की सख्या को contain करता है |(ii) position:इस varibale मे ,search () से return value को contain किया जाता है | (iii)search_item : ये variable item (जिसे search करना है) को hold करता है |
2.यूजर से node की सख्या को input कराया जाता है |फिर create_list function मे pass किया जाता है |
3.फिर display_list function से linked लिस्ट के सभी items को display किया जाता है |
4. यूजर से item (जिसे search करना है) को input कराया जाता है और इसे search() function मे pass किया जाता है |और search() function से मिला आउटपुट को variable ‘position ‘ मे received किया जाता है |
5.printf() statement से search item का position को  print करा दिया जाता है |
void create_list(int n) मे , 
1.सबसे पहले दो pointer variable addnode, t को declare किया जाता है जो की structure data है |
2.इसके अलावा दो variable ‘s’ और ‘i’ को declare किया गया है |
3.फिर pointer variable ‘h’ मे dynamic memory allocate space के address को store किया जाता है |
अगर pointer की null होती तो no more space का message आउटपुट होगा |
अन्यथा ‘h’ के item field मे user input salary को assign किया जाता है |और ‘h’ के address field को ‘null’ assign किया जाता है |इस प्रकार से linked list create हो जाती है लेकिन इसमें केवल एक ही node है |
4.create linked लिस्ट मे , यूजर द्वारा दिए node को insert करने के लिए for loop चलाया जाता है |जिसमे addnode pointer मे , allocate memory के address को assign करा देते है |
अगर addnode की value null होती तब no more space का message print हो जाता है |
अन्यथा
addnode के item field मे , यूजर input salary को assign किया जाता है और addnode के address field को null से assign किया जाता है |
linking की जाती है इसका मतलब है ‘t’ variable के address field मे addnode के address को assign किया जाता है |और t को t address बनाया जाता है |
void display_list() मे , 
1.सबसे पहले एक variable ‘t’  को declare किया जाता है जिसका data type structure employee है |
2.अगर head pointer ‘h’ null है tab empty लिस्ट का message आएगा |
अन्यथा t की h से initial करेगे |फिर while loop चलेगा  |
3.while loop की body मे , printf() statement से head pointer या ‘t’ की item को display करा देगे |
फिर t को t के address field मे सेव address से set करगे |
4.step 3 जब तक्क चलगे जब तक की t का address feild मे null नहीं आ जाता है |
int search() मे 
1.सबसे पहले एक variable ‘count’ को declare किया जाता है जिसका data type integer होता  है और इसके अलावा एक searchnode है जिसका data type structure employee है |
2.इसके बाद count की value ‘0’ और searchnode को pointer variable ‘h’ से initial किया जाता है |
3.while loop की body मे , count को 1 से set करगे और searchnode को searchnode के address field मे सेव address से set करेगे |
4. while loop मे , searchnode की address field को check करेगे |और searchnode की value को function search() मे received data से compare करेगे | तब , count को 2 से set करगे और searchnode को searchnode के address से set करेगे |
5.step 3 और step 4 तक चलता रहेगा जब तक दोनों मे से कोई एक condition true नहीं होती है|
6.count की value को return करेगे |
आउटपुट होगा :
Enter nodes number : 4
Enter the salary : 2000
Enter the salary : 1000
Enter the salary : 3000
Enter the salary : 4000
salary in list:
salary = 2000
salary = 1000
salary = 3000
salary = 4000
Enter element to be search : 3000
3000 found in the list at position 3.