將單鏈錶轉換成異或連結串列,C++


本教程中,我們將討論一個程式,將單鏈錶轉換成異或連結串列。

為此,我們會提供一個單鏈表。我們的任務是獲取該連結串列的元素並將它們轉換成異或連結串列。

示例

 線上演示

#include <bits/stdc++.h>
using namespace std;
//node structure of linked list
struct Node {
   int data;
   struct Node* next;
};
//creation of new node
Node* newNode(int data){
   Node* temp = new Node;
   temp->data = data;
   temp->next = NULL;
   return temp;
}
//printing singly linked list
void print(Node* head){
   while (head) {
      cout << head->data << " ";
      head = head->next;
   }
   cout << endl;
}
//calculating XORed values
Node* XOR(Node* a, Node* b){
   return (Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
//converting the singly linked list
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;
   }
}
//printing the XORed linked list
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

更新於: 22-01-2020

203 檢視

開啟您的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.