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

Unformatted text preview:

Recursive Linked ListsRecursive Member FunctionsThe Recursive Insert FunctionEvaluation of List ImplementationsAssignmentRecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentRecursive Linked ListsLecture 29Section 10.3Robb T. KoetherHampden-Sydney CollegeWed, Apr 1, 2009RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentOutline1Recursive Linked Lists2Recursive Member Functions3The Recursive Insert Function4Evaluation of List Implementations5AssignmentRecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentRecursive Linked ListsA linked list is a naturally recursive structure.The “linked list” is a pointer to a node. (Ignore themSize 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 datamember, so they are not exactly LinkedList objects.RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentTwo-Part Implementation of Recursive MemberFunctionsRecursive Member FunctionsType Class::func(parameters){// Do some special things...// Call the second function,// passing the head of the listfunc(head, parameters);// Do more special things...return;}We implement the recursive functions in two parts.The first function is public and nonrecursive.RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentImplementation of Recursive MemberFunctionsExample (A Typical Recursive Function)Type Class::Function(LinkedListNode*node, parameters){if (condition){// Handle the recursive casereturn Function(node->next, parameters);}else{// Handle the nonrecursive case}return;}RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentExampleExample (The insert() Function)There are two things that the nonrecursive 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.RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentThe Nonrecursive insert() FunctionExample (The Nonrecursive insert() Function)void insert(int pos, const T& value){// Do something specialassert(pos >= 0 && pos <= mSize);// Call the recursive functioninsert(head, pos, value);// Do something specialmSize++;return;}RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentThe Recursive insert() FunctionExample (The Recursive insert() Function)The recursive insert() function must distinguish twocases.Recursive case - The insertion takes place at a laternode.Non-recursive case - The insertion takes place at thecurrent node, which is the “head” of the (sub)list.RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentThe Recursive CaseExample (The Recursive insert() Function)The recursive caseIs distinguished by the condition that pos > 0 (actionat a later node).Passes a pointer to the next node.Decrements the value of pos.RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentThe Non-Recursive CaseExample (The Recursive insert() Function)if (pos > 0)insert(node->next, pos - 1, value);RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentThe Non-Recursive CaseExample (The Recursive insert() Function)The non-recursive caseIs distinguished by the condition pos == 0 (action atthis node).Inserts the new node at the head of the (sub)list.Because node is modified, it must be passed byreference.RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentThe Non-Recursive CaseExample (The Recursive insert() Function)if (pos == 0){LinkedListNode<T>*new_node= new LinkedListNode<T>(value);new_node->next = node;node = new_node;}RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentThe Recursive insert() FunctionExample (Recursive insert() Function)void insert(LinkedListNode<T>*& node, int pos,const T& value){// Recursive caseif (pos > 1)insert(node->next, pos - 1, value);// Non-recursive caseelse{LinkedListNode<T>*new_node =new LinkedListNode<T>(value);new_node->next = node;node = new_node;}return;}RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentRecursive Linked List ImplementationDownload recurlinkedlist.hDownload and run ListTest.cppRecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentEvaluation of Listsget getElement Element push pop push popType of List (seq) (rand) insert remove Front Front Back BackArray List 0.002 0.025 6.55 8.53 12.4 16.2 0.020 0.013Circ Array List 0.009 0.034 3.18 2.94 0.020 0.016 0.018 0.016Linked List 8.19 7.72 33.9 36.9 0.095 0.077 16.8 33.5Linked List w/Tail 8.46 7.97 34.3 37.2 0.098 0.083 0.094 16.8Doubly Linked List 4.17 4.19 18.5 20.0 0.097 0.072 0.099 0.073Circ Linked List 4.17 4.18 18.4 20.0 0.100 0.075 0.102 0.074Recur Linked List 34.3 32.5 81.0 79.7 0.096 0.086 124 111RecursiveLinked ListsRobb T.KoetherRecursiveLinked ListsRecursiveMemberFunctionsThe RecursiveInsertFunctionEvaluation ofList Imple-mentationsAssignmentAssignmentHomeworkRead Section 10.3, pages 548 -


View Full Document

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

Download Lecture 29 - 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 29 - 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 29 - 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?