এখানে, আমরা একটি ফাংশন তৈরি করব যা লিঙ্কড লিস্টের সব উপাদান একে একে ডিলিট করবে।
সি/সি++-এ, এই কাজ করার জন্য কোনো নির্দিষ্ট ফাংশন নেই কিন্তু জাভা-এ, অটোমেটিক গার্বেজ কলেকশন হয় লিঙ্কড লিস্ট ডিলিট করা সহজ করার জন্য।
এখন, এই প্রোগ্রামের বাস্তবায়ন দেখি,
উদাহরণ
#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 লিঙ্কড লিস্ট ডিলিট হয়েছে