Lecture 29 Section Wed, Apr 1, 2009

Similar documents
Recursive Linked Lists

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

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

List Iterator Implementation

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

Lecture 37 Section 9.4. Wed, Apr 22, 2009

Minimal Spanning Trees

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

Binary Tree Applications

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

Inheritance: The Fundamental Functions

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

Binary Tree Implementation

Friends and Unary Operators

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

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

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

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

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

Inheritance: The Fundamental Functions

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

Solving Recursive Sequences by Iteration

The Critical-Path Algorithm

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

Binary Tree Implementation

Lecture 20 Sections Wed, Apr 1, 2009

The Decreasing-Time Algorithm

The x86 Architecture

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

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

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

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

The Coefficient of Determination

Sampling Distribution Examples Sections 15.4, 15.5

The x86 Instruction Set

Stacks and their Applications

Rotations and Translations

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

Recursive Descent Parsers

1 Short Answer (7 Points Each)

Recognition of Tokens

ITI Introduction to Computing II

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

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

LR Parsing - The Items

Scope and Parameter Passing

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

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

Programming Languages

Density Curves Sections

Programming Languages

Dynamic Allocation of Memory

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

Scope and Parameter Passing

Function Definition Syntax Tree

Associate Professor Dr. Raed Ibraheem Hamed

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

Dynamic Allocation of Memory

CMPT 225. Lecture 6 linked list

The Standard Template Library Classes

The Graphics Pipeline

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

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

Lecture 2 Sections 3.6, 3.7. Fri, Aug 28, 2009

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

Algorithms and Data Structures

LR Parsing - Conflicts

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

Lecture 31 Sections 9.1, 9.2, 9.3. Mon, Nov 23, 2009

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

The Traveling Salesman Problem Nearest-Neighbor Algorithm

Street-Routing Problems

Magnification and Minification

Structured programming

Displaying Distributions - Quantitative Variables

Nondeterministic Programming in C++

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Solution for Data Structure

University of KwaZulu-Natal Pietermaritzburg Campus Examinations

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

Topics Applications Most Common Methods Serial Search Binary Search Search by Hashing (next lecture) Run-Time Analysis Average-time analysis Time anal

The Class Construct Part 2

2. List Implementations (a) Class Templates (b) Contiguous (c) Simply Linked (d) Simply Linked with Position Pointer (e) Doubly Linked

Information Science 2

Introduction to Data Structures

Lecture 23: Pointer Arithmetic

The Normal Distribution

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

PHP Queries and HTML Forms Lecture 23

Lecture 22 Sections 8.8, 8.9, Wed, Oct 28, 2009

Name: I. 20 II. 20 III. 20 IV. 40. CMSC 341 Section 01 Fall 2016 Data Structures Exam 1. Instructions: 1. This is a closed-book, closed-notes exam.

Programming II (CS300)

Course Administration

CSE302: Compiler Design

Scheduling and Digraphs

Ambient and Diffuse Light

Transcription:

The Lecture 29 Section 10.3 Hampden-Sydney College Wed, Apr 1, 2009

Outline The 1 2 3 The 4 5

The 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.

Two-Part Implementation of The Type Class::func(parameters) { // Do some special things... // Call the second function, // passing the head of the list func(head, parameters); // Do more special things... return; } We implement the recursive functions in two parts. The first function is public and nonrecursive.

Implementation of The Example (A Typical ) Type Class::(LinkedListNode* node, parameters) { if (condition) { // Handle the recursive case return (node->next, parameters); } else { // Handle the nonrecursive case } return; }

Example The Example (The insert() ) 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.

The Nonrecursive insert() The Example (The Nonrecursive insert() ) void insert(int pos, const T& value) { // Do something special assert(pos >= 0 && pos <= msize); // Call the recursive function insert(head, pos, value); // Do something special msize++; return; }

The insert() The Example (The insert() ) The recursive insert() function must distinguish two cases. 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.

The Case The Example (The insert() ) 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.

The Non- Case The Example (The insert() ) if (pos > 0) insert(node->next, pos - 1, value);

The Non- Case The Example (The insert() ) The non-recursive case Is distinguished by the condition pos == 0 (action at this node). s the new node at the head of the (sub)list. Because node is modified, it must be passed by reference.

The Non- Case The Example (The insert() ) if (pos == 0) { LinkedListNode<T>* new_node = new LinkedListNode<T>(value); new_node->next = node; node = new_node; }

The insert() The Example ( insert() ) void insert(linkedlistnode<t>*& node, int pos, const T& value) { // 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; }

Linked List Implementation The Download recurlinkedlist.h Download and run ListTest.cpp

Lists The get get Element Element push pop push pop Type of List (seq) (rand) insert remove Front Front Back Back Array List 0.002 0.025 6.55 8.53 12.4 16.2 0.020 0.013 Circ Array List 0.009 0.034 3.18 2.94 0.020 0.016 0.018 0.016 Linked List 8.19 7.72 33.9 36.9 0.095 0.077 16.8 33.5 Linked List w/tail 8.46 7.97 34.3 37.2 0.098 0.083 0.094 16.8 Doubly Linked List 4.17 4.19 18.5 20.0 0.097 0.072 0.099 0.073 Circ Linked List 4.17 4.18 18.4 20.0 0.100 0.075 0.102 0.074 Recur Linked List 34.3 32.5 81.0 79.7 0.096 0.086 124 111

The Homework Read Section 10.3, pages 548-551.