Introduction to C Programming CE00312-1 Lecture 21 Recursion and Linear Linked Lists.

Slides:



Advertisements
Similar presentations
1 - Recursion on linked lists Lecture 7 ADS2 Lecture 7.
Advertisements

Linked Lists Chapter 4.
LIST PROCESSING.
Appendix B Solving Recurrence Equations : With Applications to Analysis of Recursive Algorithms.
COMPSCI 105 S Principles of Computer Science Recursion 1.
Introduction to C Programming CE Lecture 20 Insertion and Deletion with Linear Linked Lists.
Programming with Recursion
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Review of Recursion. Definition: Recursion - The process of a function calling itself.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Introduction to C Programming CE Lecture 22 Recursive Functions for Insertion and Deletion in Linear Linked Lists.
Recursion!. Can a method call another method? YES.
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Recursion.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
1 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus and Robert Moyer, Montgomery County Community.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Modified from the slides by Sylvia Sorkin, Community College of Baltimore County.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Recursion in C++. Recursion Recursive tasks: A task that is defined in terms of itself. A function that calls itself. With each invocation, the problem.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Recursion. Circular Definition Circular definition Circular definition Dialectic materialism is materialism that is dialectic. Dialectic materialism is.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Principles of Programming Chapter 11: Recursive Function  In this chapter, you will learn about  Recursion function 1 NI S1 2009/10.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Section 4 Boxing classes Boxing classes Array initialization Array initialization Lists: recursion and iteration Lists: recursion and iteration.
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Principles of Programming - NI Simple Recursion Recursion is where a function calls itself. Concept of recursive function: A recursive function is.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
1 Trees What is a Tree? Tree terminology Why trees? What is a general tree? Implementing trees Binary trees Binary tree implementation Application of Binary.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Recursion Damian Gordon. Recursion Recursion: Factorial Recursion is a feature of modularisation, when a module can call itself to complete a solution.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
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.
Recursion.
CSC 205 Programming II Lecture 8 Recursion.
Recursion Recursion is a fundamental programming technique that can provide an elegant solution certain kinds of problems © 2004 Pearson Addison-Wesley.
5.13 Recursion Recursive functions Functions that call themselves
Lecture 7: Repeating a Known Number of Times
CSE 341 Lecture 5 efficiency issues; tail recursion; print
Programming with Recursion
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Recursion.
Recursion & Linked Lists
Recursion.
Recursive Linked List Operations
Java Programming: Chapter 9: Recursion Second Edition
General List.
Main() { int fact; fact = Factorial(4); } main fact.
Linked Lists.
Recursion Recursio Recursi Recurs Recur Recu Rec Re R
CSCE 206 Lab Structured Programming in C
Presentation transcript:

Introduction to C Programming CE Lecture 21 Recursion and Linear Linked Lists

Factorial of an Integer Factorial of 6 is expressed as 6! whose value is6  5  4  3  2  1 or it can be rewritten as6  5! We have defined 6! in terms of 5! - a recursive definition In general n! is n  (n-1)! when n > 0 and 0! is 1

Factorial as a C function int fact (int n) { if (n == 0) { return 1; } else { return n * fact(n - 1); }

An equivalent “functional” version is: int fact(int n) { return (n == 0 ? 1 : n * fact(n - 1)); } This can be read as “return the result of if n is 0 then 1 else factorial of n-1”.

Recursion with Linear Linked Lists Recursive functions are more convenient, shorter and easier to modify (once you get used to them!) compared to iterative functions (ie with while loops).

Printing a linear linked list Print 3 then list Print the rest

Recursive function print_list #include "LLL.h“// node structure, type void print_list(Listpointer list) { if (list != NULL)// not empty list { printf("%d\n", list -> value);// print first print_list(list -> next);// print rest }// else empty list do nothing } For the current node given by, pointer list, print its value and then the recursive call, print_list, deals with the rest.

Printing a linear linked list in reverse Print the rest in reverse then print list

Print the list in reverse #include "LLL.h“// node structure, type void print_reverse(Listpointer list) { if (list != NULL)// not empty list { print_reverse(list -> next);// print rest printf("%d\n", list -> value);// print first }// else empty list do nothing } This function recurses down the list, including the last, and prints values as it returns back up the list.

Adding the items in a linear linked list 3+sum of the rest list

Recursive function to add all the items in a list int sum(Listpointer list) { if (list == NULL) { return 0;// empty list, sum is 0 } else { return list->value + sum(list->next); }

The previous function could also be written as: int sum(Listpointer list) { return (list == NULL) ? 0 : list -> value + sum(list -> next); } This can be read as “return the result of if the list is empty then zero else this node’s value plus the sum of the rest”