यहां, हम एक फ़ंक्शन बनाएंगे जो लिंक्ड लिस्ट के सभी एलिमेंट्स को एक-एक करके डिलीट करेगा।
C/C++ में, इस कार्य को करने के लिए कोई विशिष्ट फ़ंक्शन नहीं है लेकिन जावा में, स्वचालित गार्बेज कलेक्शन लिंक्ड लिस्ट को डिलीट करने को आसान बनाता है।
अब, आइए इस प्रोग्राम के कार्यान्वयन को देखें:
उदाहरण
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
void deleteLinkedList(Node** head_ref) {
Node* current = *head_ref;
Node* next;
while (current != NULL) {
cout << current->data << "\t";
next = current->next;
free(current);
current = next;
}
*head_ref = NULL;
}
void push(Node** head_ref, int new_data) {
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main() {
Node* head = NULL;
push(&head, 25);
push(&head, 10);
push(&head, 5);
push(&head, 90);
push(&head, 68);
cout << "लिंक्ड लिस्ट के एलिमेंट्स: ";
deleteLinkedList(&head);
cout << "\nलिंक्ड लिस्ट डिलीट हो गई";
}
आउटपुट
लिंक्ड लिस्ट के एलिमेंट्स: 68 90 5 10 25 लिंक्ड लिस्ट डिलीट हो गई