কম্পিউটার টিউটোরিয়াল

লিংকড লিস্টে M নোডের পর N নোড মুছে ফেলা C++-এ


প্রথমে লিংকড লিস্ট সংজ্ঞায়িত করা হয় যাতে থাকে ডেটা এবং নেক্সট নোডের পয়েন্টার।

struct Node {
    int data;
    struct Node* next;
};

createList(Node** headPtr, int new_data) ফাংশন একটি ডাবল পয়েন্টার এবং ইন্টিজার ভ্যালু নেয়। নতুন নোডের নেক্সট পয়েন্টার হেড পয়েন্টারে এবং হেড পয়েন্টার নতুন নোডে সেট করা হয়।

void createList(Node** headPtr, int new_data) {
    Node* newNode = new Node();
    newNode->data = new_data;
    newNode->next = (*headPtr);
    (*headPtr) = newNode;
}

deleteNnodesAfterM(Node* head, int M, int N) মেথড রুট নোড এবং M, N ভ্যালু নেয়। current কে হেড এসাইন করা হয় এবং Node* t ডিক্লেয়ার করা হয়।

void deleteNnodesAfterM(Node* head, int M, int N) {
    Node *current = head, *t;
    int nodeCount;
    
    while (current) {
        for (nodeCount = 1; nodeCount < M && current != NULL; nodeCount++)
            current = current->next;
        if (current == NULL)
            return;
        t = current->next;
        
        for (nodeCount = 1; nodeCount <= N && t != NULL; nodeCount++) {
            Node* temp = t;
            t = t->next;
            free(temp);
        }
        current->next = t;
        current = t;
    }
}

printList(Node* head) ফাংশন লিংকড লিস্ট প্রিন্ট করে।

void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}

উদাহরণ

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};

void createList(Node** headPtr, int new_data) {
    Node* newNode = new Node();
    newNode->data = new_data;
    newNode->next = (*headPtr);
    (*headPtr) = newNode;
}

void printList(Node* head) {
    Node* temp = head;
    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }
    cout << endl;
}

void deleteNnodesAfterM(Node* head, int M, int N) {
    Node *current = head, *t;
    int nodeCount;
    while (current) {
        for (nodeCount = 1; nodeCount < M && current != NULL; nodeCount++)
            current = current->next;
        if (current == NULL)
            return;
        t = current->next;
        for (nodeCount = 1; nodeCount <= N && t != NULL; nodeCount++) {
            Node* temp = t;
            t = t->next;
            free(temp);
        }
        current->next = t;
        current = t;
    }
}

int main() {
    Node* head = NULL;
    int M = 2, N = 2;
    createList(&head, 2);
    createList(&head, 4);
    createList(&head, 6);
    createList(&head, 8);
    createList(&head, 10);
    createList(&head, 12);
    createList(&head, 14);
    cout << "M = " << M << " N = " << N << endl;
    cout << "Original linked list :" << endl;
    printList(head);
    deleteNnodesAfterM(head, M, N);
    cout << "Linked list after deletion :" << endl;
    printList(head);
    return 0;
}

আউটপুট

M = 2 N = 2
Original linked list :
14 12 10 8 6 4 2
Linked list after deletion :
14 12 6 4

  1. মাল্টিলেভেল লিংকড লিস্ট ফ্ল্যাটেন করা (C++)

  2. সি++-এ সার্কুলার লিংকড লিস্টের নোডগুলোর যোগফল (Sum)

  3. C++-এ সার্কুলার লিঙ্কড লিস্টে নোড গণনা

  4. পাইথনে লিংকড লিস্ট থেকে m নোডের পর n নোড ডিলিট করার প্রোগ্রাম