इस ट्यूटोरियल में एक लिंक्ड लिस्ट दी गई है, और हमें x से छोटी सभी संख्याओं को लिस्ट की शुरुआत में और बाकी को अंत में रखना है। हमें उनका क्रम भी पहले जैसा ही बनाए रखना है। उदाहरण के लिए
इनपुट: 1->4->3->2->5->2->3, x = 3 आउटपुट: 1->2->2->3->3->4->5 इनपुट: 1->4->2->10 x = 3 आउटपुट: 1->2->4->10 इनपुट: 10->4->20->10->3 x = 3 आउटपुट: 3->10->4->20->10
इस समस्या को हल करने के लिए, हमें तीन लिंक्ड लिस्ट बनाने की आवश्यकता है। अब जब हम x से छोटी संख्या का सामना करते हैं, तो हम उसे पहली लिस्ट में डालते हैं। अब x के बराबर मान के लिए, हम उसे दूसरी में डालते हैं, और बड़े मान के लिए, हम उसे तीसरी में डालते हैं। अंत में, हम लिस्ट को जोड़ते हैं और अंतिम लिस्ट प्रिंट करते हैं।
समाधान खोजने का दृष्टिकोण
इस दृष्टिकोण में, हम तीन लिस्ट बनाए रखने जा रहे हैं, अर्थात् small, equal, large। अब हम उनका क्रम बनाए रखते हैं और लिस्ट को अंतिम लिस्ट में जोड़ते हैं, जो हमारा उत्तर है।
उदाहरण
उपरोक्त दृष्टिकोण के लिए C++ कोड
#include<bits/stdc++.h> using namespace std; struct Node{ // हमारे नोड की संरचना int data; struct Node* next; }; // एक नया नोड बनाने के लिए यूटिलिटी फंक्शन Node *newNode(int data){ struct Node* new_node = new Node; new_node->data = data; new_node->next = NULL; return new_node; } struct Node *partition(struct Node *head, int x){ struct Node *smallhead = NULL, *smalllast = NULL; // हम लिस्ट के लिए दो पॉइंटर लेते हैं //ताकि यह कन्कैटेनेशन में हमारी मदद करे struct Node *largelast = NULL, *largehead = NULL; struct Node *equalhead = NULL, *equallast = NULL; while (head != NULL){ // मूल लिस्ट को पार करना if (head->data == x){ // x के बराबर के लिए if (equalhead == NULL) equalhead = equallast = head; else{ equallast->next = head; equallast = equallast->next; } } else if (head->data < x){ // x से छोटे के लिए if (smallhead == NULL) smalllast = smallhead = head; else{ smalllast->next = head; smalllast = head; } } else{ // x से बड़े के लिए if (largehead == NULL) largelast = largehead = head; else{ largelast->next = head; largelast = head; } } head = head->next; } if (largelast != NULL) // largelast NULL को इंगित करेगा क्योंकि यह हमारी अंतिम लिस्ट है largelast->next = NULL; /**********लिस्ट को जोड़ना**********/ if (smallhead == NULL){ if (equalhead == NULL) return largehead; equallast->next = largehead; return equalhead; } if (equalhead == NULL){ smalllast->next = largehead; return smallhead; } smalllast->next = equalhead; equallast->next = largehead; return smallhead; } void printList(struct Node *head){ // हमारी लिस्ट प्रिंट करने का फंक्शन struct Node *temp = head; while (temp != NULL){ printf("%d ", temp->data); temp = temp->next; } } int main(){ struct Node* head = newNode(10); head->next = newNode(4); head->next->next = newNode(5); head->next->next->next = newNode(30); head->next->next->next->next = newNode(2); head->next->next->next->next->next = newNode(50); int x = 3; head = partition(head, x); printList(head); return 0; }आउटपुट
2 10 4 5 30
उपरोक्त कोड की व्याख्या
ऊपर वर्णित दृष्टिकोण में, हम तीन लिंक्ड लिस्ट को क्रमिक क्रम में सामग्री के साथ रखेंगे। ये तीन लिंक्ड लिस्ट तत्वों को अलग-अलग x से छोटे, बराबर, और बड़े रखेंगी। हमारा काम अब सरल हो गया है। हमें लिस्ट को जोड़ना है और फिर हेड वापस करना है।
निष्कर्ष
इस ट्यूटोरियल में, हम दिए गए मान के आसपास लिंक्ड लिस्ट को विभाजित करना और मूल क्रम बनाए रखना हल करते हैं। हमने इस समस्या के लिए C++ प्रोग्राम और पूरा दृष्टिकोण (सामान्य) भी सीखा जिससे हमने इस समस्या को हल किया। हम यही प्रोग्राम C, Java, Python, और अन्य भाषाओं में भी लिख सकते हैं। आशा है आपको यह ट्यूटोरियल उपयोगी लगा होगा।