हिंदी माध्यम नोट्स
Categories: C Language in hindi
Linked List : Swapping in c language in hindi , लिंक लिस्ट में स्वपिंग क्या होती है c भाषा में हिंदी में
लिंक लिस्ट में स्वपिंग क्या होती है c कंप्यूटर भाषा में हिंदी में Linked List : Swapping in c language in hindi :-
Swapping linked list मे सबसे common और मुश्किल operation है | क्योकि इस operation के लिए हमे कई सारे conditions को check करना पड़ता है |
Explanation
इस code मे , employee एक structure है जो की linked लिस्ट के single node को define करता है |
इसमें चार function है :-
void create_list(int n): ये function का use , linked लिस्ट को create करने के लिए किया जाता है |
int swapping(): इस function का use, linked list के दो nodes को swap करने के लिए किया जाता है |
void display_list(): इस function का use linked लिस्ट के सभी items को display करने के लिए किया जाता है |
int counting() : इस function का use ,linked list के nodes की सख्या को calculate करता है |
int swapping(struct employee *list, int position1, int position2)
1.Singly linked लिस्ट को create किया जाता है और linked लिस्ट के first node के reference को head pointer मे store किया जाता है |
2.यूजर से swaping के लिए दो position को input करवाते है |जिसे position1 और position2 कहते है |
3.अगर यूजर द्वारा दिए गये position invalid है तब function ‘-1’ return करता है |
4.सभी चार node variable n1 ,n2 ,pre1 और pre2 को null से initial करते है |जहा पर n1 और n2 (जिन nodes को swap करना है ) के address को hold करता है |और pre1 और pre2 इस nodes के पहले के node के address को hold करता है |
5.इसके अलावा दुसरे node type variable ‘t’ को “list” से initial करते है |
6.इसके बाद हम ,node के position को set करते है इसके साथ ही इन node के पहले वाले node की position को set करते है |
7.loop के अन्दर :
अगर i==position1-1 हो तब pre1 से t तक update होती है |
अगर i==position1 हो तब position 1 के address से t तक update होती है |
अगर i==position2-1 हो तब pre1 से t तक update होती है |
अगर i==position2 हो तब position 2 के address से t तक update होती है |
इसके बाद ‘t’ को ‘t’ के addressfield मे store address से update कर देते है |
जब loop एक बार completes होता है तब तक हमे swap किये जाने वाले node की position मिल जाती है |
8.pre1 के address field को n2 से और pre2 के address field को n1 से लिंक कर देते है |
9.उसके बाद n2 के address feild को n1 के से लिंक करा देते है |
10.head node को adjust करा देते है अगर जरुरत है |
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 count_nodes() मे
1.while loop की body मे , c को 1 से set करगे और l को l के address field मे सेव address से set करेगे |
2. while loop मे , l की address field को check करेगे |अगर null नहीं है तब , ‘c’ को 2 से set करगे और ‘l’ को ‘l’ के address से set करेगे |
3.step 1 और step 2 तक चलता रहेगा जब तक की l की address field की value null नहीं हो जाती है |
4.’c’ की value को return करेगे |
source code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct employee{
int salary; // salary
struct employee *address; // Address
} * h;
/* Function declaration */
void create_list(int n);
void display_list();
int counting(struct employee *l );
int swapping(struct employee *list , int position1, int position2);
void main()
{
int num, pos1, pos2;
printf(“Enter nodes number : “);
scanf(“%d”, &num);
create_list(num);
printf(“\n\nsalary in list before swappingping: \n”);
display_list();
printf(“\nEnter first node position that you want to swapping : “);
scanf(“%d”, &pos1);
printf(“\nEnter second node position that you want to swapping : “);
scanf(“%d”, &pos2);
if (swapping(h, pos1, pos2) == 1)
{
printf(“\n salary swapping successfully.\n”);
printf(“salary in list after swapping %d node with %d: \n”, pos1, pos2);
display_list();
}
else
{
printf(“Invalid position”\n);
printf(“please enter position greater than 0 and less than nodes in list.\n”);
}
return 0;
}
int counting(struct employee *l)
{
int c = 0;
while (l != NULL)
{
c++;
l = l->address;
}
return c;
}
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 display_list()
{
struct employee *t;
if(h == NULL)
{
printf(” Empty List “);
}
else
{
t = h;
while(t != NULL)
{
printf(“salary = %d\n”, t->salary);
t = t->address;
}
}
}
int swapping(struct employee *list, int position1, int position2)
{
struct employee *n1, *n2, *pre1, *pre2, *t;
int i;
const int maxposition = (position1 > position2) ? position1 : position2;
const int total = counting(list);
if ((position1 <= 0 || position1 > total) || (position2 <= 0 || position2 > total))
{
return ( -1 );
}
if (position1 == position2)
{
return 1;
}
/* find out position of items that you want to be swapping */
i = 1;
t = list;
pre1 = NULL;
pre2 = NULL;
while (t != NULL && (i <= maxposition))
{
if (i == position1 – 1)
pre1 = t;
if (i == position1)
n1 = t;
if (i == position2 – 1)
pre2 = t;
if (i == position2)
n2 = t;
t = t->address;
i++;
}
/* swappinging start */
if (n1 != NULL && n2 != NULL)
{
// Link previous of n1 with n2
if (pre1 != NULL)
pre1->address = n2;
// Link previous of n2 with n1
if (pre2 != NULL)
pre2->address = n1;
/* swapping n1 and n2 by interchange their address */
t = n1->address;
n1->address = n2->address;
n2->address = t;
/8 swaping first element .*/
if (pre1 == NULL)
h = n2;
else if (pre2 == NULL)
h = n1;
}
return 1;
}
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
4 weeks ago
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
4 weeks ago
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
1 month ago
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
1 month ago
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
1 month ago
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…
1 month ago