H-SC COMS 262 - Lecture 35 - Recursive Linked Lists

Unformatted text preview:

Recursive Linked ListsTopicsSlide 3Two-Part Implementation of Recursive Member FunctionsImplementation of Recursive Member FunctionsExample: The insert() FunctionThe Non-Recursive insert() FunctionThe Recursive insert() FunctionThe Recursive CaseThe Non-Recursive CaseSlide 11Recursive Linked List ImplementationEvaluation of ListsRecursive Linked ListsLecture 35Fri, Apr 21, 200601/14/19Recursion 2TopicsRecursive linked listsThe recursive insert() functionEvaluation ofThe recursive List implementationLists with iterators01/14/19Recursion 3Recursive 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.01/14/19Recursion 4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;}Two-Part Implementation of Recursive Member Functions01/14/19Recursion 5The second function is private and recursive.Type Class::Function(LinkedListNode* node, parameters){ if (condition) { // Handle the non-recursive case } else { // Handle the recursive case return Function(node->next, parameters); }}Implementation of Recursive Member Functions01/14/19Recursion 6Example: 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.01/14/19Recursion 7void insert(int pos, const T& value){// Do some special things assert(pos >= 1 && pos <= mSize); size++;// Call the recursive function Insert(head, pos, value); return;}The Non-Recursive insert() Function01/14/19Recursion 8The 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.01/14/19Recursion 9The 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);01/14/19Recursion 10The Non-Recursive Case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;01/14/19Recursion 11The Recursive insert() Functionvoid 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;}01/14/19Recursion 12Examplerecurlinkedlist.hListTest.cppRecursive Linked List Implementation01/14/19Recursion 13Evaluation of ListsType of ListGet Element(seq)Get Element(rand) Insert DeletePushFrontPopFrontPushBackPopBackArray List w Iterator0.05 0.29 14.6 15.9 28.2 31.0 0.15 0.23Linked List w Iterator0.10 8.91 16.8 20.1 0.69 0.69 32.5 62.6Circ Linked List w Iterator0.13 5.00 8.28 9.28 0.75 0.63 0.76 0.61Recur Linked List35.1 35.7 71.2 57.9 0.73 0.71 147.6


View Full Document

H-SC COMS 262 - Lecture 35 - Recursive Linked Lists

Download Lecture 35 - Recursive Linked Lists
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 35 - Recursive Linked Lists and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 35 - Recursive Linked Lists 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?