Download presentation
Presentation is loading. Please wait.
1
Recursive Linked Lists
Lecture 35 Sections 10.3 – 10.4 Fri, Apr 20, 2007
2
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/24/2019 Recursion
3
Two-Part Implementation of Recursive Member Functions
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/24/2019 Recursion
4
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/24/2019 Recursion
5
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/24/2019 Recursion
6
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/24/2019 Recursion
7
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/24/2019 Recursion
8
The Recursive Case The recursive case
Is distinguished by the condition that pos > 1. Passes a pointer to the next node. Decrements the value of pos. insert(node->next, pos – 1, value); 5/24/2019 Recursion
9
The Non-Recursive Case
Is distinguished by the condition pos == 1. 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/24/2019 Recursion
10
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/24/2019 Recursion
11
Recursive Linked List Implementation
Example recurlinkedlist.h ListTest.cpp 5/24/2019 Recursion
12
Evaluation of Lists Type of List Get Element (seq) (rand) Insert Delete Push Front Pop Back Array List w Iterator 0.05 0.29 14.6 15.9 28.2 31.0 0.15 0.23 Linked List w Iterator 0.10 8.91 16.8 20.1 0.69 32.5 62.6 Circ Linked List w Iterator 0.13 5.00 8.28 9.28 0.75 0.63 0.76 0.61 Recur Linked List 35.1 35.7 71.2 57.9 0.73 0.71 147.6 115.5 5/24/2019 Recursion
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.