This C++ program defines functions to delete an entire linked list and to add nodes to the front of a linked list. The deleteList function iterates through the list and frees the memory of each node, ultimately setting the head pointer to NULL. The push function allocates a new node, sets its data, links it to the existing list, and updates the head pointer. The main function uses push to construct a sample list and then calls deleteList to delete the whole list.
This C++ program defines functions to delete an entire linked list and to add nodes to the front of a linked list. The deleteList function iterates through the list and frees the memory of each node, ultimately setting the head pointer to NULL. The push function allocates a new node, sets its data, links it to the existing list, and updates the head pointer. The main function uses push to construct a sample list and then calls deleteList to delete the whole list.
This C++ program defines functions to delete an entire linked list and to add nodes to the front of a linked list. The deleteList function iterates through the list and frees the memory of each node, ultimately setting the head pointer to NULL. The push function allocates a new node, sets its data, links it to the existing list, and updates the head pointer. The main function uses push to construct a sample list and then calls deleteList to delete the whole list.
{ next = current->next; free(current); current = next; }
/* deref 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;