Algoritmo para C++:
Iterar a través de la lista enlazada y eliminar todos los Nodes uno por uno. El punto principal aquí es no acceder al siguiente del puntero actual si se elimina el puntero actual.
Implementación:
C++
// C++ program to delete a linked list #include <bits/stdc++.h> using namespace std; // Link list node class Node { public: int data; Node* next; }; // Function to delete the entire // linked list void deleteList(Node** head_ref) { // Define head_ref to get the // real head Node* current = *head_ref; Node* next = NULL; while (current != NULL) { next = current->next; free(current); current = next; } // Define head_ref to affect the real // head back in the caller. *head_ref = NULL; } /* Given a reference (pointer to pointer) to the head of a list and an int, push a new node on the front of the list. */ void push(Node** head_ref, int new_data) { // Allocate node Node* new_node = new Node(); // Put in the data new_node->data = new_data; // Link the old list off the new node new_node->next = (*head_ref); // Move the head to point to the new node (*head_ref) = new_node; } // Driver code int main() { // Start with the empty list Node* head = NULL; // Use push() to construct list // 1->12->1->4->1 push(&head, 1); push(&head, 4); push(&head, 1); push(&head, 12); push(&head, 1); cout << "Deleting linked list"; deleteList(&head); cout << "Linked list deleted"; } // This code is contributed by rathbhupendra
Producción:
Deleting linked list Linked list deleted
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA