सबसे पहले हम अपना लिंक्ड लिस्ट परिभाषित करते हैं जिसमें डेटा और अगले नोड का पॉइंटर होता है।
struct Node { int data; struct Node* next; }; फिर हम createList(Node ** headPtr, int new_data) फ़ंक्शन बनाते हैं जो Node के डबल पॉइंटर और एक int मान लेता है। फ़ंक्शन के अंदर हम नए बने नोड के नेक्स्ट पॉइंटर को headptr को निर्दिष्ट करते हैं और फिर headptr को नए बने नोड को निर्दिष्ट करते हैं।
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 मान लेता है। अंदर हम Node* current को head असाइन करते हैं और Node *t भी घोषित करते हैं।
void deleteNnodesAfterM(Node *head, int M, int N){ Node *current = head, *t; int nodeCount; फ़ंक्शन के अंदर हमारे पास एक व्हाइल लूप है जो तब तक चलता है जब तक current NULL की ओर इशारा नहीं करता। पहला फॉर लूप M इटरेशन के लिए चलता है। पहले फॉर लूप के समाप्त होने के बाद current पॉइंटर लिंक्ड लिस्ट में M के बाद के नोड की ओर इशारा करता है। Node *t को फिर current->next का मान असाइन किया जाता है जो हटाया जाने वाला पहला मान है।
while (current){ for (nodeCount = 1; nodeCount < M && current!= NULL; nodeCount++) current = current->next; if (current == NULL) return; t = current->next; दूसरा फॉर लूप N इटरेशन के लिए चलता है और प्रारंभिक स्थिति से N संख्या में नोड्स को मुक्त करता है। current->next को फिर t असाइन किया जाता है और t हमारा करंट नोड बन जाता है।
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; } उदाहरण
आइए लिंक्ड लिस्ट में M नोड्स के बाद N नोड्स को हटाने का निम्नलिखित कार्यान्वयन देखें:
#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