কম্পিউটার

প্রদত্ত লিঙ্কযুক্ত তালিকার C++ পেয়ারওয়াইজ অদলবদল উপাদান


একটি সমস্যা সমাধানের জন্য যেখানে আমাদের একটি লিঙ্কযুক্ত তালিকায় উপস্থিত জোড়াওয়াইজ নোডগুলিকে অদলবদল করতে হবে এবং তারপরে এটি প্রিন্ট করতে হবে, উদাহরণস্বরূপ

Input : 1->2->3->4->5->6->NULL

Output : 2->1->4->3->6->5->NULL

Input : 1->2->3->4->5->NULL

Output : 2->1->4->3->5->NULL

Input : 1->NULL

Output : 1->NULL

O(N) এর সময় জটিলতা থাকার জন্য উভয় সমাধানের দিকে যাওয়ার দুটি উপায় রয়েছে, যেখানে N হল আমাদের প্রদত্ত লিঙ্কযুক্ত তালিকার আকার, তাই এখন আমরা উভয় পদ্ধতির অন্বেষণ করতে যাচ্ছি

পুনরাবৃত্ত পদ্ধতি

আমরা এই পদ্ধতিতে লিঙ্ক করা তালিকার উপাদানগুলির মাধ্যমে পুনরাবৃত্তি করব, এবং তারা NULL-এ না পৌঁছানো পর্যন্ত তাদের জোড়ায় জোড়ায় অদলবদল করব৷

উদাহরণ

#include <bits/stdc++.h>
using namespace std;
class Node { // node of our list
public:
    int data;
    Node* next;
};
void swapPairwise(Node* head){
    Node* temp = head;
    while (temp != NULL && temp->next != NULL) { // for pairwise swap we need to have 2 nodes hence we are checking
        swap(temp->data,
            temp->next->data); // swapping the data
        temp = temp->next->next; // going to the next pair
    }
}
void push(Node** head_ref, int new_data){ // function to push our data in list
    Node* new_node = new Node(); // creating new node
    new_node->data = new_data;
    new_node->next = (*head_ref); // head is pushed inwards
    (*head_ref) = new_node; // our new node becomes our head
}
void printList(Node* node){ // utility function to print the given linked list
    while (node != NULL) {
       cout << node->data << " ";
       node = node->next;
    }
}
int main(){
    Node* head = NULL;
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
    cout << "Linked list before\n";
    printList(head);
    swapPairwise(head);
    cout << "\nLinked list after\n";
    printList(head);
    return 0;
}

আউটপুট

Linked list before
1 2 3 4 5
Linked list after
2 1 4 3 5

আমরা আমাদের নিম্নলিখিত পদ্ধতিতে একই সূত্র ব্যবহার করব, কিন্তু আমরা পুনরাবৃত্তির মাধ্যমে পুনরাবৃত্তি করব।

পুনরাবৃত্ত পদ্ধতি

এই পদ্ধতিতে, আমরা পুনরাবৃত্তির সাথে একই যুক্তি প্রয়োগ করছি।

উদাহরণ

#include <bits/stdc++.h>
using namespace std;
class Node { // node of our list
public:
    int data;
    Node* next;
};
void swapPairwise(struct Node* head){
    if (head != NULL && head->next != NULL) { // same condition as our iterative
        swap(head->data, head->next->data); // swapping data
        swapPairwise(head->next->next); // moving to the next pair
    }
    return; // else return
}
void push(Node** head_ref, int new_data){ // function to push our data in list
    Node* new_node = new Node(); // creating new node
    new_node->data = new_data;
    new_node->next = (*head_ref); // head is pushed inwards
    (*head_ref) = new_node; // our new node becomes our head
}
void printList(Node* node){ // utility function to print the given linked list
    while (node != NULL) {
        cout << node->data << " ";
        node = node->next;
    }
}
int main(){
    Node* head = NULL;
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
    cout << "Linked list before\n";
    printList(head);
    swapPairwise(head);
    cout << "\nLinked list after\n";
    printList(head);
    return 0;
}

আউটপুট

Linked list before
1 2 3 4 5
Linked list after
2 1 4 3 5

উপরের কোডের ব্যাখ্যা

এই পদ্ধতিতে, আমরা জোড়ায় জোড়ায় আমাদের লিঙ্কযুক্ত তালিকার মধ্য দিয়ে যাই। এখন, আমরা একটি জোড়ায় পৌঁছানোর সাথে সাথে আমরা তাদের ডেটা অদলবদল করি এবং পরবর্তী জোড়ায় চলে যাই, এবং এভাবেই আমাদের প্রোগ্রাম উভয় পদ্ধতিতে এগিয়ে যায়।

উপসংহার

এই টিউটোরিয়ালে, আমরা পুনরাবৃত্তি এবং পুনরাবৃত্তি ব্যবহার করে প্রদত্ত লিঙ্কযুক্ত তালিকার পেয়ারওয়াইজ সোয়াপ উপাদানগুলি সমাধান করি। আমরা এই সমস্যার জন্য C++ প্রোগ্রাম এবং সম্পূর্ণ পদ্ধতি (স্বাভাবিক) শিখেছি যার মাধ্যমে আমরা এই সমস্যার সমাধান করেছি। আমরা অন্যান্য ভাষা যেমন সি, জাভা, পাইথন এবং অন্যান্য ভাষায় একই প্রোগ্রাম লিখতে পারি। আমরা আশা করি আপনার এই টিউটোরিয়ালটি সহায়ক হবে৷


  1. C++ এ লিঙ্ক করা তালিকার বিকল্প নোডের যোগফল

  2. C++ এর বিপরীতে অপরিবর্তনীয় লিঙ্কযুক্ত তালিকা প্রিন্ট করুন

  3. C++ এ রিভার্স লিঙ্কড লিস্ট II

  4. একটি লিঙ্কযুক্ত তালিকা C++ এ জোড়া অনুসারে সাজানো আছে কিনা তা পরীক্ষা করুন