इस लेख में, हमें एक लिंक्ड लिस्ट दी गई है। हमारा कार्य एकल लिंक्ड लिस्ट में प्रत्येक नोड मान को उलटने के लिए एक C प्रोग्राम बनाना है।
हम लिंक्ड लिस्ट के प्रत्येक नोड को लेंगे और मान को उलट देंगे।
लिंक्ड लिस्ट लिंक का एक क्रम है जिसमें आइटम होते हैं जो दूसरे लिंक से जुड़े होते हैं।
आइए एक उदाहरण लेकर समस्या को समझते हैं,
इनपुट
34 12 89 56 72
आउटपुट
43 21 98 65 27
इस समस्या को हल करने के लिए, हम एकल लिंक्ड लिस्ट को पार करेंगे और प्रत्येक नोड को लेंगे। और फिर वर्तमान नोड के मान को उलट देंगे।
एकल लिंक्ड लिस्ट में प्रत्येक नोड मान को उलटने का प्रोग्राम
// एकल लिंक्ड लिस्ट में प्रत्येक नोड मान को उलटने का प्रोग्राम।
उदाहरण
#include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; struct Node* insertNode(int key) { struct Node* temp = (struct Node*)malloc(sizeof(struct Node)); temp->data = key; temp->next = NULL; return temp; } int reverseValue(int number) { int revElement = 0, rem; while (number != 0) { rem = number % 10; revElement = revElement * 10 + rem; number = number / 10; } return revElement; } void reverseLinkedListElements(struct Node* node) { if (node == NULL) return; while (node != NULL) { node->data = reverseValue(node->data); node = node->next; } } void printLinkedList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } } int main() { struct Node* head = NULL; head = insertNode(34); head->next = insertNode(12); head->next->next = insertNode(89); head->next->next->next = insertNode(56); head->next->next->next->next = insertNode(72); printf("मूल लिंक्ड लिस्ट:\t"); printLinkedList(head); reverseLinkedListElements(head); printf("\nपरिवर्तित लिंक्ड लिस्ट:\t"); printLinkedList(head); return 0; } आउटपुट
मूल लिंक्ड लिस्ट: 34 12 89 56 72 परिवर्तित लिंक्ड लिस्ट: 43 21 98 65 27