इस ट्यूटोरियल में, हम सिंगली लिंक्ड लिस्ट को XOR लिंक्ड लिस्ट में बदलने के प्रोग्राम पर चर्चा करेंगे।
इसके लिए हमें एक सिंगली लिंक्ड लिस्ट दी जाएगी। हमारा कार्य उस लिस्ट के एलिमेंट्स लेना और उसे XOR लिंक्ड लिस्ट में बदलना है।
उदाहरण
#include <bits/stdc++.h>
using namespace std;
// लिंक्ड लिस्ट की नोड संरचना
struct Node {
int data;
struct Node* next;
};
// नया नोड बनाना
Node* newNode(int data) {
Node* temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
// सिंगली लिंक्ड लिस्ट प्रिंट करना
void print(Node* head) {
while (head) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
}
// XOR मान कैलकुलेट करना
Node* XOR(Node* a, Node* b) {
return (Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
// सिंगली लिंक्ड लिस्ट को बदलना
void convert(Node* head) {
Node* curr = head;
Node* prev = NULL;
Node* next = curr->next;
while (curr) {
next = curr->next;
curr->next = XOR(prev, next);
prev = curr;
curr = next;
}
}
// XOR लिंक्ड लिस्ट प्रिंट करना
void printXOR(Node* head) {
Node* curr = head;
Node* prev = NULL;
while (curr) {
cout << curr->data << " ";
Node* temp = curr;
curr = XOR(prev, curr->next);
prev = temp;
}
cout << endl;
}
int main() {
Node* head = newNode(1);
head->next = newNode(2);
head->next->next = newNode(3);
head->next->next->next = newNode(4);
cout << "Before Conversion : " << endl;
print(head);
convert(head);
cout << "After Conversion : " << endl;
printXOR(head);
return 0;
}
आउटपुट
Before Conversion : 1 2 3 4 After Conversion : 1 2 3 4