Recitation 8 Recursion and Linked Lists Ben Stoddard Tabitha Peck March 7, 2014.

Slides:



Advertisements
Similar presentations
PRESENTED BY MATTHEW GRAF AND LEE MIROWITZ Linked Lists.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
David Weinberg presents Linked Lists: The Background  Linked Lists are similar to ArrayLists in their appearance and method of manipulation  They do.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
The Singleton Pattern II Recursive Linked Structures.
CS252: Systems Programming Ninghui Li Program Interview Questions.
Variations on Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Recursion Introduction to Computing Science and Programming I.
Final Exam Preview Chapter 4,5,6,7,8,9. Remember to evaluate CS221  Go to  Ends tonight.
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
Midterm Exam Two Tuesday, November 25 st In class cumulative.
Introduction to C Programming CE Lecture 21 Recursion and Linear Linked Lists.
Chapter 3: Arrays, Linked Lists, and Recursion
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
Week 7 - Wednesday.  What did we talk about last time?  Recursive running time  Master Theorem  Introduction to trees.
20 Questions Assignment Intro James Wei Professor Peck March 19, 2014 Slides by James Wei.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
CSE 131 Computer Science 1 Module 9: Linked Lists Using references to link objects Basic operations on linked lists Implementing a linked list of integers.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Programming Paradigms Backus Naur Form and Syntax Diagrams.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Binary Search Trees Nilanjan Banerjee. 2 Goal of today’s lecture Learn about Binary Search Trees Discuss the first midterm.
Recitation 2 James Wei Professor Peck 1/17/2013. Covered in this Recitation Arrays Variable Scoping Local variables Instance variables Class variables.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
1 Java linked list. Java linked list - definition ▪ Often in programming we are required to systematically store some type of information. A prime example.
Compsci 201 Recitation 10 Professor Peck Jimmy Wei 11/1/2013.
Heaps and Heap Sorting Heaps implemented in an Array Heap Sorting.
1ADS Lecture 11 Stacks contd.. ADS Lecture 113 Singly Linked list-based Stack Top of stack is head of list (can insert elements at head in constant.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Lab - 11 Keerthi Nelaturu. Ordered Structure Using Doubly linked list All elements in the list are arranged in an order Implement the Ordered Structure.
Java linked list.
Reverse Subtraction Objectives:  do a subtract by adding  check your answer by adding.
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.
Binary Search Trees (BST) Let’s look at some pics …and some code.
Linked Lists Data Structures and Algorithms CS 244 Brent M. Dingle, Ph.D. Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Recursion repetition without loops. Recursion  definition solving a problem in terms of a smaller version of itself  form of a recursive solution: base.
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.
Week 15 – Monday.  What did we talk about last time?  Tries.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Recursion CS 180 Department of Computer Science Purdue University.
Recursion Powerful Tool
Introduction to Computing Science and Programming I
Doubly Linked List Review - We are writing this code
Mid Term Review Advanced Programming Ananda Gunawardena
EEL 4854 IT Data Structures Linked Lists
Week 15 – Monday CS221.
ENEE150 Discussion 13 Section 0101 Adam Wang.
Linked list insertion Head. Linked list insertion Head.
8-1.
Prof. Neary Adapted from slides by Dr. Katherine Gibson
8-1.
LINKED LISTS.
Selection Sort Find the smallest value in the array. Put it in location zero. Find the second smallest value in the array and put it in location 1. Find.
Lesson Objectives Aims
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Heaps and Heap Sorting Heaps implemented in an Array Heap Sorting.
Linked Lists [AJ 15].
More Linked Lists and Loops Based on slides by Ethan Apter
Recursive Linked List Operations
slides created by Alyssa Harding
Linked Lists.
COMPUTER 2430 Object Oriented Programming and Data Structures I
slides created by Marty Stepp
Ignite presentation - Title slide
Presentation transcript:

Recitation 8 Recursion and Linked Lists Ben Stoddard Tabitha Peck March 7, 2014

Covered in this recitation Recursion Linked Lists Recursion + Linked Lists

Why recursion? Many of these methods would work better with loops. Recursion will be very useful to you later (trees, midterms, finals) though, so it’s better to practice now. These problems are another chance to think about how recursion works. What are your base cases? How do you simplify the problem?

Unique to this recitation Private Variables Using Getter/Setter methods Private Helper Methods User calls the public method for the answer, and the public method uses the private helper methods In this recitation, the helper methods will be recursive (see next slide)

Add to tail public void addToTailNoRecursion(int value) { if(head == null) { head = new IntListNode(value); } else { IntListNode current = head; while(current.getNext() != null) { current = current.getNext(); } current.setNext(new IntListNode(value)); } size++; } public void addToTail(int value){ if(head == null) { head = new IntListNode(value); } else { addToTail(value, head); } size++; } private void addToTail(int value, IntListNode n) { if(n.getNext() == null) { n.setNext(new IntListNode(value)); } else { addToTail(value, n.getNext()); } Non-Recursive Recursive ← Base Case: Empty List ← Loop to advance ↑ Add new ← Call recursive helper ← Base Case: No next ↑ Recursive case: call with next node (loop advance)

What you will do Complete the recursive private helper functions Contains: Check for a value. countOccurences: Counts a value. sum: Sums the list. sumEven: Sums only the even values in the list. reverseList: Reurns the same list, but in reverse order. mergeLists: Merges this list with another and returns the resulting list. *Assumes both lists are sorted. Result should be sorted as well.

Go for it! You shouldn’t have to change any given code. You should be able to pass unit tests if you are done. These slides are online for your reference. Please do as much as you can and ask UTAs for help. Submit before March 21 (your next recitation).