Download presentation
Presentation is loading. Please wait.
1
Templated recursive linked lists
uff da But first...
2
Women in Computing Ice cream social Thursday at 6pm in Herak Courtyard
The group is for all who are in favor of gender equality in computer science
3
Tutoring
4
Assignment 1 For your first assignment, you will be taking linked list and adapting it to be templated, and to have all functions implemented recursively This assignment is due a week from today Let's take a look at the existing code...
5
Assertions An assertion is a statement that must be true for your code to function correctly Include the header file <cassert> to have access to assert() assert() takes a single parameter: a boolean expression that should be true If the parameter is false, assert() will stop your program, output an error message, and trigger a debugger (if you are running with one) assert(factorial(1) == 1) a.out: main.cpp:13: int main(): Assertion `factorial(1) == 1' failed.
6
Using a templated class
Think of templates as telling C++ a recipe to create a class based on a class you're passing as a parameter Whenever* you're referring to a templated class, you must include what type you're passing to it Let's look at a TenElementArray class...
7
TenElementArray code #ifndef TENELEMENTARRAY_H
#define TENELEMENTARRAY_H template<class ArrayType> class TenElementArray { public: ArrayType data[10]; void setValue(ArrayType val, int index) { data[index] = val; } }; #endif
8
Making a class templated
If you have an existing class, there's a fairly straightforward method to make it templated: Move member function bodies into the header file Choose an identifier to use as your template class parameter Replace datatypes you want to be templated with the parameter name Change places using the class to specify the template type For an example, let's look at the homework assignment writeup...
9
Recursion We need two things to make a recursive solution to a problem: A base case Other cases that we can solve in terms of solutions to simpler versions of the problem Let's look at the factorial function
10
Recursion: factorial introduction
The factorial of a non-negative number n is equal to to all numbers between 1 and that number multiplied together An iterative solution to this is fairly simple Give it a shot!
11
Recursion: factorial via recursion
We need two things to make a recursive solution to a problem: A base case Other cases that we can solve in terms of solutions to simpler versions of the problem What is our base case for factorial? What is our recursive step? (How does factorial(n) relate to the answer to a simpler version of the problem?)
12
Debugging recursion When you compile your code, use the -Wall and -g flags If you're on clang++ on macOS or Linux, also use -fsanitize=address If your program crashes, try running it under a debugger With gdb: gdb your-program-name gdb ./a.out With lldb: lldb your-program-name lldb ./a.out Then type r<enter> to run your program If it crashes, type bt<enter> to see what functions were running and where
13
Recursion: helper functions
Sometimes, for the recursive call, we need more information that we need in our public-facing function In these cases, we write helper functions that perform the recursion Our publically-accessible will almost immediately return the answer as calculated by the helper function Let's look at our factorial example again...
14
Factorial with a helper function
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.