Function and class templates

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
1 CSC 222: Computer Programming II Spring 2004 Pointers and linked lists  human chain analogy  linked lists: adding/deleting/traversing nodes  Node.
Adapted from instructor resources Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights.
Final Review Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
April 27, 2017 COSC Data Structures I Review & Final Exam
Week 15 – Monday.  What did we talk about last time?  Tries.
1 Data Structures and Algorithms Queue. 2 The Queue ADT Introduction to the Queue data structure Designing a Queue class using dynamic arrays Linked Queues.
Programming with Recursion
Final Exam Review CS 3358.
Chapter 19: Recursion.
ECE Application Programming
CS505 Data Structures and Algorithms
Chapter 15 Recursion.
C++ Templates.
ECE Application Programming
Towers of Hanoi Move n (4) disks from pole A to pole C
Chapter 15 Recursion.
Week 15 – Monday CS221.
October 30th – Priority QUeues
Stack and Queue APURBO DATTA.
Programming with Recursion
Map interface Empty() - return true if the map is empty; else return false Size() - return the number of elements in the map Find(key) - if there is an.
Topic 16 Queues "FISH queue: n.
CMSC 341 Lecture 5 Stacks, Queues
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
ADT Implementations: Templates and Standard Containers
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Algorithm design and Analysis
Topic 16 Queues "FISH queue: n.
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
CSC 143 Queues [Chapter 7].
Unit 3 Test: Friday.
CS210- Lecture 5 Jun 9, 2005 Agenda Queues
EECE.2160 ECE Application Programming
Queues Jyh-Shing Roger Jang (張智星)
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
ECE Application Programming
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 13: Exam 1 Preview
Instructor: Dr. Michael Geiger Spring 2019 Lecture 29: Linked queues
Dynamic allocation (continued)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 4: Functions in C++
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 12: Exam 1 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.2160 ECE Application Programming
Heaps and priority queues
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Getting queues right … finally (?)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Instructor: Dr. Michael Geiger Spring 2017 Lecture 36: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
EECE.2160 ECE Application Programming
EECE.3170 Microprocessor Systems Design I
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
EECE.2160 ECE Application Programming
Presentation transcript:

Function and class templates EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017 Lecture 24: Function and class templates Recursion intro

Data Structures: Lecture 24 Lecture outline Announcements/reminders Program 4 still to be posted; due at some point before 4/28 Exam 2 in class Friday, 3/31 Will be allowed one 8.5” x 11” double-sided note sheet Today’s material will not be on exam—exam coverage stops with queues Today’s lecture Review: Queue implementations Function and class templates Recursion intro 11/10/2018 Data Structures: Lecture 24

Review: queue implementations Array-based queues Treat array as circular so you can add new items to lowest-indexed positions as original values in those positions are dequeued Two possible solutions Only store front/back and leave 1 array slot empty Store front/back & extra “empty” variable Linked queues Front pointer points to first node in queue Back pointer points to last node in queue Could use circular linked list with pointer only to last node (back = last node; front = (last node)->next 11/10/2018 Data Structures: Lecture 24

Review: Justifying templates Want to write general code we can use in specific cases General functions that work for different data types General classes that can store different data types 11/10/2018 Data Structures: Lecture 24

Data Structures: Lecture 24 Templates Templates allow us to write general functions/classes and specify the data type later template keyword indicates that what follows is a pattern, not a full definition Generic typename specified in angle brackets with typename (or class) keyword At least one templated parameter must be used Desired type automatically bound if calling function Desired type must be specified if declaring object 11/10/2018 Data Structures: Lecture 24

Function template example General form: template <typename type> function definition Rewriting swap with templates: template <typename T> void swap(T &v1, T &v2) { T temp = v1; v1 = v2; v2 = temp; } Calling swap: int x = 10; int y = 20; swap(x, y); 11/10/2018 Data Structures: Lecture 24

Data Structures: Lecture 24 Class templates Specify template outside of class definition Can then use templated type(s) inside definition All member functions must be declared as template functions Template specification and implementation cannot be split Function definitions listed in .h file 11/10/2018 Data Structures: Lecture 24

Class template example: array-based queue template <typename T> class AQueue { public: AQueue(); bool isEmpty(); void enqueue(T val); void dequeue(); T front(); void display(ostream &out); private: T Qarr[CAPACITY]; int Qfront, Qback; }; 11/10/2018 Data Structures: Lecture 24

Class template example: enqueue template <typename T> void AQueue<T>::enqueue(T val) { int newBack = (Qback + 1) % CAPACITY; if (newBack != Qfront) { Qarr[newBack] = val; Qback = newBack; } else cerr << "Queue is full--can't enqueue\n"; 11/10/2018 Data Structures: Lecture 24

Class template example: objects Specify desired types of storage after typename Two different instances of AQueue: AQueue <int> intQ; AQueue <double> doubleQ; 11/10/2018 Data Structures: Lecture 24

Data Structures: Lecture 24 Recursion Some functions call other functions Recursive functions call themselves A recursive function has two parts Anchor / base case: function value is defined for one or more specific argument values Inductive / recursive case: function value for current argument value defined in terms of previously defined function values and/or arguments 11/10/2018 Data Structures: Lecture 24

Data Structures: Lecture 24 Recursive example Recursive power function double power(double x, unsigned n){ if (n == 0) return 1.0; else return x * power(x, n – 1); } 11/10/2018 Data Structures: Lecture 24

Data Structures: Lecture 24 Recursion downsides Space efficiency Every function needs a stack frame/activation record Computational efficiency Recursive solution may not be as efficient Example: Fibonacci sequence (1, 1, 2, 3, 5, 8 …) Recursive function to find nth Fibonacci number int fib(unsigned n) { if (n <= 2) return 1; else return fib(n-1) + fib(n-2); } 11/10/2018 Data Structures: Lecture 24

Data Structures: Lecture 24 Common recursive uses Use if, on each iteration, problem splits into 2 or more similar tasks Don’t want to repeat work Graph/tree traversal Will see this with binary trees Divide and conquer algorithms Search, sort algorithms very common examples Think about binary search Test value at midpoint of array If higher than value you’re searching for, test lower half If lower than value you’re searching for, test upper half 11/10/2018 Data Structures: Lecture 24

Data Structures: Lecture 24 Final notes Next time: Exam 2 Preview Reminders: Program 4 still to be posted; due date TBD Exam 2 in class Friday, 3/31 Will be allowed one 8.5” x 11” double-sided note sheet Today’s material will not be on exam—exam coverage stops with queues 11/10/2018 Data Structures: Lecture 24