Recursive Linked Lists Lecture 29 Sections 10.3 – 10.4 Wed, Apr 2, 2008
Recursive Linked Lists A linked list is a naturally recursive structure. The “linked list” is a pointer to a node. (Ignore the mSize data member.) Furthermore, each node contains a pointer to a node. Therefore, each node contains a “linked list.” However, the sublists do not have an mSize data member, so they are not exactly LinkedList objects. 5/4/2019 Recursion
Two-Part Implementation of Recursive Member Functions We implement the recursive functions in two parts. The first function is public and non-recursive. Type Class::Function(parameters) { // Do some special things… // Call the second function, // passing the head of the list Function(head, parameters); // Do more special things… return; } 5/4/2019 Recursion
Implementation of Recursive Member Functions The second function is private and recursive. Type Class::Function(LinkedListNode* node, parameters) { if (condition) // Handle the recursive case return Function(node->next, parameters); } else // Handle the non-recursive case 5/4/2019 Recursion
Example: The insert() Function There are two things that the non-recursive insert() function should do only once. Check that pos is valid. Increment mSize. Then it makes a call to the recursive insert() function, passing it the head pointer. 5/4/2019 Recursion
The Non-Recursive insert() Function void insert(int pos, const T& value) { // Do something special assert(pos >= 1 && pos <= mSize); // Call the recursive function insert(head, pos, value); mSize++; return; } 5/4/2019 Recursion
The Recursive insert() Function The recursive insert() function must distinguish two cases. Recursive case The insertion takes place at a later node. Non-recursive case The insertion takes place at the current node, which is the “head” of the (sub)list. 5/4/2019 Recursion
The Recursive Case The recursive case Is distinguished by the condition that pos > 0 (a later node). Passes a pointer to the next node. Decrements the value of pos. insert(node->next, pos – 1, value); 5/4/2019 Recursion
The Non-Recursive Case Is distinguished by the condition pos == 0 (this node). Inserts the new node at the head of the (sub)list. Because node is modified, it must be passed by reference. LinkedListNode<T>* new_node = new LinkedListNode<T>(value); new_node->next = node; node = new_node; 5/4/2019 Recursion
The Recursive insert() Function void insert(LinkedListNode<T>*& node, int pos, const T& value) { // Recursive case if (pos > 1) Insert(node->next, pos – 1, value); // Non-recursive case else LinkedListNode<T>* new_node = new LinkedListNode<T>(value); new_node->next = node; node = new_node; } return; 5/4/2019 Recursion
Recursive Linked List Implementation Example recurlinkedlist.h ListTest.cpp 5/4/2019 Recursion
Evaluation of Lists Type of List Get Element (seq) (rand) Insert Delete Push Front Pop Back Array List 0.020 0.116 39.5 40.9 50.4 47.8 0.035 0.030 Circ Array List 0.015 0.074 12.3 12.2 0.027 0.032 Linked List 23.2 21.5 47.0 57.9 0.183 0.144 52.4 99.2 Linked List w Tail 23.3 21.7 57.1 0.178 0.143 0.163 52.3 Doubly Linked List 13.1 12.7 25.0 30.1 0.171 0.121 0.165 Circ Linked List 13.0 12.8 24.7 29.7 0.168 0.115 0.117 Recur Linked List 53.5 49.3 89.4 91.4 0.132 148 135 5/4/2019 Recursion