CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.

Slides:



Advertisements
Similar presentations
Chapter 17 Linked Lists.
Advertisements

Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Data Structures Data Structures Topic #5. Today’s Agenda Other types of linked lists –discuss algorithms to manage circular and doubly linked lists –should.
Data Structures Topic #9. Today’s Agenda Continue Discussing Trees Examine the algorithm to insert Examine the algorithm to remove Begin discussing efficiency.
Data Structures Data Structures Topic #8. Today’s Agenda Continue Discussing Table Abstractions But, this time, let’s talk about them in terms of new.
Linked Lists. COMP104 Lecture 33 / Slide 2 Linked Lists: Basic Idea * Data may be stored consecutively in a linked list. * Each element of the linked.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Circular List Next field in the last node contains a pointer back to the first node rather than null pointer From any point in such a list it is possible.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion Bryce Boe 2013/11/18 CS24, Fall Outline Wednesday Recap Lab 7 Iterative Solution Recursion Binary Tree Traversals Lab 7 Recursive Solution.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Stacks. An alternative storage structure for collections of entities is a stack. A stack is a simplified form of a linked list in which all insertions.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
INTRODUCTION TO BINARY TREES P SORTING  Review of Linear Search: –again, begin with first element and search through list until finding element,
CS 11 C++ track: lecture 5 Today: Member initialization lists Linked lists friend functions.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 12 – Data Structures – Trees Sorting Algorithms.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CS342 Data Structures End-of-semester Review S2002.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
1 CSC 211 Data Structures Lecture 11 Dr. Iftikhar Azim Niaz 1.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
CS162 - Topic #6 Lecture: Pointers and Dynamic Memory –Review –Dynamically allocating structures –Combining the notion of classes and pointers –Destructors.
CSS 342 DATA STRUCTURES, ALGORITHMS, AND DISCRETE MATHEMATICS I LECTURE CARRANO C++ INTERLUDE 2, CHAPT 4, 6.
CS162 - Topic #10 Lecture: Recursion –The Nature of Recursion –Tracing a Recursive Function –Work through Examples of Recursion Programming Project –Discuss.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
LINKED LISTS.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
CS162 - Topic #9 Lecture: Dynamic Data Structures –Deallocating all nodes in a LLL –Inserting nodes into sorted order Programming Assignment Questions.
Data Structures Intro2CS – week Stack ADT (Abstract Data Type) A container with 3 basic actions: – push(item) – pop() – is_empty() Semantics: –
CS505 Data Structures and Algorithms
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 4 Linked Lists.
Programming Abstractions
Introduction to C++ Recursion
Chapter 4 Linked Lists
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Chapter 16-2 Linked Structures
Linked Lists.
CSS 342 Data Structures, Algorithms, and Discrete Mathematics I
Chapter 4 Linked Lists.
Data Structures ADT List
Programming Abstractions
ADT list.
Recursive Linked Lists
Chapter 16 Linked Structures
Stacks and Queues Prof. Michael Tsai 2017/02/21.
Introduction to C++ Linear Linked Lists
Stacks and Queues.
Linked Lists.
List Iterator Implementation
Linked Lists.
CSCS-200 Data Structure and Algorithms
slides created by Marty Stepp
Presentation transcript:

CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any questions on the User’s Manual and External Design Document?

CS162 - Using Recursion Today we will walk through examples solving problems with recursion To get used to this process –we will select simple problems that in reality should be solved using iteration and not recursion –but, it should give you an understanding of how to design using recursion –which we will need to understand for CS163

CS162 - Example #1 First, let’s display the contents of a linear linked list, recursively –obviously this is should be done iteratively! –but, as an exercise determine what the stopping condition should be first: when the head pointer is NULL –what should be done when this condition is reached? return –what should be done otherwise? display and call the function recursively

CS162 - Example #1 If we were to do this iteratively: void display(node * head) { while (head) { cout data->title <<endl; head = head->next; } Why is it ok in this case to change head? Look at the stopping condition –with recursion we will replace the while with an if....and replace the traversal with a function call

CS162 - Example #1 If we were to do this recursively: void display(node * head) { if (head) { cout data->title <<endl; display(head->next); } Now, change this to display the list backwards (recursively) Discuss the code you’d need to do THAT recursively....

CS162 - Example #2 Next, let’s insert at the end of a linear linked list, recursively –again this is should be done iteratively! –but, as an exercise determine what the stopping condition should be first: when the head pointer is NULL –what should be done when this condition is reached? allocate memory and save the data –what should be done otherwise? call the function recursively with the next ptr

CS162 - Example #2 If we were to do this iteratively: void append(node * & head, const video & d) { if (!head) { head = new node; head->data = //save the data head->next = NULL; } else { node * current = head; while (current->next) { current = current->next; } current->next = new node; current = current->next; current->data = //save the data current->next = NULL; }

CS162 - Example #2 If we were to do this recursively: void append(node * & head, const video & d) { if (!head) { head = new node; head->data = //save the data head->next = NULL; } else append(head->next,d); } Notice this is much shorter (but less efficient) Notice the stopping condition (!head) Examine how the pass by reference can be used to implicitly connect up the nodes Walk thru an example of invoking this function

CS162 - Example #2 This can also be done recursively by using the returned value (rather than call by reference): node * append(node * head, const video & d) { if (!head) { head = new node; head->data = //save the data head->next = NULL; } else head ->next = append(head->next,d); return head; } Notice the function call must use the returned value Here, we are explicitly connecting up the nodes Walk thru an example of invoking this function

CS162 - Example #3 Next, let’s remove an item from a linear linked list, recursively –again this is should be done iteratively! –but, as an exercise determine what the stopping condition should be first: when the head pointer is NULL when a match (the item to be removed) is found –what should be done when this condition is reached? deallocate memory –what should be done otherwise? call the function recursively with the next ptr

CS162 - Example #3 If we were to do this recursively: int remove(node * & head, const video & d) { if (!head) return 0; //match not found! if (strcmp(head->data->title, d->title)==0) { delete [] head->data->title; delete head->data; delete head; head = NULL; return 1; } return remove(head->next,d); } Does this reconnect the nodes? How does it handle the special cases of a) empty list, b) deleting the first item, c) deleting elsewhere

CS162 - More Examples Now in class, let’s design and implement the following recursively –count the number of items in a linear linked list –delete all nodes in a linear linked list Why would recursion not be the proper solution for push, pop, enqueue, dequeue?

CS162 - More Examples What is the output for the following program fragment? called: f(5) int f(int n) { cout <<n <<endl; if (n == 0) return 4; else if (n == 1) return 2; else if (n == 2) return 3; n=f(n-2) * f(n-4); cout <<n <<endl; return n; }

CS162 - More Examples What is the output of the following program or write INFINITE if there are indefinite recursive calls? called: cout <<watch(-7) int watch(int n) { if (n > 0) return n; cout <<n <<endl; return watch(n+2)*2; }

CS162 - For Next Time Practice Recursion Do the following: –Make a copy of a linear linked list, recursively –Merge two sorted linear linked lists, keeping the result sorted, recursively