Recursive Linked Lists

Similar documents
Lecture 29 Section Wed, Apr 1, 2009

Implementing Linked Lists

Linked Lists. Lecture 16 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 22, 2017

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Fri, Feb 16, 2018

List Iterator Implementation

Array Lists. Lecture 15. Robb T. Koether. Hampden-Sydney College. Mon, Feb 22, 2016

List Iterators. Lecture 27 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

List Iterators. Lecture 34 Section Robb T. Koether. Hampden-Sydney College. Wed, Apr 24, 2013

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Friends and Unary Operators

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Minimal Spanning Trees

Solving Recursive Sequences by Iteration

Inheritance: The Fundamental Functions

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Inheritance: The Fundamental Functions

Scope and Parameter Passing

The Critical-Path Algorithm

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 18, 2013

LR Parsing - Conflicts

while Loops Lecture 13 Sections Robb T. Koether Wed, Sep 26, 2018 Hampden-Sydney College

Recursion. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 17, 2018

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Magnification and Minification

The Traveling Salesman Problem Brute Force Method

Scope and Parameter Passing

Recursion. Lecture 26 Sections , Robb T. Koether. Hampden-Sydney College. Mon, Apr 6, 2015

Programming Languages

Binary Tree Applications

The x86 Architecture

Stacks and their Applications

Pointers. Lecture 1 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 14, 2015

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

The string Class. Lecture 21 Sections 2.9, 3.9, Robb T. Koether. Wed, Oct 17, Hampden-Sydney College

Sampling Distribution Examples Sections 15.4, 15.5

Pointers. Lecture 2 Sections Robb T. Koether. Hampden-Sydney College. Mon, Jan 20, 2014

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

The Class Construct Part 1

The Constructors. Lecture 6 Sections Robb T. Koether. Hampden-Sydney College. Fri, Jan 26, 2018

Binary Tree Implementation

Dynamic Allocation of Memory

Recognition of Tokens

The Constructors. Lecture 7 Sections Robb T. Koether. Hampden-Sydney College. Wed, Feb 1, 2017

Rotations and Translations

The Graphics Pipeline

Scheduling and Digraphs

Recursive Descent Parsers

The x86 Instruction Set

The Decreasing-Time Algorithm

The Pairwise-Comparison Method

Programming Languages

Pointer Arithmetic. Lecture 4 Chapter 10. Robb T. Koether. Hampden-Sydney College. Wed, Jan 25, 2017

The Plurality-with-Elimination Method

Binary Tree Implementation

LR Parsing - The Items

Density Curves Sections

Function Usage. Lecture 15 Sections 6.3, 6.4. Robb T. Koether. Hampden-Sydney College. Mon, Oct 1, 2018

Function Definition Syntax Tree

The Coefficient of Determination

The Graphics Pipeline

Boxplots. Lecture 17 Section Robb T. Koether. Hampden-Sydney College. Wed, Feb 10, 2010

Ambient and Diffuse Light

XQuery FLOWR Expressions Lecture 35

PHP Queries and HTML Forms Lecture 23

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Abstract Data Types. Lecture 23 Section 7.1. Robb T. Koether. Hampden-Sydney College. Wed, Oct 24, 2012

Building the Abstract Syntax Trees

The Class Construct Part 2

XPath Lecture 34. Robb T. Koether. Hampden-Sydney College. Wed, Apr 11, 2012

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Street-Routing Problems

Dynamic Allocation of Memory

The CYK Parsing Algorithm

Displaying Distributions - Quantitative Variables

Abstract Syntax Trees Synthetic and Inherited Attributes

Nondeterministic Programming in C++

Lecture 37 Section 9.4. Wed, Apr 22, 2009

Webpage Navigation. Lecture 27. Robb T. Koether. Hampden-Sydney College. Mon, Apr 2, 2018

PHP Querying. Lecture 21. Robb T. Koether. Hampden-Sydney College. Fri, Mar 2, 2018

The x87 Floating-Point Unit

Lecture 25 Sections 8.4, 8.6. Fri, Apr 24, 2009

Total Orders. Lecture 41 Section 8.5. Robb T. Koether. Hampden-Sydney College. Mon, Apr 8, 2013

Basic PHP Lecture 17

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Shader Programs. Lecture 30 Subsections 2.8.2, Robb T. Koether. Hampden-Sydney College. Wed, Nov 16, 2011

The Normal Distribution

XML Attributes. Lecture 33. Robb T. Koether. Hampden-Sydney College. Wed, Apr 25, 2018

Form Validation. Lecture 25. Robb T. Koether. Hampden-Sydney College. Wed, Mar 23, 2018

The Projection Matrix

Relational Databases

XPath. Lecture 36. Robb T. Koether. Wed, Apr 16, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XPath Wed, Apr 16, / 28

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

The Model Stack. Lecture 8. Robb T. Koether. Hampden-Sydney College. Wed, Sep 6, 2017

Introduction to Compiler Design

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

Integer Overflow. Lecture 8 Section 2.5. Robb T. Koether. Hampden-Sydney College. Mon, Jan 27, 2014

Mipmaps. Lecture 35. Robb T. Koether. Hampden-Sydney College. Wed, Nov 18, 2015

The Projection Matrix

XML. Lecture 29. Robb T. Koether. Fri, Mar 28, Hampden-Sydney College. Robb T. Koether (Hampden-Sydney College) XML Fri, Mar 28, / 23

Aggregation. Lecture 7 Section Robb T. Koether. Hampden-Sydney College. Wed, Jan 29, 2014

Transcription:

Recursive Linked Lists Lecture 28 Sections 14.1-14.5, 14.7 Robb T. Koether Hampden-Sydney College Fri, Mar 31, 2017 Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 1 / 23

1 Recursive Linked Lists 2 Recursive Member Functions The locatenode() Function The insert() Function The remove() Function 3 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 2 / 23

Outline 1 Recursive Linked Lists 2 Recursive Member Functions The locatenode() Function The insert() Function The remove() Function 3 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 3 / 23

Recursive Linked Lists A linked list is a naturally recursive structure. The linked list is a pointer to a node. (Ignore the m_size 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 m_size data member, so they are not exactly LinkedList objects. Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 4 / 23

Outline 1 Recursive Linked Lists 2 Recursive Member Functions The locatenode() Function The insert() Function The remove() Function 3 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 5 / 23

Two-Part Implementation of Recursive Member Functions Recursive Member Functions Type Class::func(parameters) // Non-recursive func() { {Do some special things...} {Call the second function,} {passing the head of the list} func(m_head, parameters); // Recursive func() {Do more special things...} return; } We implement the recursive functions in two parts. The first function is public and nonrecursive. Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 6 / 23

Implementation of Recursive Member Functions Example (A Typical Recursive Function) ret-type Class::func(LinkedListNode* node, parameters) { if (condition) { // Handle the recursive case return func(node->next, parameters); } else { // Handle the nonrecursive case } return; } Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 7 / 23

Outline 1 Recursive Linked Lists 2 Recursive Member Functions The locatenode() Function The insert() Function The remove() Function 3 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 8 / 23

The Nonrecursive locatenode() Function Example (The Nonrecursive locatenode() Function) LinkedListNode<T>* locatenode(int pos) const { assert(pos >= 0 && pos < m_size); return locatenode(m_head, pos); } Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 9 / 23

The Recursive locatenode() Function Example (The Recursive locatenode() Function) LinkedListNode<T>* locatenode(linkedlistnode<t>* node, int pos) const { if (pos == 0) return node; else return locatenode(node->m_next, pos - 1); } Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 10 / 23

Outline 1 Recursive Linked Lists 2 Recursive Member Functions The locatenode() Function The insert() Function The remove() Function 3 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 11 / 23

Example Example (The insert() Function) There are two things that the nonrecursive insert() function should do only once. Check that pos is valid. Increment m_size. Then it makes a call to the recursive insert() function, passing it the head pointer. Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 12 / 23

The Nonrecursive insert() Function Example (The Nonrecursive insert() Function) void insert(int pos, const T& value) { {Do something special} assert(pos >= 0 && pos <= m_size); {Call the recursive function} insert(m_head, pos, value); {Do something special} m_size++; return; } Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 13 / 23

The Recursive insert() Function Example (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. Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 14 / 23

The Recursive Case Example (The Recursive Case) The recursive case Is distinguished by the condition that pos > 0 (action at a later node). Passes a pointer to the next node. Decrements the value of pos. Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 15 / 23

The Recursive Case Example (The Recursive Case) if (pos > 0) insert(node->m_next, pos - 1, value);. Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 16 / 23

The Non-Recursive Case Example (The Non-Recursive Case) The non-recursive case Is distinguished by the condition pos == 0 (action at this node). Inserts the new node at the head of the (sub)list. Because node is modified, it must be passed by reference. Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 17 / 23

The Non-Recursive Case Example (The Non-Recursive Case) if (pos == 0) { LinkedListNode<T>* new_node = new LinkedListNode<T>(value); new_node->m_next = node; node = new_node; } Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 18 / 23

The Recursive insert() Function Example (Recursive insert() Function) void insert(linkedlistnode<t>*& node, int pos, const T& value) { // Recursive case if (pos > 1) insert(node->m_next, pos - 1, value); // Non-recursive case else { LinkedListNode<T>* new_node = new LinkedListNode<T>(value); new_node->m_next = node; node = new_node; } return; } Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 19 / 23

Outline 1 Recursive Linked Lists 2 Recursive Member Functions The locatenode() Function The insert() Function The remove() Function 3 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 20 / 23

The Recursive remove() Function Write a recursive implementation of the remove() function. Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 21 / 23

Outline 1 Recursive Linked Lists 2 Recursive Member Functions The locatenode() Function The insert() Function The remove() Function 3 Assignment Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 22 / 23

Assignment Homework Read Section 14.1-14.5, 14.7. Robb T. Koether (Hampden-Sydney College) Recursive Linked Lists Fri, Mar 31, 2017 23 / 23