poltcommercial.blogg.se

Recursive linked list stack
Recursive linked list stack







recursive linked list stack

After that it deletes all the nodes of the list by calling the function deleteLL() recursively. It then prints the original list using the function printLL().

Recursive linked list stack series#

The following C++ program inserts a series of nodes, one by one, in the linked list using the insert() function. In simple words, the nodes are deleted in reverse order: 4 -> 3 -> 2 -> 1. So the function returns to the previous call in the linked list and frees up the current head node. After this, in the next call, the head = NULL since we have reached the end of the linked list. Similarly, it calls itself with the node 4. Since it's not, it calls itself with the next node in the list (i.e., node 3). The second call to deleteLL() checks if the current head (i.e., node 2) is NULL. Since it's not, it calls itself with the next node in the list (i.e., node 2). When the deleteLL() function is called with the head of this list (i.e., node 1), it first checks if the head is NULL. Initialize new node ‘n’ with data value ‘key’.Įxplanation − After inserting all the nodes, one by one, in the linked list, the linked list can be represented as The approach consists of the following steps −ĭelete the node that the head node points to recursively. The linked list is considered empty when the head is equal to NULL. To delete all the elements of a linked list, we traverse through the linked list and delete all the nodes one by one.

recursive linked list stack

the head of the linked list should be NULL.

recursive linked list stack

Given a linked list, the task is to delete all its elements using recursion, i.e. A diagrammatic representation of a linked list is shown below. The last element of the linked list can be defined as the element which points to NULL. The first node of the linked list is referred to as the ‘head’ of the list. A node is composed of a data field, which holds the value of the element, and an address field, which points to the location of the next node in the series. A linked list is a linear data structure in which the elements are stored at non-contiguous memory locations.









Recursive linked list stack